zd_mac.c 35.7 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
/* zd_mac.c
 *
 * 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; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/wireless.h>
#include <linux/usb.h>
#include <linux/jiffies.h>
#include <net/ieee80211_radiotap.h>

#include "zd_def.h"
#include "zd_chip.h"
#include "zd_mac.h"
#include "zd_ieee80211.h"
#include "zd_netdev.h"
#include "zd_rf.h"
#include "zd_util.h"

static void ieee_init(struct ieee80211_device *ieee);
static void softmac_init(struct ieee80211softmac_device *sm);
35 36
static void set_rts_cts_work(struct work_struct *work);
static void set_basic_rates_work(struct work_struct *work);
37

38 39 40 41
static void housekeeping_init(struct zd_mac *mac);
static void housekeeping_enable(struct zd_mac *mac);
static void housekeeping_disable(struct zd_mac *mac);

42
static void set_multicast_hash_handler(struct work_struct *work);
43

44 45
static void do_rx(unsigned long mac_ptr);

46 47 48 49 50 51 52 53 54
int zd_mac_init(struct zd_mac *mac,
	        struct net_device *netdev,
	        struct usb_interface *intf)
{
	struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);

	memset(mac, 0, sizeof(*mac));
	spin_lock_init(&mac->lock);
	mac->netdev = netdev;
55 56
	INIT_DELAYED_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
	INIT_DELAYED_WORK(&mac->set_basic_rates_work, set_basic_rates_work);
57

58 59 60 61
	skb_queue_head_init(&mac->rx_queue);
	tasklet_init(&mac->rx_tasklet, do_rx, (unsigned long)mac);
	tasklet_disable(&mac->rx_tasklet);

62 63 64
	ieee_init(ieee);
	softmac_init(ieee80211_priv(netdev));
	zd_chip_init(&mac->chip, netdev, intf);
65
	housekeeping_init(mac);
66
	INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
	return 0;
}

static int reset_channel(struct zd_mac *mac)
{
	int r;
	unsigned long flags;
	const struct channel_range *range;

	spin_lock_irqsave(&mac->lock, flags);
	range = zd_channel_range(mac->regdomain);
	if (!range->start) {
		r = -EINVAL;
		goto out;
	}
	mac->requested_channel = range->start;
	r = 0;
out:
	spin_unlock_irqrestore(&mac->lock, flags);
	return r;
}

89
int zd_mac_preinit_hw(struct zd_mac *mac)
90 91 92
{
	int r;
	u8 addr[ETH_ALEN];
93 94 95 96 97 98 99 100 101 102 103 104 105

	r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
	if (r)
		return r;

	memcpy(mac->netdev->dev_addr, addr, ETH_ALEN);
	return 0;
}

int zd_mac_init_hw(struct zd_mac *mac)
{
	int r;
	struct zd_chip *chip = &mac->chip;
106 107 108 109 110
	u8 default_regdomain;

	r = zd_chip_enable_int(chip);
	if (r)
		goto out;
111
	r = zd_chip_init_hw(chip);
112 113 114 115 116 117 118 119 120
	if (r)
		goto disable_int;

	ZD_ASSERT(!irqs_disabled());

	r = zd_read_regdomain(chip, &default_regdomain);
	if (r)
		goto disable_int;
	if (!zd_regdomain_supported(default_regdomain)) {
121 122 123 124 125 126 127 128
		/* The vendor driver overrides the regulatory domain and
		 * allowed channel registers and unconditionally restricts
		 * available channels to 1-11 everywhere. Match their
		 * questionable behaviour only for regdomains which we don't
		 * recognise. */
		dev_warn(zd_mac_dev(mac),  "Unrecognised regulatory domain: "
			"%#04x. Defaulting to FCC.\n", default_regdomain);
		default_regdomain = ZD_REGDOMAIN_FCC;
129 130 131 132 133 134 135 136
	}
	spin_lock_irq(&mac->lock);
	mac->regdomain = mac->default_regdomain = default_regdomain;
	spin_unlock_irq(&mac->lock);
	r = reset_channel(mac);
	if (r)
		goto disable_int;

137 138 139
	/* We must inform the device that we are doing encryption/decryption in
	 * software at the moment. */
	r = zd_set_encryption_type(chip, ENC_SNIFFER);
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	if (r)
		goto disable_int;

	r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
	if (r)
		goto disable_int;

	r = 0;
disable_int:
	zd_chip_disable_int(chip);
out:
	return r;
}

void zd_mac_clear(struct zd_mac *mac)
{
156
	flush_workqueue(zd_workqueue);
157 158
	skb_queue_purge(&mac->rx_queue);
	tasklet_kill(&mac->rx_tasklet);
159
	zd_chip_clear(&mac->chip);
U
Ulrich Kunitz 已提交
160 161
	ZD_ASSERT(!spin_is_locked(&mac->lock));
	ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
162 163 164 165 166
}

static int reset_mode(struct zd_mac *mac)
{
	struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
167 168
	u32 filter = (ieee->iw_mode == IW_MODE_MONITOR) ? ~0 : STA_RX_FILTER;
	return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
169 170 171 172 173 174
}

