zd_mac.c 31.5 KB
Newer Older
1
/* ZD1211 USB-WLAN driver for Linux
Daniel Drake's avatar
Daniel Drake committed
2
 *
3 4 5
 * Copyright (C) 2005-2007 Ulrich Kunitz <kune@deine-taler.de>
 * Copyright (C) 2006-2007 Daniel Drake <dsd@gentoo.org>
 * Copyright (C) 2006-2007 Michael Wu <flamingice@sourmilk.net>
6
 * Copyright (C) 2007-2008 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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>
25
#include <linux/slab.h>
26 27 28 29 30 31 32 33 34
#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_rf.h"

35 36 37 38 39 40 41 42 43 44
struct zd_reg_alpha2_map {
	u32 reg;
	char alpha2[2];
};

static struct zd_reg_alpha2_map reg_alpha2_map[] = {
	{ ZD_REGDOMAIN_FCC, "US" },
	{ ZD_REGDOMAIN_IC, "CA" },
	{ ZD_REGDOMAIN_ETSI, "DE" }, /* Generic ETSI, use most restrictive */
	{ ZD_REGDOMAIN_JAPAN, "JP" },
45 46
	{ ZD_REGDOMAIN_JAPAN_2, "JP" },
	{ ZD_REGDOMAIN_JAPAN_3, "JP" },
47 48 49 50
	{ ZD_REGDOMAIN_SPAIN, "ES" },
	{ ZD_REGDOMAIN_FRANCE, "FR" },
};