int zd_mac_open(struct net_device *netdev)
{
	struct zd_mac *mac = zd_netdev_mac(netdev);
	struct zd_chip *chip = &mac->chip;
175
	struct zd_usb *usb = &chip->usb;
176 177
	int r;

178 179 180 181 182 183
	if (!usb->initialized) {
		r = zd_usb_init_hw(usb);
		if (r)
			goto out;
	}

184 185
	tasklet_enable(&mac->rx_tasklet);

186 187 188 189
	r = zd_chip_enable_int(chip);
	if (r < 0)
		goto out;

190 191 192 193
	r = zd_write_mac_addr(chip, netdev->dev_addr);
	if (r)
		goto disable_int;

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
	if (r < 0)
		goto disable_int;
	r = reset_mode(mac);
	if (r)
		goto disable_int;
	r = zd_chip_switch_radio_on(chip);
	if (r < 0)
		goto disable_int;
	r = zd_chip_set_channel(chip, mac->requested_channel);
	if (r < 0)
		goto disable_radio;
	r = zd_chip_enable_rx(chip);
	if (r < 0)
		goto disable_radio;
	r = zd_chip_enable_hwint(chip);
	if (r < 0)
		goto disable_rx;

213
	housekeeping_enable(mac);
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
	ieee80211softmac_start(netdev);
	return 0;
disable_rx:
	zd_chip_disable_rx(chip);
disable_radio:
	zd_chip_switch_radio_off(chip);
disable_int:
	zd_chip_disable_int(chip);
out:
	return r;
}

int zd_mac_stop(struct net_device *netdev)
{
	struct zd_mac *mac = zd_netdev_mac(netdev);
	struct zd_chip *chip = &mac->chip;

231 232
	netif_stop_queue(netdev);

233 234 235 236 237 238 239
	/*
	 * The order here deliberately is a little different from the open()
	 * method, since we need to make sure there is no opportunity for RX
	 * frames to be processed by softmac after we have stopped it.
	 */

	zd_chip_disable_rx(chip);
240 241
	skb_queue_purge(&mac->rx_queue);
	tasklet_disable(&mac->rx_tasklet);
242
	housekeeping_disable(mac);
243 244
	ieee80211softmac_stop(netdev);

245 246 247 248 249 250 251
	/* Ensure no work items are running or queued from this point */
	cancel_delayed_work(&mac->set_rts_cts_work);
	cancel_delayed_work(&mac->set_basic_rates_work);
	flush_workqueue(zd_workqueue);
	mac->updating_rts_rate = 0;
	mac->updating_basic_rates = 0;

252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
	zd_chip_disable_hwint(chip);
	zd_chip_switch_radio_off(chip);
	zd_chip_disable_int(chip);

	return 0;
}

int zd_mac_set_mac_address(struct net_device *netdev, void *p)
{
	int r;
	unsigned long flags;
	struct sockaddr *addr = p;
	struct zd_mac *mac = zd_netdev_mac(netdev);
	struct zd_chip *chip = &mac->chip;

	if (!is_valid_ether_addr(addr->sa_data))
		return -EADDRNOTAVAIL;

	dev_dbg_f(zd_mac_dev(mac),
		  "Setting MAC to " MAC_FMT "\n", MAC_ARG(addr->sa_data));

273 274 275 276 277
	if (netdev->flags & IFF_UP) {
		r = zd_write_mac_addr(chip, addr->sa_data);
		if (r)
			return r;
	}
278 279 280 281 282 283 284 285

	spin_lock_irqsave(&mac->lock, flags);
	memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
	spin_unlock_irqrestore(&mac->lock, flags);

	return 0;
}

286
static void set_multicast_hash_handler(struct work_struct *work)
287
{
288 289
	struct zd_mac *mac = container_of(work, struct zd_mac,
					  set_multicast_hash_work);
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
	struct zd_mc_hash hash;

	spin_lock_irq(&mac->lock);
	hash = mac->multicast_hash;
	spin_unlock_irq(&mac->lock);

	zd_chip_set_multicast_hash(&mac->chip, &hash);
}

void zd_mac_set_multicast_list(struct net_device *dev)
{
	struct zd_mc_hash hash;
	struct zd_mac *mac = zd_netdev_mac(dev);
	struct dev_mc_list *mc;
	unsigned long flags;

	if (dev->flags & (IFF_PROMISC|IFF_ALLMULTI)) {
		zd_mc_add_all(&hash);
	} else {
		zd_mc_clear(&hash);
		for (mc = dev->mc_list; mc; mc = mc->next) {
			dev_dbg_f(zd_mac_dev(mac), "mc addr " MAC_FMT "\n",
				  MAC_ARG(mc->dmi_addr));
			zd_mc_add_addr(&hash, mc->dmi_addr);
		}
	}

	spin_lock_irqsave(&mac->lock, flags);
	mac->multicast_hash = hash;
	spin_unlock_irqrestore(&mac->lock, flags);
	queue_work(zd_workqueue, &mac->set_multicast_hash_work);
}

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
int zd_mac_set_regdomain(struct zd_mac *mac, u8 regdomain)
{
	int r;
	u8 channel;

	ZD_ASSERT(!irqs_disabled());
	spin_lock_irq(&mac->lock);
	if (regdomain == 0) {
		regdomain = mac->default_regdomain;
	}
	if (!zd_regdomain_supported(regdomain)) {
		spin_unlock_irq(&mac->lock);
		return -EINVAL;
	}
	mac->regdomain = regdomain;
	channel = mac->requested_channel;
	spin_unlock_irq(&mac->lock);

	r = zd_geo_init(zd_mac_to_ieee80211(mac), regdomain);
	if (r)
		return r;
	if (!zd_regdomain_supports_channel(regdomain, channel)) {
		r = reset_channel(mac);
		if (r)
			return r;
	}

	return 0;
}

u8 zd_mac_get_regdomain(struct zd_mac *mac)
{
	unsigned long flags;
	u8 regdomain;

	spin_lock_irqsave(&mac->lock, flags);
	regdomain = mac->regdomain;
	spin_unlock_irqrestore(&mac->lock, flags);
	return regdomain;
}

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
/* Fallback to lowest rate, if rate is unknown. */
static u8 rate_to_zd_rate(u8 rate)
{
	switch (rate) {
	case IEEE80211_CCK_RATE_2MB:
		return ZD_CCK_RATE_2M;
	case IEEE80211_CCK_RATE_5MB:
		return ZD_CCK_RATE_5_5M;
	case IEEE80211_CCK_RATE_11MB:
		return ZD_CCK_RATE_11M;
	case IEEE80211_OFDM_RATE_6MB:
		return ZD_OFDM_RATE_6M;
	case IEEE80211_OFDM_RATE_9MB:
		return ZD_OFDM_RATE_9M;
	case IEEE80211_OFDM_RATE_12MB:
		return ZD_OFDM_RATE_12M;
	case IEEE80211_OFDM_RATE_18MB:
		return ZD_OFDM_RATE_18M;
	case IEEE80211_OFDM_RATE_24MB:
		return ZD_OFDM_RATE_24M;
	case IEEE80211_OFDM_RATE_36MB:
		return ZD_OFDM_RATE_36M;
	case IEEE80211_OFDM_RATE_48MB:
		return ZD_OFDM_RATE_48M;
	case IEEE80211_OFDM_RATE_54MB:
		return ZD_OFDM_RATE_54M;
	}
	return ZD_CCK_RATE_1M;
}

static u16 rate_to_cr_rate(u8 rate)
{
	switch (rate) {
	case IEEE80211_CCK_RATE_2MB:
		return CR_RATE_1M;
	case IEEE80211_CCK_RATE_5MB:
		return CR_RATE_5_5M;
	case IEEE80211_CCK_RATE_11MB:
		return CR_RATE_11M;
	case IEEE80211_OFDM_RATE_6MB:
		return CR_RATE_6M;
	case IEEE80211_OFDM_RATE_9MB:
		return CR_RATE_9M;
	case IEEE80211_OFDM_RATE_12MB:
		return CR_RATE_12M;
	case IEEE80211_OFDM_RATE_18MB:
		return CR_RATE_18M;
	case IEEE80211_OFDM_RATE_24MB:
		return CR_RATE_24M;
	case IEEE80211_OFDM_RATE_36MB:
		return CR_RATE_36M;
	case IEEE80211_OFDM_RATE_48MB:
		return CR_RATE_48M;
	case IEEE80211_OFDM_RATE_54MB:
		return CR_RATE_54M;
	}
	return CR_RATE_1M;
}

static void try_enable_tx(struct zd_mac *mac)
{
	unsigned long flags;

	spin_lock_irqsave(&mac->lock, flags);
	if (mac->updating_rts_rate == 0 && mac->updating_basic_rates == 0)
		netif_wake_queue(mac->netdev);
	spin_unlock_irqrestore(&mac->lock, flags);
}

433
static void set_rts_cts_work(struct work_struct *work)
434
{
435 436
	struct zd_mac *mac =
		container_of(work, struct zd_mac, set_rts_cts_work.work);
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
	unsigned long flags;
	u8 rts_rate;
	unsigned int short_preamble;

	mutex_lock(&mac->chip.mutex);

	spin_lock_irqsave(&mac->lock, flags);
	mac->updating_rts_rate = 0;
	rts_rate = mac->rts_rate;
	short_preamble = mac->short_preamble;
	spin_unlock_irqrestore(&mac->lock, flags);

	zd_chip_set_rts_cts_rate_locked(&mac->chip, rts_rate, short_preamble);
	mutex_unlock(&mac->chip.mutex);

	try_enable_tx(mac);
}

455
static void set_basic_rates_work(struct work_struct *work)
456
{
457 458
	struct zd_mac *mac =
		container_of(work, struct zd_mac, set_basic_rates_work.work);
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
	unsigned long flags;
	u16 basic_rates;

	mutex_lock(&mac->chip.mutex);

	spin_lock_irqsave(&mac->lock, flags);
	mac->updating_basic_rates = 0;
	basic_rates = mac->basic_rates;
	spin_unlock_irqrestore(&mac->lock, flags);

	zd_chip_set_basic_rates_locked(&mac->chip, basic_rates);
	mutex_unlock(&mac->chip.mutex);

	try_enable_tx(mac);
}

static void bssinfo_change(struct net_device *netdev, u32 changes)
{
	struct zd_mac *mac = zd_netdev_mac(netdev);
	struct ieee80211softmac_device *softmac = ieee80211_priv(netdev);
	struct ieee80211softmac_bss_info *bssinfo = &softmac->bssinfo;
	int need_set_rts_cts = 0;
	int need_set_rates = 0;
	u16 basic_rates;
	unsigned long flags;

	dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);

	if (changes & IEEE80211SOFTMAC_BSSINFOCHG_SHORT_PREAMBLE) {
		spin_lock_irqsave(&mac->lock, flags);
		mac->short_preamble = bssinfo->short_preamble;
		spin_unlock_irqrestore(&mac->lock, flags);
		need_set_rts_cts = 1;
	}

	if (changes & IEEE80211SOFTMAC_BSSINFOCHG_RATES) {
		/* Set RTS rate to highest available basic rate */
496
		u8 hi_rate = ieee80211softmac_highest_supported_rate(softmac,
497
			&bssinfo->supported_rates, 1);
498
		hi_rate = rate_to_zd_rate(hi_rate);
499 500

		spin_lock_irqsave(&mac->lock, flags);
501 502
		if (hi_rate != mac->rts_rate) {
			mac->rts_rate = hi_rate;
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
			need_set_rts_cts = 1;
		}
		spin_unlock_irqrestore(&mac->lock, flags);

		/* Set basic rates */
		need_set_rates = 1;
		if (bssinfo->supported_rates.count == 0) {
			/* Allow the device to be flexible */
			basic_rates = CR_RATES_80211B | CR_RATES_80211G;
		} else {
			int i = 0;
			basic_rates = 0;

			for (i = 0; i < bssinfo->supported_rates.count; i++) {
				u16 rate = bssinfo->supported_rates.rates[i];
				if ((rate & IEEE80211_BASIC_RATE_MASK) == 0)
					continue;

				rate &= ~IEEE80211_BASIC_RATE_MASK;
				basic_rates |= rate_to_cr_rate(rate);
			}
		}
		spin_lock_irqsave(&mac->lock, flags);
		mac->basic_rates = basic_rates;
		spin_unlock_irqrestore(&mac->lock, flags);
	}

	/* Schedule any changes we made above */

	spin_lock_irqsave(&mac->lock, flags);
	if (need_set_rts_cts && !mac->updating_rts_rate) {
		mac->updating_rts_rate = 1;
		netif_stop_queue(mac->netdev);
536
		queue_delayed_work(zd_workqueue, &mac->set_rts_cts_work, 0);
537 538 539 540
	}
	if (need_set_rates && !mac->updating_basic_rates) {
		mac->updating_basic_rates = 1;
		netif_stop_queue(mac->netdev);
541 542
		queue_delayed_work(zd_workqueue, &mac->set_basic_rates_work,
				   0);
543 544 545 546
	}
	spin_unlock_irqrestore(&mac->lock, flags);
}

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
static void set_channel(struct net_device *netdev, u8 channel)
{
	struct zd_mac *mac = zd_netdev_mac(netdev);

	dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);

	zd_chip_set_channel(&mac->chip, channel);
}