Daniel Drake's avatar
Daniel Drake committed
51 52
/* This table contains the hardware specific values for the modulation rates. */
static const struct ieee80211_rate zd_rates[] = {
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	{ .bitrate = 10,
	  .hw_value = ZD_CCK_RATE_1M, },
	{ .bitrate = 20,
	  .hw_value = ZD_CCK_RATE_2M,
	  .hw_value_short = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
	{ .bitrate = 55,
	  .hw_value = ZD_CCK_RATE_5_5M,
	  .hw_value_short = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
	{ .bitrate = 110,
	  .hw_value = ZD_CCK_RATE_11M,
	  .hw_value_short = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
	{ .bitrate = 60,
	  .hw_value = ZD_OFDM_RATE_6M,
	  .flags = 0 },
	{ .bitrate = 90,
	  .hw_value = ZD_OFDM_RATE_9M,
	  .flags = 0 },
	{ .bitrate = 120,
	  .hw_value = ZD_OFDM_RATE_12M,
	  .flags = 0 },
	{ .bitrate = 180,
	  .hw_value = ZD_OFDM_RATE_18M,
	  .flags = 0 },
	{ .bitrate = 240,
	  .hw_value = ZD_OFDM_RATE_24M,
	  .flags = 0 },
	{ .bitrate = 360,
	  .hw_value = ZD_OFDM_RATE_36M,
	  .flags = 0 },
	{ .bitrate = 480,
	  .hw_value = ZD_OFDM_RATE_48M,
	  .flags = 0 },
	{ .bitrate = 540,
	  .hw_value = ZD_OFDM_RATE_54M,
	  .flags = 0 },
Daniel Drake's avatar
Daniel Drake committed
91 92
};

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
/*
 * Zydas retry rates table. Each line is listed in the same order as
 * in zd_rates[] and contains all the rate used when a packet is sent
 * starting with a given rates. Let's consider an example :
 *
 * "11 Mbits : 4, 3, 2, 1, 0" means :
 * - packet is sent using 4 different rates
 * - 1st rate is index 3 (ie 11 Mbits)
 * - 2nd rate is index 2 (ie 5.5 Mbits)
 * - 3rd rate is index 1 (ie 2 Mbits)
 * - 4th rate is index 0 (ie 1 Mbits)
 */

static const struct tx_retry_rate zd_retry_rates[] = {
	{ /*  1 Mbits */	1, { 0 }},
	{ /*  2 Mbits */	2, { 1,  0 }},
	{ /*  5.5 Mbits */	3, { 2,  1, 0 }},
	{ /* 11 Mbits */	4, { 3,  2, 1, 0 }},
	{ /*  6 Mbits */	5, { 4,  3, 2, 1, 0 }},
	{ /*  9 Mbits */	6, { 5,  4, 3, 2, 1, 0}},
	{ /* 12 Mbits */	5, { 6,  3, 2, 1, 0 }},
	{ /* 18 Mbits */	6, { 7,  6, 3, 2, 1, 0 }},
	{ /* 24 Mbits */	6, { 8,  6, 3, 2, 1, 0 }},
	{ /* 36 Mbits */	7, { 9,  8, 6, 3, 2, 1, 0 }},
	{ /* 48 Mbits */	8, {10,  9, 8, 6, 3, 2, 1, 0 }},
	{ /* 54 Mbits */	9, {11, 10, 9, 8, 6, 3, 2, 1, 0 }}
};

Daniel Drake's avatar
Daniel Drake committed
121
static const struct ieee80211_channel zd_channels[] = {
122 123 124 125 126 127 128 129 130 131 132 133 134 135
	{ .center_freq = 2412, .hw_value = 1 },
	{ .center_freq = 2417, .hw_value = 2 },
	{ .center_freq = 2422, .hw_value = 3 },
	{ .center_freq = 2427, .hw_value = 4 },
	{ .center_freq = 2432, .hw_value = 5 },
	{ .center_freq = 2437, .hw_value = 6 },
	{ .center_freq = 2442, .hw_value = 7 },
	{ .center_freq = 2447, .hw_value = 8 },
	{ .center_freq = 2452, .hw_value = 9 },
	{ .center_freq = 2457, .hw_value = 10 },
	{ .center_freq = 2462, .hw_value = 11 },
	{ .center_freq = 2467, .hw_value = 12 },
	{ .center_freq = 2472, .hw_value = 13 },
	{ .center_freq = 2484, .hw_value = 14 },
Daniel Drake's avatar
Daniel Drake committed
136
};
137

138 139 140 141
static void housekeeping_init(struct zd_mac *mac);
static void housekeeping_enable(struct zd_mac *mac);
static void housekeeping_disable(struct zd_mac *mac);

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
static int zd_reg2alpha2(u8 regdomain, char *alpha2)
{
	unsigned int i;
	struct zd_reg_alpha2_map *reg_map;
	for (i = 0; i < ARRAY_SIZE(reg_alpha2_map); i++) {
		reg_map = &reg_alpha2_map[i];
		if (regdomain == reg_map->reg) {
			alpha2[0] = reg_map->alpha2[0];
			alpha2[1] = reg_map->alpha2[1];
			return 0;
		}
	}
	return 1;
}

Daniel Drake's avatar
Daniel Drake committed
157
int zd_mac_preinit_hw(struct ieee80211_hw *hw)
158 159 160
{
	int r;
	u8 addr[ETH_ALEN];
Daniel Drake's avatar
Daniel Drake committed
161
	struct zd_mac *mac = zd_hw_mac(hw);
162 163 164 165 166

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

Daniel Drake's avatar
Daniel Drake committed
167 168
	SET_IEEE80211_PERM_ADDR(hw, addr);

169 170 171
	return 0;
}

Daniel Drake's avatar
Daniel Drake committed
172
int zd_mac_init_hw(struct ieee80211_hw *hw)
173 174
{
	int r;
Daniel Drake's avatar
Daniel Drake committed
175
	struct zd_mac *mac = zd_hw_mac(hw);
176
	struct zd_chip *chip = &mac->chip;
177
	char alpha2[2];
178 179 180 181 182
	u8 default_regdomain;

	r = zd_chip_enable_int(chip);
	if (r)
		goto out;
183
	r = zd_chip_init_hw(chip);
184 185 186 187 188 189 190 191 192 193 194 195
	if (r)
		goto disable_int;

	ZD_ASSERT(!irqs_disabled());

	r = zd_read_regdomain(chip, &default_regdomain);
	if (r)
		goto disable_int;
	spin_lock_irq(&mac->lock);
	mac->regdomain = mac->default_regdomain = default_regdomain;
	spin_unlock_irq(&mac->lock);

196 197 198
	/* We must inform the device that we are doing encryption/decryption in
	 * software at the moment. */
	r = zd_set_encryption_type(chip, ENC_SNIFFER);
199 200 201
	if (r)
		goto disable_int;

202
	r = zd_reg2alpha2(mac->regdomain, alpha2);
203 204
	if (r)
		goto disable_int;
205

206
	r = regulatory_hint(hw->wiphy, alpha2);
207 208 209 210 211 212 213 214
disable_int:
	zd_chip_disable_int(chip);
out:
	return r;
}

void zd_mac_clear(struct zd_mac *mac)
{
215
	flush_workqueue(zd_workqueue);
216
	zd_chip_clear(&mac->chip);
Ulrich Kunitz's avatar
Ulrich Kunitz committed
217 218
	ZD_ASSERT(!spin_is_locked(&mac->lock));
	ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
219 220
}

221
static int set_rx_filter(struct zd_mac *mac)
222
{
Daniel Drake's avatar
Daniel Drake committed
223 224
	unsigned long flags;
	u32 filter = STA_RX_FILTER;
225

Daniel Drake's avatar
Daniel Drake committed
226 227 228 229 230 231
	spin_lock_irqsave(&mac->lock, flags);
	if (mac->pass_ctrl)
		filter |= RX_FILTER_CTRL;
	spin_unlock_irqrestore(&mac->lock, flags);

	return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
232 233
}

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
static int set_mac_and_bssid(struct zd_mac *mac)
{
	int r;

	if (!mac->vif)
		return -1;

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

	/* Vendor driver after setting MAC either sets BSSID for AP or
	 * filter for other modes.
	 */
	if (mac->type != NL80211_IFTYPE_AP)
		return set_rx_filter(mac);
	else
		return zd_write_bssid(&mac->chip, mac->vif->addr);
}

254 255 256 257 258 259 260
static int set_mc_hash(struct zd_mac *mac)
{
	struct zd_mc_hash hash;
	zd_mc_clear(&hash);
	return zd_chip_set_multicast_hash(&mac->chip, &hash);
}

Daniel Drake's avatar
Daniel Drake committed
261
static int zd_op_start(struct ieee80211_hw *hw)
262
{
Daniel Drake's avatar
Daniel Drake committed
263
	struct zd_mac *mac = zd_hw_mac(hw);
264
	struct zd_chip *chip = &mac->chip;
265
	struct zd_usb *usb = &chip->usb;
266 267
	int r;

268 269 270 271 272 273
	if (!usb->initialized) {
		r = zd_usb_init_hw(usb);
		if (r)
			goto out;
	}

274 275 276 277 278 279 280
	r = zd_chip_enable_int(chip);
	if (r < 0)
		goto out;

	r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
	if (r < 0)
		goto disable_int;
281 282 283 284
	r = set_rx_filter(mac);
	if (r)
		goto disable_int;
	r = set_mc_hash(mac);
285 286 287 288 289
	if (r)
		goto disable_int;
	r = zd_chip_switch_radio_on(chip);
	if (r < 0)
		goto disable_int;
Daniel Drake's avatar
Daniel Drake committed
290
	r = zd_chip_enable_rxtx(chip);
291 292 293 294
	if (r < 0)
		goto disable_radio;
	r = zd_chip_enable_hwint(chip);
	if (r < 0)
Daniel Drake's avatar
Daniel Drake committed
295
		goto disable_rxtx;
296

297
	housekeeping_enable(mac);
298
	return 0;
Daniel Drake's avatar
Daniel Drake committed
299 300
disable_rxtx:
	zd_chip_disable_rxtx(chip);
301 302 303 304 305 306 307 308
disable_radio:
	zd_chip_switch_radio_off(chip);
disable_int:
	zd_chip_disable_int(chip);
out:
	return r;
}

Daniel Drake's avatar
Daniel Drake committed
309 310 311 312 313 314
static void zd_op_stop(struct ieee80211_hw *hw)
{
	struct zd_mac *mac = zd_hw_mac(hw);
	struct zd_chip *chip = &mac->chip;
	struct sk_buff *skb;
	struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
315

Daniel Drake's avatar
Daniel Drake committed
316
	/* The order here deliberately is a little different from the open()
317
	 * method, since we need to make sure there is no opportunity for RX
Daniel Drake's avatar
Daniel Drake committed
318
	 * frames to be processed by mac80211 after we have stopped it.
319 320
	 */

Daniel Drake's avatar
Daniel Drake committed
321
	zd_chip_disable_rxtx(chip);
322
	housekeeping_disable(mac);
323 324
	flush_workqueue(zd_workqueue);

325 326 327 328 329
	zd_chip_disable_hwint(chip);
	zd_chip_switch_radio_off(chip);
	zd_chip_disable_int(chip);


Daniel Drake's avatar
Daniel Drake committed
330
	while ((skb = skb_dequeue(ack_wait_queue)))
331
		dev_kfree_skb_any(skb);
332 333
}

Daniel Drake's avatar
Daniel Drake committed
334
/**
335
 * zd_mac_tx_status - reports tx status of a packet if required
Daniel Drake's avatar
Daniel Drake committed
336 337
 * @hw - a &struct ieee80211_hw pointer
 * @skb - a sk-buffer
338 339
 * @flags: extra flags to set in the TX status info
 * @ackssi: ACK signal strength
Coly Li's avatar
Coly Li committed
340
 * @success - True for successful transmission of the frame
Daniel Drake's avatar
Daniel Drake committed
341 342 343 344 345 346 347
 *
 * This information calls ieee80211_tx_status_irqsafe() if required by the
 * control information. It copies the control information into the status
 * information.
 *
 * If no status information has been requested, the skb is freed.
 */
348 349
static void zd_mac_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
		      int ackssi, struct tx_status *tx_status)
350
{
351
	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
352 353 354 355
	int i;
	int success = 1, retry = 1;
	int first_idx;
	const struct tx_retry_rate *retries;
356

357
	ieee80211_tx_info_clear_status(info);
358

359 360 361 362 363 364 365
	if (tx_status) {
		success = !tx_status->failure;
		retry = tx_status->retry + success;
	}

	if (success) {
		/* success */
366
		info->flags |= IEEE80211_TX_STAT_ACK;
367 368 369 370 371 372 373 374
	} else {
		/* failure */
		info->flags &= ~IEEE80211_TX_STAT_ACK;
	}

	first_idx = info->status.rates[0].idx;
	ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
	retries = &zd_retry_rates[first_idx];
375
	ZD_ASSERT(1 <= retry && retry <= retries->count);
376 377 378 379 380 381 382 383 384

	info->status.rates[0].idx = retries->rate[0];
	info->status.rates[0].count = 1; // (retry > 1 ? 2 : 1);

	for (i=1; i<IEEE80211_TX_MAX_RATES-1 && i<retry; i++) {
		info->status.rates[i].idx = retries->rate[i];
		info->status.rates[i].count = 1; // ((i==retry-1) && success ? 1:2);
	}
	for (; i<IEEE80211_TX_MAX_RATES && i<retry; i++) {
385
		info->status.rates[i].idx = retries->rate[retry - 1];
386 387 388 389 390
		info->status.rates[i].count = 1; // (success ? 1:2);
	}
	if (i<IEEE80211_TX_MAX_RATES)
		info->status.rates[i].idx = -1; /* terminate */

391 392
	info->status.ack_signal = ackssi;
	ieee80211_tx_status_irqsafe(hw, skb);
393 394
}

Daniel Drake's avatar
Daniel Drake committed
395 396 397 398
/**
 * zd_mac_tx_failed - callback for failed frames
 * @dev: the mac80211 wireless device
 *
399
 * This function is called if a frame couldn't be successfully
Daniel Drake's avatar
Daniel Drake committed
400 401 402
 * transferred. The first frame from the tx queue, will be selected and
 * reported as error to the upper layers.
 */
403
void zd_mac_tx_failed(struct urb *urb)
404
{
405 406 407
	struct ieee80211_hw * hw = zd_usb_to_hw(urb->context);
	struct zd_mac *mac = zd_hw_mac(hw);
	struct sk_buff_head *q = &mac->ack_wait_queue;
Daniel Drake's avatar
Daniel Drake committed
408
	struct sk_buff *skb;
409 410 411 412 413 414
	struct tx_status *tx_status = (struct tx_status *)urb->transfer_buffer;
	unsigned long flags;
	int success = !tx_status->failure;
	int retry = tx_status->retry + success;
	int found = 0;
	int i, position = 0;
415

416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
	q = &mac->ack_wait_queue;
	spin_lock_irqsave(&q->lock, flags);

	skb_queue_walk(q, skb) {
		struct ieee80211_hdr *tx_hdr;
		struct ieee80211_tx_info *info;
		int first_idx, final_idx;
		const struct tx_retry_rate *retries;
		u8 final_rate;

		position ++;

		/* if the hardware reports a failure and we had a 802.11 ACK
		 * pending, then we skip the first skb when searching for a
		 * matching frame */
		if (tx_status->failure && mac->ack_pending &&
		    skb_queue_is_first(q, skb)) {
			continue;
		}

		tx_hdr = (struct ieee80211_hdr *)skb->data;

		/* we skip all frames not matching the reported destination */
		if (unlikely(memcmp(tx_hdr->addr1, tx_status->mac, ETH_ALEN))) {
			continue;
		}

		/* we skip all frames not matching the reported final rate */
444

445 446 447 448
		info = IEEE80211_SKB_CB(skb);
		first_idx = info->status.rates[0].idx;
		ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
		retries = &zd_retry_rates[first_idx];
449
		if (retry <= 0 || retry > retries->count)
450 451
			continue;

452
		final_idx = retries->rate[retry - 1];
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
		final_rate = zd_rates[final_idx].hw_value;

		if (final_rate != tx_status->rate) {
			continue;
		}

		found = 1;
		break;
	}

	if (found) {
		for (i=1; i<=position; i++) {
			skb = __skb_dequeue(q);
			zd_mac_tx_status(hw, skb,
					 mac->ack_pending ? mac->ack_signal : 0,
					 i == position ? tx_status : NULL);
			mac->ack_pending = 0;
		}
	}

	spin_unlock_irqrestore(&q->lock, flags);
474 475
}

Daniel Drake's avatar
Daniel Drake committed
476 477 478 479 480 481 482 483 484 485 486 487
/**
 * zd_mac_tx_to_dev - callback for USB layer
 * @skb: a &sk_buff pointer
 * @error: error value, 0 if transmission successful
 *
 * Informs the MAC layer that the frame has successfully transferred to the
 * device. If an ACK is required and the transfer to the device has been
 * successful, the packets are put on the @ack_wait_queue with
 * the control set removed.
 */
void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
{
488
	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
489
	struct ieee80211_hw *hw = info->rate_driver_data[0];
490 491 492
	struct zd_mac *mac = zd_hw_mac(hw);

	ieee80211_tx_info_clear_status(info);
493

494 495 496
	skb_pull(skb, sizeof(struct zd_ctrlset));
	if (unlikely(error ||
	    (info->flags & IEEE80211_TX_CTL_NO_ACK))) {
497 498 499 500
		/*
		 * FIXME : do we need to fill in anything ?
		 */
		ieee80211_tx_status_irqsafe(hw, skb);
Daniel Drake's avatar
Daniel Drake committed
501
	} else {
502
		struct sk_buff_head *q = &mac->ack_wait_queue;
503 504

		skb_queue_tail(q, skb);
505 506 507 508 509 510
		while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS) {
			zd_mac_tx_status(hw, skb_dequeue(q),
					 mac->ack_pending ? mac->ack_signal : 0,
					 NULL);
			mac->ack_pending = 0;
		}
511 512 513
	}
}

514
static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
515
{
516
	/* ZD_PURE_RATE() must be used to remove the modulation type flag of
Daniel Drake's avatar
Daniel Drake committed
517 518
	 * the zd-rate values.
	 */
519
	static const u8 rate_divisor[] = {
Daniel Drake's avatar
Daniel Drake committed
520 521 522 523 524 525 526 527 528 529 530 531 532
		[ZD_PURE_RATE(ZD_CCK_RATE_1M)]   =  1,
		[ZD_PURE_RATE(ZD_CCK_RATE_2M)]	 =  2,
		/* Bits must be doubled. */
		[ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
		[ZD_PURE_RATE(ZD_CCK_RATE_11M)]	 = 11,
		[ZD_PURE_RATE(ZD_OFDM_RATE_6M)]  =  6,
		[ZD_PURE_RATE(ZD_OFDM_RATE_9M)]  =  9,
		[ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
		[ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
		[ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
		[ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
		[ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
		[ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
533 534 535 536 537
	};

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

538
	divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
539 540 541
	if (divisor == 0)
		return -EINVAL;

542 543
	switch (zd_rate) {
	case ZD_CCK_RATE_5_5M:
544 545
		bits = (2*bits) + 10; /* round up to the next integer */
		break;
546
	case ZD_CCK_RATE_11M:
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
		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;
}

static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
562 563
	                   struct ieee80211_hdr *header,
	                   struct ieee80211_tx_info *info)
564 565
{
	/*
566
	 * CONTROL TODO:
567 568 569 570 571 572 573
	 * - if backoff needed, enable bit 0
	 * - if burst (backoff not needed) disable bit 0
	 */

	cs->control = 0;

	/* First fragment */
574
	if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
575 576
		cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;

577 578 579
	/* No ACK expected (multicast, etc.) */
	if (info->flags & IEEE80211_TX_CTL_NO_ACK)
		cs->control |= ZD_CS_NO_ACK;
580 581

	/* PS-POLL */
582
	if (ieee80211_is_pspoll(header->frame_control))
583 584
		cs->control |= ZD_CS_PS_POLL_FRAME;

585
	if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
586 587
		cs->control |= ZD_CS_RTS;

588
	if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
589
		cs->control |= ZD_CS_SELF_CTS;
590 591 592 593

	/* FIXME: Management frame? */
}

594
static int zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon)
595 596
{
	struct zd_mac *mac = zd_hw_mac(hw);
597
	int r;
598 599 600
	u32 tmp, j = 0;
	/* 4 more bytes for tail CRC */
	u32 full_len = beacon->len + 4;
601 602 603 604 605 606 607 608

	r = zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 0);
	if (r < 0)
		return r;
	r = zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
	if (r < 0)
		return r;

609
	while (tmp & 0x2) {
610 611 612
		r = zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
		if (r < 0)
			return r;
613 614 615 616
		if ((++j % 100) == 0) {
			printk(KERN_ERR "CR_BCN_FIFO_SEMAPHORE not ready\n");
			if (j >= 500)  {
				printk(KERN_ERR "Giving up beacon config.\n");
617
				return -ETIMEDOUT;
618 619 620 621 622
			}
		}
		msleep(1);
	}

623 624 625 626 627 628 629 630
	r = zd_iowrite32(&mac->chip, CR_BCN_FIFO, full_len - 1);
	if (r < 0)
		return r;
	if (zd_chip_is_zd1211b(&mac->chip)) {
		r = zd_iowrite32(&mac->chip, CR_BCN_LENGTH, full_len - 1);
		if (r < 0)
			return r;
	}
631

632 633
	for (j = 0 ; j < beacon->len; j++) {
		r = zd_iowrite32(&mac->chip, CR_BCN_FIFO,
634
				*((u8 *)(beacon->data + j)));
635 636 637
		if (r < 0)
			return r;
	}
638

639 640 641 642 643 644 645 646 647
	for (j = 0; j < 4; j++) {
		r = zd_iowrite32(&mac->chip, CR_BCN_FIFO, 0x0);
		if (r < 0)
			return r;
	}

	r = zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 1);
	if (r < 0)
		return r;
648 649 650 651 652

	/* 802.11b/g 2.4G CCK 1Mb
	 * 802.11a, not yet implemented, uses different values (see GPL vendor
	 * driver)
	 */
653
	return zd_iowrite32(&mac->chip, CR_BCN_PLCP_CFG, 0x00000400 |
654 655 656
			(full_len << 19));
}

657
static int fill_ctrlset(struct zd_mac *mac,
658
			struct sk_buff *skb)
659 660
{
	int r;
Daniel Drake's avatar
Daniel Drake committed
661 662
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
	unsigned int frag_len = skb->len + FCS_LEN;
663
	unsigned int packet_length;
664
	struct ieee80211_rate *txrate;
665 666
	struct zd_ctrlset *cs = (struct zd_ctrlset *)
		skb_push(skb, sizeof(struct zd_ctrlset));
667
	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
668 669 670

	ZD_ASSERT(frag_len <= 0xffff);

671
	txrate = ieee80211_get_tx_rate(mac->hw, info);
672 673

	cs->modulation = txrate->hw_value;
674
	if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
675
		cs->modulation = txrate->hw_value_short;
676 677 678

	cs->tx_length = cpu_to_le16(frag_len);

679
	cs_set_control(mac, cs, hdr, info);
680 681 682 683 684 685

	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.
	 */
686
	cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
			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;
703
	r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
704 705 706 707
		                 le16_to_cpu(cs->tx_length));
	if (r < 0)
		return r;
	cs->current_length = cpu_to_le16(r);
Daniel Drake's avatar
Daniel Drake committed
708
	cs->next_frame_length = 0;
709 710 711 712

	return 0;
}

Daniel Drake's avatar
Daniel Drake committed
713 714 715 716 717 718 719 720 721 722 723
/**
 * zd_op_tx - transmits a network frame to the device
 *
 * @dev: mac80211 hardware device
 * @skb: socket buffer
 * @control: the control structure
 *
 * This function transmit an IEEE 802.11 network frame to the device. The
 * control block of the skbuff will be initialized. If necessary the incoming
 * mac80211 queues will be stopped.
 */
724
static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
725
{
Daniel Drake's avatar
Daniel Drake committed
726
	struct zd_mac *mac = zd_hw_mac(hw);
727
	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Daniel Drake's avatar
Daniel Drake committed
728
	int r;
729

730
	r = fill_ctrlset(mac, skb);
Daniel Drake's avatar
Daniel Drake committed
731
	if (r)
732
		goto fail;
733

734
	info->rate_driver_data[0] = hw;
735

Daniel Drake's avatar
Daniel Drake committed
736
	r = zd_usb_tx(&mac->chip.usb, skb);
737
	if (r)
738 739 740 741 742
		goto fail;
	return 0;

fail:
	dev_kfree_skb(skb);
743 744 745
	return 0;
}

Daniel Drake's avatar
Daniel Drake committed
746 747 748 749 750
/**
 * filter_ack - filters incoming packets for acknowledgements
 * @dev: the mac80211 device
 * @rx_hdr: received header
 * @stats: the status for the received packet
Ulrich Kunitz's avatar
Ulrich Kunitz committed
751
 *
Daniel Drake's avatar
Daniel Drake committed
752 753 754 755 756
 * This functions looks for ACK packets and tries to match them with the
 * frames in the tx queue. If a match is found the frame will be dequeued and
 * the upper layers is informed about the successful transmission. If
 * mac80211 queues have been stopped and the number of frames still to be
 * transmitted is low the queues will be opened again.
757
 *
Daniel Drake's avatar
Daniel Drake committed
758
 * Returns 1 if the frame was an ACK, 0 if it was ignored.
759
 */
Daniel Drake's avatar
Daniel Drake committed
760 761
static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
		      struct ieee80211_rx_status *stats)
762
{
763
	struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drake's avatar
Daniel Drake committed
764 765 766
	struct sk_buff *skb;
	struct sk_buff_head *q;
	unsigned long flags;
767 768
	int found = 0;
	int i, position = 0;
769

770
	if (!ieee80211_is_ack(rx_hdr->frame_control))
771 772
		return 0;

773
	q = &mac->ack_wait_queue;
Daniel Drake's avatar
Daniel Drake committed
774
	spin_lock_irqsave(&q->lock, flags);
775
	skb_queue_walk(q, skb) {
Daniel Drake's avatar
Daniel Drake committed
776 777
		struct ieee80211_hdr *tx_hdr;

778 779 780 781 782
		position ++;

		if (mac->ack_pending && skb_queue_is_first(q, skb))
		    continue;

Daniel Drake's avatar
Daniel Drake committed
783
		tx_hdr = (struct ieee80211_hdr *)skb->data;
784
		if (likely(!memcmp(tx_hdr->addr2, rx_hdr->addr1, ETH_ALEN)))
Daniel Drake's avatar
Daniel Drake committed
785
		{
786 787
			found = 1;
			break;
Daniel Drake's avatar
Daniel Drake committed
788 789
		}
	}
790 791 792 793 794 795 796 797 798 799 800 801 802 803

	if (found) {
		for (i=1; i<position; i++) {
			skb = __skb_dequeue(q);
			zd_mac_tx_status(hw, skb,
					 mac->ack_pending ? mac->ack_signal : 0,
					 NULL);
			mac->ack_pending = 0;
		}

		mac->ack_pending = 1;
		mac->ack_signal = stats->signal;
	}

Daniel Drake's avatar
Daniel Drake committed
804 805
	spin_unlock_irqrestore(&q->lock, flags);
	return 1;
806 807
}

Daniel Drake's avatar
Daniel Drake committed
808
int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
809
{
Daniel Drake's avatar
Daniel Drake committed
810 811 812 813 814
	struct zd_mac *mac = zd_hw_mac(hw);
	struct ieee80211_rx_status stats;
	const struct rx_status *status;
	struct sk_buff *skb;
	int bad_frame = 0;
815 816
	__le16 fc;
	int need_padding;
817 818
	int i;
	u8 rate;
819

Daniel Drake's avatar
Daniel Drake committed
820 821 822
	if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
	             FCS_LEN + sizeof(struct rx_status))
		return -EINVAL;
823

Daniel Drake's avatar
Daniel Drake committed
824
	memset(&stats, 0, sizeof(stats));
825

Daniel Drake's avatar
Daniel Drake committed
826 827 828 829 830 831 832 833
	/* Note about pass_failed_fcs and pass_ctrl access below:
	 * mac locking intentionally omitted here, as this is the only unlocked
	 * reader and the only writer is configure_filter. Plus, if there were
	 * any races accessing these variables, it wouldn't really matter.
	 * If mac80211 ever provides a way for us to access filter flags
	 * from outside configure_filter, we could improve on this. Also, this
	 * situation may change once we implement some kind of DMA-into-skb
	 * RX path. */
834

Daniel Drake's avatar
Daniel Drake committed
835 836
	/* Caller has to ensure that length >= sizeof(struct rx_status). */
	status = (struct rx_status *)
837
		(buffer + (length - sizeof(struct rx_status)));
838
	if (status->frame_status & ZD_RX_ERROR) {
Daniel Drake's avatar
Daniel Drake committed
839 840 841 842 843 844
		if (mac->pass_failed_fcs &&
				(status->frame_status & ZD_RX_CRC32_ERROR)) {
			stats.flag |= RX_FLAG_FAILED_FCS_CRC;
			bad_frame = 1;
		} else {
			return -EINVAL;
845
		}
846
	}
847

848 849
	stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
	stats.band = IEEE80211_BAND_2GHZ;
850
	stats.signal = status->signal_strength;
851 852 853 854 855 856 857

	rate = zd_rx_rate(buffer, status);

	/* todo: return index in the big switches in zd_rx_rate instead */
	for (i = 0; i < mac->band.n_bitrates; i++)
		if (rate == mac->band.bitrates[i].hw_value)
			stats.rate_idx = i;
Daniel Drake's avatar
Daniel Drake committed
858 859 860 861 862 863 864 865 866 867 868 869 870

	length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
	buffer += ZD_PLCP_HEADER_SIZE;

	/* Except for bad frames, filter each frame to see if it is an ACK, in
	 * which case our internal TX tracking is updated. Normally we then
	 * bail here as there's no need to pass ACKs on up to the stack, but
	 * there is also the case where the stack has requested us to pass
	 * control frames on up (pass_ctrl) which we must consider. */
	if (!bad_frame &&
			filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
			&& !mac->pass_ctrl)
		return 0;
871

872
	fc = get_unaligned((__le16*)buffer);
873
	need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
874 875

	skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
Daniel Drake's avatar
Daniel Drake committed
876 877
	if (skb == NULL)
		return -ENOMEM;
878
	if (need_padding) {
879
		/* Make sure the payload data is 4 byte aligned. */
880 881 882
		skb_reserve(skb, 2);
	}

883
	/* FIXME : could we avoid this big memcpy ? */
Daniel Drake's avatar
Daniel Drake committed
884 885
	memcpy(skb_put(skb, length), buffer, length);

886 887
	memcpy(IEEE80211_SKB_RXCB(skb), &stats, sizeof(stats));
	ieee80211_rx_irqsafe(hw, skb);
888 889 890
	return 0;
}

Daniel Drake's avatar
Daniel Drake committed
891
static int zd_op_add_interface(struct ieee80211_hw *hw,
892
				struct ieee80211_vif *vif)
893
{
Daniel Drake's avatar
Daniel Drake committed
894
	struct zd_mac *mac = zd_hw_mac(hw);
895

896 897
	/* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */
	if (mac->type != NL80211_IFTYPE_UNSPECIFIED)
Daniel Drake's avatar
Daniel Drake committed
898
		return -EOPNOTSUPP;
899

900
	switch (vif->type) {
901 902 903 904
	case NL80211_IFTYPE_MONITOR:
	case NL80211_IFTYPE_MESH_POINT:
	case NL80211_IFTYPE_STATION:
	case NL80211_IFTYPE_ADHOC:
905
		mac->type = vif->type;
Daniel Drake's avatar
Daniel Drake committed
906 907 908
		break;
	default:
		return -EOPNOTSUPP;
909
	}
910

911 912 913
	mac->vif = vif;

	return set_mac_and_bssid(mac);
Daniel Drake's avatar
Daniel Drake committed
914
}
915

Daniel Drake's avatar
Daniel Drake committed
916
static void zd_op_remove_interface(struct ieee80211_hw *hw,
917
				    struct ieee80211_vif *vif)
Daniel Drake's avatar
Daniel Drake committed
918 919
{
	struct zd_mac *mac = zd_hw_mac(hw);
920
	mac->type = NL80211_IFTYPE_UNSPECIFIED;
921
	mac->vif = NULL;
922
	zd_set_beacon_interval(&mac->chip, 0);
Daniel Drake's avatar
Daniel Drake committed
923 924
	zd_write_mac_addr(&mac->chip, NULL);
}
925

926
static int zd_op_config(struct ieee80211_hw *hw, u32 changed)
Daniel Drake's avatar
Daniel Drake committed
927 928
{
	struct zd_mac *mac = zd_hw_mac(hw);
929 930
	struct ieee80211_conf *conf = &hw->conf;

931
	return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
Daniel Drake's avatar
Daniel Drake committed
932
}
933

934
static void zd_process_intr(struct work_struct *work)
935 936
{
	u16 int_status;
937
	unsigned long flags;
938 939
	struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);

940 941 942 943
	spin_lock_irqsave(&mac->lock, flags);
	int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer + 4));
	spin_unlock_irqrestore(&mac->lock, flags);