int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
{
	unsigned long lock_flags;
	struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);

	if (ieee->iw_mode == IW_MODE_INFRA)
		return -EPERM;

	spin_lock_irqsave(&mac->lock, lock_flags);
	if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
		spin_unlock_irqrestore(&mac->lock, lock_flags);
		return -EINVAL;
	}
	mac->requested_channel = channel;
	spin_unlock_irqrestore(&mac->lock, lock_flags);
	if (netif_running(mac->netdev))
		return zd_chip_set_channel(&mac->chip, channel);
	else
		return 0;
}

577
u8 zd_mac_get_channel(struct zd_mac *mac)
578
{
579
	u8 channel = zd_chip_get_channel(&mac->chip);
580

581 582
	dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
	return channel;
583 584 585
}

/* If wrong rate is given, we are falling back to the slowest rate: 1MBit/s */
586
static u8 zd_rate_typed(u8 zd_rate)
587 588
{
	static const u8 typed_rates[16] = {
589 590 591 592
		[ZD_CCK_RATE_1M]	= ZD_CS_CCK|ZD_CCK_RATE_1M,
		[ZD_CCK_RATE_2M]	= ZD_CS_CCK|ZD_CCK_RATE_2M,
		[ZD_CCK_RATE_5_5M]	= ZD_CS_CCK|ZD_CCK_RATE_5_5M,
		[ZD_CCK_RATE_11M]	= ZD_CS_CCK|ZD_CCK_RATE_11M,
593 594 595 596 597 598 599 600 601 602 603
		[ZD_OFDM_RATE_6M]	= ZD_CS_OFDM|ZD_OFDM_RATE_6M,
		[ZD_OFDM_RATE_9M]	= ZD_CS_OFDM|ZD_OFDM_RATE_9M,
		[ZD_OFDM_RATE_12M]	= ZD_CS_OFDM|ZD_OFDM_RATE_12M,
		[ZD_OFDM_RATE_18M]	= ZD_CS_OFDM|ZD_OFDM_RATE_18M,
		[ZD_OFDM_RATE_24M]	= ZD_CS_OFDM|ZD_OFDM_RATE_24M,
		[ZD_OFDM_RATE_36M]	= ZD_CS_OFDM|ZD_OFDM_RATE_36M,
		[ZD_OFDM_RATE_48M]	= ZD_CS_OFDM|ZD_OFDM_RATE_48M,
		[ZD_OFDM_RATE_54M]	= ZD_CS_OFDM|ZD_OFDM_RATE_54M,
	};

	ZD_ASSERT(ZD_CS_RATE_MASK == 0x0f);
604
	return typed_rates[zd_rate & ZD_CS_RATE_MASK];
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 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 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
}

int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
{
	struct ieee80211_device *ieee;

	switch (mode) {
	case IW_MODE_AUTO:
	case IW_MODE_ADHOC:
	case IW_MODE_INFRA:
		mac->netdev->type = ARPHRD_ETHER;
		break;
	case IW_MODE_MONITOR:
		mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
		break;
	default:
		dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
		return -EINVAL;
	}

	ieee = zd_mac_to_ieee80211(mac);
	ZD_ASSERT(!irqs_disabled());
	spin_lock_irq(&ieee->lock);
	ieee->iw_mode = mode;
	spin_unlock_irq(&ieee->lock);

	if (netif_running(mac->netdev))
		return reset_mode(mac);

	return 0;
}

int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
{
	unsigned long flags;
	struct ieee80211_device *ieee;

	ieee = zd_mac_to_ieee80211(mac);
	spin_lock_irqsave(&ieee->lock, flags);
	*mode = ieee->iw_mode;
	spin_unlock_irqrestore(&ieee->lock, flags);
	return 0;
}

int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
{
	int i;
	const struct channel_range *channel_range;
	u8 regdomain;

	memset(range, 0, sizeof(*range));

	/* FIXME: Not so important and depends on the mode. For 802.11g
	 * usually this value is used. It seems to be that Bit/s number is
	 * given here.
	 */
	range->throughput = 27 * 1000 * 1000;

	range->max_qual.qual = 100;
	range->max_qual.level = 100;

	/* FIXME: Needs still to be tuned. */
	range->avg_qual.qual = 71;
	range->avg_qual.level = 80;

	/* FIXME: depends on standard? */
	range->min_rts = 256;
	range->max_rts = 2346;

	range->min_frag = MIN_FRAG_THRESHOLD;
	range->max_frag = MAX_FRAG_THRESHOLD;

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

	range->we_version_compiled = WIRELESS_EXT;
	range->we_version_source = 20;

685 686 687
	range->enc_capa = IW_ENC_CAPA_WPA |  IW_ENC_CAPA_WPA2 |
			  IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;

688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
	ZD_ASSERT(!irqs_disabled());
	spin_lock_irq(&mac->lock);
	regdomain = mac->regdomain;
	spin_unlock_irq(&mac->lock);
	channel_range = zd_channel_range(regdomain);

	range->num_channels = channel_range->end - channel_range->start;
	range->old_num_channels = range->num_channels;
	range->num_frequency = range->num_channels;
	range->old_num_frequency = range->num_frequency;

	for (i = 0; i < range->num_frequency; i++) {
		struct iw_freq *freq = &range->freq[i];
		freq->i = channel_range->start + i;
		zd_channel_to_freq(freq, freq->i);
	}

	return 0;
}