944 945 946
	if (int_status & INT_CFG_NEXT_BCN)
		dev_dbg_f_limit(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");
	else
947 948 949 950 951 952
		dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");

	zd_chip_enable_hwint(&mac->chip);
}


953
static u64 zd_op_prepare_multicast(struct ieee80211_hw *hw,
954
				   struct netdev_hw_addr_list *mc_list)
955 956 957
{
	struct zd_mac *mac = zd_hw_mac(hw);
	struct zd_mc_hash hash;
958
	struct netdev_hw_addr *ha;
959 960 961

	zd_mc_clear(&hash);

962 963 964
	netdev_hw_addr_list_for_each(ha, mc_list) {
		dev_dbg_f(zd_mac_dev(mac), "mc addr %pM\n", ha->addr);
		zd_mc_add_addr(&hash, ha->addr);
965 966 967 968 969
	}

	return hash.low | ((u64)hash.high << 32);
}

Daniel Drake's avatar
Daniel Drake committed
970 971
#define SUPPORTED_FIF_FLAGS \
	(FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
972
	FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
Daniel Drake's avatar
Daniel Drake committed
973 974 975
static void zd_op_configure_filter(struct ieee80211_hw *hw,
			unsigned int changed_flags,
			unsigned int *new_flags,
976
			u64 multicast)
977
{
978 979 980 981
	struct zd_mc_hash hash = {
		.low = multicast,
		.high = multicast >> 32,
	};
Daniel Drake's avatar
Daniel Drake committed
982 983
	struct zd_mac *mac = zd_hw_mac(hw);
	unsigned long flags;
984
	int r;
985

Daniel Drake's avatar
Daniel Drake committed
986 987 988 989
	/* Only deal with supported flags */
	changed_flags &= SUPPORTED_FIF_FLAGS;
	*new_flags &= SUPPORTED_FIF_FLAGS;

990 991 992 993 994 995 996
	/*
	 * If multicast parameter (as returned by zd_op_prepare_multicast)
	 * has changed, no bit in changed_flags is set. To handle this
	 * situation, we do not return if changed_flags is 0. If we do so,
	 * we will have some issue with IPv6 which uses multicast for link
	 * layer address resolution.
	 */
997
	if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI))