708
static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
709 710
{
	static const u8 rate_divisor[] = {
711 712 713 714
		[ZD_CCK_RATE_1M]	=  1,
		[ZD_CCK_RATE_2M]	=  2,
		[ZD_CCK_RATE_5_5M]	= 11, /* bits must be doubled */
		[ZD_CCK_RATE_11M]	= 11,
715 716 717 718 719 720 721 722 723 724 725 726 727
		[ZD_OFDM_RATE_6M]	=  6,
		[ZD_OFDM_RATE_9M]	=  9,
		[ZD_OFDM_RATE_12M]	= 12,
		[ZD_OFDM_RATE_18M]	= 18,
		[ZD_OFDM_RATE_24M]	= 24,
		[ZD_OFDM_RATE_36M]	= 36,
		[ZD_OFDM_RATE_48M]	= 48,
		[ZD_OFDM_RATE_54M]	= 54,
	};

	u32 bits = (u32)tx_length * 8;
	u32 divisor;

728
	divisor = rate_divisor[zd_rate];
729 730 731
	if (divisor == 0)
		return -EINVAL;

732 733
	switch (zd_rate) {
	case ZD_CCK_RATE_5_5M:
734 735
		bits = (2*bits) + 10; /* round up to the next integer */
		break;
736
	case ZD_CCK_RATE_11M:
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
		if (service) {
			u32 t = bits % 11;
			*service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
			if (0 < t && t <= 3) {
				*service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
			}
		}
		bits += 10; /* round up to the next integer */
		break;
	}

	return bits/divisor;
}

enum {
	R2M_SHORT_PREAMBLE = 0x01,
	R2M_11A		   = 0x02,
};

756
static u8 zd_rate_to_modulation(u8 zd_rate, int flags)
757 758 759
{
	u8 modulation;

760
	modulation = zd_rate_typed(zd_rate);
761 762
	if (flags & R2M_SHORT_PREAMBLE) {
		switch (ZD_CS_RATE(modulation)) {
763 764 765
		case ZD_CCK_RATE_2M:
		case ZD_CCK_RATE_5_5M:
		case ZD_CCK_RATE_11M:
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
			modulation |= ZD_CS_CCK_PREA_SHORT;
			return modulation;
		}
	}
	if (flags & R2M_11A) {
		if (ZD_CS_TYPE(modulation) == ZD_CS_OFDM)
			modulation |= ZD_CS_OFDM_MODE_11A;
	}
	return modulation;
}

static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
	                      struct ieee80211_hdr_4addr *hdr)
{
	struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
	u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
782
	u8 rate, zd_rate;
783
	int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
784 785 786 787
	int is_multicast = is_multicast_ether_addr(hdr->addr1);
	int short_preamble = ieee80211softmac_short_preamble_ok(softmac,
		is_multicast, is_mgt);
	int flags = 0;
788

789 790
	/* FIXME: 802.11a? */
	rate = ieee80211softmac_suggest_txrate(softmac, is_multicast, is_mgt);
791

792 793
	if (short_preamble)
		flags |= R2M_SHORT_PREAMBLE;
794

795
	zd_rate = rate_to_zd_rate(rate);
796
	cs->modulation = zd_rate_to_modulation(zd_rate, flags);
797 798 799 800 801
}

static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
	                   struct ieee80211_hdr_4addr *header)
{
802
	struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
803 804 805 806 807 808
	unsigned int tx_length = le16_to_cpu(cs->tx_length);
	u16 fctl = le16_to_cpu(header->frame_ctl);
	u16 ftype = WLAN_FC_GET_TYPE(fctl);
	u16 stype = WLAN_FC_GET_STYPE(fctl);

	/*
809
	 * CONTROL TODO:
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
	 * - if backoff needed, enable bit 0
	 * - if burst (backoff not needed) disable bit 0
	 */

	cs->control = 0;

	/* First fragment */
	if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
		cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;

	/* Multicast */
	if (is_multicast_ether_addr(header->addr1))
		cs->control |= ZD_CS_MULTICAST;

	/* PS-POLL */
825
	if (ftype == IEEE80211_FTYPE_CTL && stype == IEEE80211_STYPE_PSPOLL)
826 827
		cs->control |= ZD_CS_PS_POLL_FRAME;

828
	/* Unicast data frames over the threshold should have RTS */
829
	if (!is_multicast_ether_addr(header->addr1) &&
830 831 832 833 834 835 836 837 838 839
	    	ftype != IEEE80211_FTYPE_MGMT &&
		    tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
		cs->control |= ZD_CS_RTS;

	/* Use CTS-to-self protection if required */
	if (ZD_CS_TYPE(cs->modulation) == ZD_CS_OFDM &&
			ieee80211softmac_protection_needed(softmac)) {
		/* FIXME: avoid sending RTS *and* self-CTS, is that correct? */
		cs->control &= ~ZD_CS_RTS;
		cs->control |= ZD_CS_SELF_CTS;
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
	}

	/* FIXME: Management frame? */
}

static int fill_ctrlset(struct zd_mac *mac,
	                struct ieee80211_txb *txb,
			int frag_num)
{
	int r;
	struct sk_buff *skb = txb->fragments[frag_num];
	struct ieee80211_hdr_4addr *hdr =
		(struct ieee80211_hdr_4addr *) skb->data;
	unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
	unsigned int next_frag_len;
	unsigned int packet_length;
	struct zd_ctrlset *cs = (struct zd_ctrlset *)
		skb_push(skb, sizeof(struct zd_ctrlset));

	if (frag_num+1  < txb->nr_frags) {
		next_frag_len = txb->fragments[frag_num+1]->len +
			        IEEE80211_FCS_LEN;
	} else {
		next_frag_len = 0;
	}
	ZD_ASSERT(frag_len <= 0xffff);
	ZD_ASSERT(next_frag_len <= 0xffff);

	cs_set_modulation(mac, cs, hdr);

	cs->tx_length = cpu_to_le16(frag_len);

	cs_set_control(mac, cs, hdr);

	packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
	ZD_ASSERT(packet_length <= 0xffff);
	/* ZD1211B: Computing the length difference this way, gives us
	 * flexibility to compute the packet length.
	 */
879
	cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
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
			packet_length - frag_len : packet_length);

	/*
	 * CURRENT LENGTH:
	 * - transmit frame length in microseconds
	 * - seems to be derived from frame length
	 * - see Cal_Us_Service() in zdinlinef.h
	 * - if macp->bTxBurstEnable is enabled, then multiply by 4
	 *  - bTxBurstEnable is never set in the vendor driver
	 *
	 * SERVICE:
	 * - "for PLCP configuration"
	 * - always 0 except in some situations at 802.11b 11M
	 * - see line 53 of zdinlinef.h
	 */
	cs->service = 0;
	r = zd_calc_tx_length_us(&cs->service, ZD_CS_RATE(cs->modulation),
		                 le16_to_cpu(cs->tx_length));
	if (r < 0)
		return r;
	cs->current_length = cpu_to_le16(r);

	if (next_frag_len == 0) {
		cs->next_frame_length = 0;
	} else {
		r = zd_calc_tx_length_us(NULL, ZD_CS_RATE(cs->modulation),
			                 next_frag_len);
		if (r < 0)
			return r;
		cs->next_frame_length = cpu_to_le16(r);
	}

	return 0;
}

static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
{
	int i, r;
918
	struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
919 920 921 922 923

	for (i = 0; i < txb->nr_frags; i++) {
		struct sk_buff *skb = txb->fragments[i];

		r = fill_ctrlset(mac, txb, i);
924 925
		if (r) {
			ieee->stats.tx_dropped++;
926
			return r;
927
		}
928
		r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
929 930
		if (r) {
			ieee->stats.tx_dropped++;
931
			return r;
932
		}
933 934 935 936 937 938 939 940 941 942 943 944
	}

	/* FIXME: shouldn't this be handled by the upper layers? */
	mac->netdev->trans_start = jiffies;

	ieee80211_txb_free(txb);
	return 0;
}

struct zd_rt_hdr {
	struct ieee80211_radiotap_header rt_hdr;
	u8  rt_flags;
945
	u8  rt_rate;
946 947
	u16 rt_channel;
	u16 rt_chbitmask;
948
} __attribute__((packed));
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966

static void fill_rt_header(void *buffer, struct zd_mac *mac,
	                   const struct ieee80211_rx_stats *stats,
			   const struct rx_status *status)
{
	struct zd_rt_hdr *hdr = buffer;

	hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
	hdr->rt_hdr.it_pad = 0;
	hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
	hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
		                 (1 << IEEE80211_RADIOTAP_CHANNEL) |
				 (1 << IEEE80211_RADIOTAP_RATE));

	hdr->rt_flags = 0;
	if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
		hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;

967 968
	hdr->rt_rate = stats->rate / 5;

969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
	/* FIXME: 802.11a */
	hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
		                             _zd_chip_get_channel(&mac->chip)));
	hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
		((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
		ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
}

/* Returns 1 if the data packet is for us and 0 otherwise. */
static int is_data_packet_for_us(struct ieee80211_device *ieee,
	                         struct ieee80211_hdr_4addr *hdr)
{
	struct net_device *netdev = ieee->dev;
	u16 fc = le16_to_cpu(hdr->frame_ctl);

	ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);

	switch (ieee->iw_mode) {
	case IW_MODE_ADHOC:
		if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
989
		    compare_ether_addr(hdr->addr3, ieee->bssid) != 0)
990 991 992 993 994 995
			return 0;
		break;
	case IW_MODE_AUTO:
	case IW_MODE_INFRA:
		if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
		    IEEE80211_FCTL_FROMDS ||
996
		    compare_ether_addr(hdr->addr2, ieee->bssid) != 0)
997 998 999 1000 1001 1002 1003
			return 0;
		break;
	default:
		ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
		return 0;
	}

1004
	return compare_ether_addr(hdr->addr1, netdev->dev_addr) == 0 ||
1005
	       (is_multicast_ether_addr(hdr->addr1) &&
1006
		compare_ether_addr(hdr->addr3, netdev->dev_addr) != 0) ||
1007 1008 1009
	       (netdev->flags & IFF_PROMISC);
}

U
Ulrich Kunitz 已提交
1010 1011 1012 1013 1014
/* Filters received packets. The function returns 1 if the packet should be
 * forwarded to ieee80211_rx(). If the packet should be ignored the function
 * returns 0. If an invalid packet is found the function returns -EINVAL.
 *
 * The function calls ieee80211_rx_mgt() directly.
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
 *
 * It has been based on ieee80211_rx_any.
 */
static int filter_rx(struct ieee80211_device *ieee,
	             const u8 *buffer, unsigned int length,
		     struct ieee80211_rx_stats *stats)
{
	struct ieee80211_hdr_4addr *hdr;
	u16 fc;