Daniel Drake's avatar
Daniel Drake committed
998 999 1000 1001 1002 1003 1004
		zd_mc_add_all(&hash);

	spin_lock_irqsave(&mac->lock, flags);
	mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
	mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
	mac->multicast_hash = hash;
	spin_unlock_irqrestore(&mac->lock, flags);
1005

1006
	zd_chip_set_multicast_hash(&mac->chip, &hash);
Daniel Drake's avatar
Daniel Drake committed
1007

1008 1009 1010 1011 1012
	if (changed_flags & FIF_CONTROL) {
		r = set_rx_filter(mac);
		if (r)
			dev_err(zd_mac_dev(mac), "set_rx_filter error %d\n", r);
	}
Daniel Drake's avatar
Daniel Drake committed
1013 1014 1015 1016 1017 1018 1019 1020 1021

	/* no handling required for FIF_OTHER_BSS as we don't currently
	 * do BSSID filtering */
	/* FIXME: in future it would be nice to enable the probe response
	 * filter (so that the driver doesn't see them) until
	 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
	 * have to schedule work to enable prbresp reception, which might
	 * happen too late. For now we'll just listen and forward them all the
	 * time. */
1022 1023
}

1024
static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble)
1025
{
Daniel Drake's avatar
Daniel Drake committed
1026 1027 1028
	mutex_lock(&mac->chip.mutex);
	zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
	mutex_unlock(&mac->chip.mutex);
1029 1030
}

1031 1032 1033 1034
static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
				   struct ieee80211_vif *vif,
				   struct ieee80211_bss_conf *bss_conf,
				   u32 changes)
1035
{
Daniel Drake's avatar
Daniel Drake committed
1036
	struct zd_mac *mac = zd_hw_mac(hw);
1037
	int associated;
Daniel Drake's avatar
Daniel Drake committed
1038 1039 1040

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

1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
	if (mac->type == NL80211_IFTYPE_MESH_POINT ||
	    mac->type == NL80211_IFTYPE_ADHOC) {
		associated = true;
		if (changes & BSS_CHANGED_BEACON) {
			struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);

			if (beacon) {
				zd_mac_config_beacon(hw, beacon);
				kfree_skb(beacon);
			}
		}

		if (changes & BSS_CHANGED_BEACON_ENABLED) {
			u32 interval;

			if (bss_conf->enable_beacon)
				interval = BCN_MODE_IBSS |
						bss_conf->beacon_int;
			else
				interval = 0;

			zd_set_beacon_interval(&mac->chip, interval);
		}
	} else
		associated = is_valid_ether_addr(bss_conf->bssid);

	spin_lock_irq(&mac->lock);
	mac->associated = associated;
	spin_unlock_irq(&mac->lock);

	/* TODO: do hardware bssid filtering */