	if (ieee->iw_mode == IW_MODE_MONITOR)
		return 1;

	hdr = (struct ieee80211_hdr_4addr *)buffer;
	fc = le16_to_cpu(hdr->frame_ctl);
	if ((fc & IEEE80211_FCTL_VERS) != 0)
		return -EINVAL;

	switch (WLAN_FC_GET_TYPE(fc)) {
	case IEEE80211_FTYPE_MGMT:
		if (length < sizeof(struct ieee80211_hdr_3addr))
			return -EINVAL;
		ieee80211_rx_mgt(ieee, hdr, stats);
		return 0;
	case IEEE80211_FTYPE_CTL:
		return 0;
	case IEEE80211_FTYPE_DATA:
U
Ulrich Kunitz 已提交
1042
		/* Ignore invalid short buffers */
1043 1044 1045 1046 1047 1048 1049 1050
		if (length < sizeof(struct ieee80211_hdr_3addr))
			return -EINVAL;
		return is_data_packet_for_us(ieee, hdr);
	}

	return -EINVAL;
}

1051 1052 1053
static void update_qual_rssi(struct zd_mac *mac,
			     const u8 *buffer, unsigned int length,
			     u8 qual_percent, u8 rssi_percent)
1054 1055
{
	unsigned long flags;
1056 1057 1058 1059 1060 1061
	struct ieee80211_hdr_3addr *hdr;
	int i;

	hdr = (struct ieee80211_hdr_3addr *)buffer;
	if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
		return;
1062
	if (compare_ether_addr(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid) != 0)
1063
		return;
1064 1065

	spin_lock_irqsave(&mac->lock, flags);
1066 1067 1068 1069
	i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
	mac->qual_buffer[i] = qual_percent;
	mac->rssi_buffer[i] = rssi_percent;
	mac->stats_count++;
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
	spin_unlock_irqrestore(&mac->lock, flags);
}

static int fill_rx_stats(struct ieee80211_rx_stats *stats,
	                 const struct rx_status **pstatus,
		         struct zd_mac *mac,
			 const u8 *buffer, unsigned int length)
{
	const struct rx_status *status;

	*pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
	if (status->frame_status & ZD_RX_ERROR) {
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
		struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
		ieee->stats.rx_errors++;
		if (status->frame_status & ZD_RX_TIMEOUT_ERROR)
			ieee->stats.rx_missed_errors++;
		else if (status->frame_status & ZD_RX_FIFO_OVERRUN_ERROR)
			ieee->stats.rx_fifo_errors++;
		else if (status->frame_status & ZD_RX_DECRYPTION_ERROR)
			ieee->ieee_stats.rx_discards_undecryptable++;
		else if (status->frame_status & ZD_RX_CRC32_ERROR) {
			ieee->stats.rx_crc_errors++;
			ieee->ieee_stats.rx_fcs_errors++;
		}
		else if (status->frame_status & ZD_RX_CRC16_ERROR)
			ieee->stats.rx_crc_errors++;
1096 1097
		return -EINVAL;
	}
1098

1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
	memset(stats, 0, sizeof(struct ieee80211_rx_stats));
	stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
		               + sizeof(struct rx_status));
	/* FIXME: 802.11a */
	stats->freq = IEEE80211_24GHZ_BAND;
	stats->received_channel = _zd_chip_get_channel(&mac->chip);
	stats->rssi = zd_rx_strength_percent(status->signal_strength);
	stats->signal = zd_rx_qual_percent(buffer,
		                          length - sizeof(struct rx_status),
		                          status);
	stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
	stats->rate = zd_rx_rate(buffer, status);
	if (stats->rate)
		stats->mask |= IEEE80211_STATMASK_RATE;

	return 0;
}

1117
static void zd_mac_rx(struct zd_mac *mac, struct sk_buff *skb)
1118 1119 1120 1121 1122 1123
{
	int r;
	struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
	struct ieee80211_rx_stats stats;
	const struct rx_status *status;

1124 1125 1126
	if (skb->len < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
	               IEEE80211_FCS_LEN + sizeof(struct rx_status))
	{
1127 1128
		ieee->stats.rx_errors++;
		ieee->stats.rx_length_errors++;
1129 1130
		goto free_skb;
	}
1131

1132 1133
	r = fill_rx_stats(&stats, &status, mac, skb->data, skb->len);
	if (r) {
1134 1135 1136
		/* Only packets with rx errors are included here.
		 * The error stats have already been set in fill_rx_stats.
		 */
1137 1138
		goto free_skb;
	}
1139

1140 1141 1142
	__skb_pull(skb, ZD_PLCP_HEADER_SIZE);
	__skb_trim(skb, skb->len -
		        (IEEE80211_FCS_LEN + sizeof(struct rx_status)));
1143

1144 1145
	update_qual_rssi(mac, skb->data, skb->len, stats.signal,
		         status->signal_strength);
1146

1147 1148
	r = filter_rx(ieee, skb->data, skb->len, &stats);
	if (r <= 0) {
1149 1150
		if (r < 0) {
			ieee->stats.rx_errors++;
1151
			dev_dbg_f(zd_mac_dev(mac), "Error in packet.\n");
1152
		}
1153 1154
		goto free_skb;
	}
1155 1156

	if (ieee->iw_mode == IW_MODE_MONITOR)
1157
		fill_rt_header(skb_push(skb, sizeof(struct zd_rt_hdr)), mac,
1158 1159 1160
			       &stats, status);

	r = ieee80211_rx(ieee, skb, &stats);
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
	if (r)
		return;
free_skb:
	/* We are always in a soft irq. */
	dev_kfree_skb(skb);
}