1073
	if (changes & BSS_CHANGED_ERP_PREAMBLE) {
1074
		spin_lock_irq(&mac->lock);
1075
		mac->short_preamble = bss_conf->use_short_preamble;
1076 1077 1078
		spin_unlock_irq(&mac->lock);

		set_rts_cts(mac, bss_conf->use_short_preamble);
Daniel Drake's avatar
Daniel Drake committed
1079
	}
1080 1081
}

1082 1083 1084 1085 1086 1087
static u64 zd_op_get_tsf(struct ieee80211_hw *hw)
{
	struct zd_mac *mac = zd_hw_mac(hw);
	return zd_chip_get_tsf(&mac->chip);
}

Daniel Drake's avatar
Daniel Drake committed
1088 1089 1090 1091 1092 1093 1094
static const struct ieee80211_ops zd_ops = {
	.tx			= zd_op_tx,
	.start			= zd_op_start,
	.stop			= zd_op_stop,
	.add_interface		= zd_op_add_interface,
	.remove_interface	= zd_op_remove_interface,
	.config			= zd_op_config,
1095
	.prepare_multicast	= zd_op_prepare_multicast,
Daniel Drake's avatar
Daniel Drake committed
1096
	.configure_filter	= zd_op_configure_filter,
1097
	.bss_info_changed	= zd_op_bss_info_changed,
1098
	.get_tsf		= zd_op_get_tsf,
Daniel Drake's avatar
Daniel Drake committed
1099 1100 1101
};

struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
1102
{
Daniel Drake's avatar
Daniel Drake committed
1103 1104
	struct zd_mac *mac;
	struct ieee80211_hw *hw;
1105

Daniel Drake's avatar
Daniel Drake committed
1106 1107 1108 1109
	hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
	if (!hw) {
		dev_dbg_f(&intf->dev, "out of memory\n");
		return NULL;
1110
	}
Daniel Drake's avatar
Daniel Drake committed
1111 1112 1113 1114 1115 1116 1117

	mac = zd_hw_mac(hw);

	memset(mac, 0, sizeof(*mac));
	spin_lock_init(&mac->lock);
	mac->hw = hw;

1118
	mac->type = NL80211_IFTYPE_UNSPECIFIED;
Daniel Drake's avatar
Daniel Drake committed
1119 1120 1121

	memcpy(mac->channels, zd_channels, sizeof(zd_channels));
	memcpy(mac->rates, zd_rates, sizeof(zd_rates));
1122 1123 1124 1125 1126 1127 1128
	mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
	mac->band.bitrates = mac->rates;
	mac->band.n_channels = ARRAY_SIZE(zd_channels);
	mac->band.channels = mac->channels;

	hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;

1129
	hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
1130
		    IEEE80211_HW_SIGNAL_UNSPEC;
Daniel Drake's avatar
Daniel Drake committed
1131

1132 1133 1134 1135 1136
	hw->wiphy->interface_modes =
		BIT(NL80211_IFTYPE_MESH_POINT) |
		BIT(NL80211_IFTYPE_STATION) |
		BIT(NL80211_IFTYPE_ADHOC);

1137
	hw->max_signal = 100;
Daniel Drake's avatar
Daniel Drake committed
1138 1139 1140
	hw->queues = 1;
	hw->extra_tx_headroom = sizeof(struct zd_ctrlset);

1141 1142 1143 1144 1145 1146
	/*
	 * Tell mac80211 that we support multi rate retries
	 */
	hw->max_rates = IEEE80211_TX_MAX_RATES;
	hw->max_rate_tries = 18;	/* 9 rates * 2 retries/rate */

Daniel Drake's avatar
Daniel Drake committed
1147
	skb_queue_head_init(&mac->ack_wait_queue);
1148
	mac->ack_pending = 0;
Daniel Drake's avatar
Daniel Drake committed
1149 1150 1151

	zd_chip_init(&mac->chip, hw, intf);
	housekeeping_init(mac);
1152
	INIT_WORK(&mac->process_intr, zd_process_intr);
Daniel Drake's avatar
Daniel Drake committed
1153 1154 1155

	SET_IEEE80211_DEV(hw, &intf->dev);
	return hw;
1156 1157
}

1158 1159
#define LINK_LED_WORK_DELAY HZ

1160
static void link_led_handler(struct work_struct *work)
1161
{
1162 1163
	struct zd_mac *mac =
		container_of(work, struct zd_mac, housekeeping.link_led_work.work);
1164 1165 1166 1167 1168
	struct zd_chip *chip = &mac->chip;
	int is_associated;
	int r;

	spin_lock_irq(&mac->lock);
Daniel Drake's avatar
Daniel Drake committed
1169
	is_associated = mac->associated;
1170 1171 1172
	spin_unlock_irq(&mac->lock);

	r = zd_chip_control_leds(chip,
1173
		                 is_associated ? ZD_LED_ASSOCIATED : ZD_LED_SCANNING);
1174
	if (r)
Daniel Drake's avatar
Daniel Drake committed
1175
		dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1176 1177 1178 1179 1180 1181 1182

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

static void housekeeping_init(struct zd_mac *mac)
{
1183
	INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
}

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");
1196
	cancel_delayed_work_sync(&mac->housekeeping.link_led_work);
1197
	zd_chip_control_leds(&mac->chip, ZD_LED_OFF);
1198
}