static void do_rx(unsigned long mac_ptr)
{
	struct zd_mac *mac = (struct zd_mac *)mac_ptr;
	struct sk_buff *skb;

	while ((skb = skb_dequeue(&mac->rx_queue)) != NULL)
		zd_mac_rx(mac, skb);
}

int zd_mac_rx_irq(struct zd_mac *mac, const u8 *buffer, unsigned int length)
{
	struct sk_buff *skb;

	skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
	if (!skb) {
1183
		struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1184
		dev_warn(zd_mac_dev(mac), "Could not allocate skb.\n");
1185
		ieee->stats.rx_dropped++;
1186 1187 1188 1189 1190 1191
		return -ENOMEM;
	}
	skb_reserve(skb, sizeof(struct zd_rt_hdr));
	memcpy(__skb_put(skb, length), buffer, length);
	skb_queue_tail(&mac->rx_queue, skb);
	tasklet_schedule(&mac->rx_tasklet);
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
	return 0;
}

static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
		     int pri)
{
	return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
}

static void set_security(struct net_device *netdev,
			 struct ieee80211_security *sec)
{
	struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
	struct ieee80211_security *secinfo = &ieee->sec;
	int keyidx;

	dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");

	for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
		if (sec->flags & (1<<keyidx)) {
			secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
			secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
			memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
			       SCM_KEY_LEN);
		}

	if (sec->flags & SEC_ACTIVE_KEY) {
		secinfo->active_key = sec->active_key;
		dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
			"   .active_key = %d\n", sec->active_key);
	}
	if (sec->flags & SEC_UNICAST_GROUP) {
		secinfo->unicast_uses_group = sec->unicast_uses_group;
		dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
			"   .unicast_uses_group = %d\n",
			sec->unicast_uses_group);
	}
	if (sec->flags & SEC_LEVEL) {
		secinfo->level = sec->level;
		dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
			"   .level = %d\n", sec->level);
	}
	if (sec->flags & SEC_ENABLED) {
		secinfo->enabled = sec->enabled;
		dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
			"   .enabled = %d\n", sec->enabled);
	}
	if (sec->flags & SEC_ENCRYPT) {
		secinfo->encrypt = sec->encrypt;
		dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
			"   .encrypt = %d\n", sec->encrypt);
	}
	if (sec->flags & SEC_AUTH_MODE) {
		secinfo->auth_mode = sec->auth_mode;
		dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
			"   .auth_mode = %d\n", sec->auth_mode);
	}
}

static void ieee_init(struct ieee80211_device *ieee)
{
	ieee->mode = IEEE_B | IEEE_G;
	ieee->freq_band = IEEE80211_24GHZ_BAND;
	ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
	ieee->tx_headroom = sizeof(struct zd_ctrlset);
	ieee->set_security = set_security;
	ieee->hard_start_xmit = netdev_tx;

	/* Software encryption/decryption for now */
	ieee->host_build_iv = 0;
	ieee->host_encrypt = 1;
	ieee->host_decrypt = 1;

	/* FIXME: default to managed mode, until ieee80211 and zd1211rw can
	 * correctly support AUTO */
	ieee->iw_mode = IW_MODE_INFRA;
}

static void softmac_init(struct ieee80211softmac_device *sm)
{
	sm->set_channel = set_channel;
1273
	sm->bssinfo_change = bssinfo_change;
1274 1275 1276 1277 1278 1279
}

struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
{
	struct zd_mac *mac = zd_netdev_mac(ndev);
	struct iw_statistics *iw_stats = &mac->iw_stats;
1280
	unsigned int i, count, qual_total, rssi_total;
1281 1282 1283 1284 1285 1286

	memset(iw_stats, 0, sizeof(struct iw_statistics));
	/* We are not setting the status, because ieee->state is not updated
	 * at all and this driver doesn't track authentication state.
	 */
	spin_lock_irq(&mac->lock);
1287 1288 1289 1290 1291 1292 1293
	count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
		mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
	qual_total = rssi_total = 0;
	for (i = 0; i < count; i++) {
		qual_total += mac->qual_buffer[i];
		rssi_total += mac->rssi_buffer[i];
	}
1294
	spin_unlock_irq(&mac->lock);
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
	iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
	if (count > 0) {
		iw_stats->qual.qual = qual_total / count;
		iw_stats->qual.level = rssi_total / count;
		iw_stats->qual.updated |=
			IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
	} else {
		iw_stats->qual.updated |=
			IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
	}
1305 1306 1307 1308
	/* TODO: update counter */
	return iw_stats;
}

1309 1310
#define LINK_LED_WORK_DELAY HZ

D
David Howells 已提交
1311
static void link_led_handler(struct work_struct *work)
1312
{
D
David Howells 已提交
1313 1314
	struct zd_mac *mac =
		container_of(work, struct zd_mac, housekeeping.link_led_work.work);
1315 1316 1317 1318 1319 1320
	struct zd_chip *chip = &mac->chip;
	struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
	int is_associated;
	int r;

	spin_lock_irq(&mac->lock);
1321
	is_associated = sm->associnfo.associated != 0;
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
	spin_unlock_irq(&mac->lock);

	r = zd_chip_control_leds(chip,
		                 is_associated ? LED_ASSOCIATED : LED_SCANNING);
	if (r)
		dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);

	queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
		           LINK_LED_WORK_DELAY);
}

static void housekeeping_init(struct zd_mac *mac)
{
D
David Howells 已提交
1335
	INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
}

static void housekeeping_enable(struct zd_mac *mac)
{
	dev_dbg_f(zd_mac_dev(mac), "\n");
	queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
			   0);
}

static void housekeeping_disable(struct zd_mac *mac)
{
	dev_dbg_f(zd_mac_dev(mac), "\n");
	cancel_rearming_delayed_workqueue(zd_workqueue,
		&mac->housekeeping.link_led_work);
	zd_chip_control_leds(&mac->chip, LED_OFF);
}