zd1211rw: lower hw command timeouts
[linux-block.git] / drivers / net / wireless / zd1211rw / zd_mac.c
CommitLineData
66bb42fd 1/* ZD1211 USB-WLAN driver for Linux
459c51ad 2 *
66bb42fd
DD
3 * Copyright (C) 2005-2007 Ulrich Kunitz <kune@deine-taler.de>
4 * Copyright (C) 2006-2007 Daniel Drake <dsd@gentoo.org>
5 * Copyright (C) 2006-2007 Michael Wu <flamingice@sourmilk.net>
e83a1070 6 * Copyright (C) 2007-2008 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
e85d0918
DD
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#include <linux/netdevice.h>
24#include <linux/etherdevice.h>
5a0e3ad6 25#include <linux/slab.h>
e85d0918
DD
26#include <linux/usb.h>
27#include <linux/jiffies.h>
28#include <net/ieee80211_radiotap.h>
29
30#include "zd_def.h"
31#include "zd_chip.h"
32#include "zd_mac.h"
e85d0918 33#include "zd_rf.h"
e85d0918 34
e83a1070
LR
35struct zd_reg_alpha2_map {
36 u32 reg;
37 char alpha2[2];
38};
39
40static struct zd_reg_alpha2_map reg_alpha2_map[] = {
41 { ZD_REGDOMAIN_FCC, "US" },
42 { ZD_REGDOMAIN_IC, "CA" },
43 { ZD_REGDOMAIN_ETSI, "DE" }, /* Generic ETSI, use most restrictive */
44 { ZD_REGDOMAIN_JAPAN, "JP" },
3d3b33bd
JL
45 { ZD_REGDOMAIN_JAPAN_2, "JP" },
46 { ZD_REGDOMAIN_JAPAN_3, "JP" },
e83a1070
LR
47 { ZD_REGDOMAIN_SPAIN, "ES" },
48 { ZD_REGDOMAIN_FRANCE, "FR" },
49};
50
459c51ad
DD
51/* This table contains the hardware specific values for the modulation rates. */
52static const struct ieee80211_rate zd_rates[] = {
8318d78a
JB
53 { .bitrate = 10,
54 .hw_value = ZD_CCK_RATE_1M, },
55 { .bitrate = 20,
56 .hw_value = ZD_CCK_RATE_2M,
57 .hw_value_short = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
58 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
59 { .bitrate = 55,
60 .hw_value = ZD_CCK_RATE_5_5M,
61 .hw_value_short = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
62 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
63 { .bitrate = 110,
64 .hw_value = ZD_CCK_RATE_11M,
65 .hw_value_short = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
66 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
67 { .bitrate = 60,
68 .hw_value = ZD_OFDM_RATE_6M,
69 .flags = 0 },
70 { .bitrate = 90,
71 .hw_value = ZD_OFDM_RATE_9M,
72 .flags = 0 },
73 { .bitrate = 120,
74 .hw_value = ZD_OFDM_RATE_12M,
75 .flags = 0 },
76 { .bitrate = 180,
77 .hw_value = ZD_OFDM_RATE_18M,
78 .flags = 0 },
79 { .bitrate = 240,
80 .hw_value = ZD_OFDM_RATE_24M,
81 .flags = 0 },
82 { .bitrate = 360,
83 .hw_value = ZD_OFDM_RATE_36M,
84 .flags = 0 },
85 { .bitrate = 480,
86 .hw_value = ZD_OFDM_RATE_48M,
87 .flags = 0 },
88 { .bitrate = 540,
89 .hw_value = ZD_OFDM_RATE_54M,
90 .flags = 0 },
459c51ad
DD
91};
92
7f4013f0
BP
93/*
94 * Zydas retry rates table. Each line is listed in the same order as
95 * in zd_rates[] and contains all the rate used when a packet is sent
96 * starting with a given rates. Let's consider an example :
97 *
98 * "11 Mbits : 4, 3, 2, 1, 0" means :
99 * - packet is sent using 4 different rates
100 * - 1st rate is index 3 (ie 11 Mbits)
101 * - 2nd rate is index 2 (ie 5.5 Mbits)
102 * - 3rd rate is index 1 (ie 2 Mbits)
103 * - 4th rate is index 0 (ie 1 Mbits)
104 */
105
106static const struct tx_retry_rate zd_retry_rates[] = {
107 { /* 1 Mbits */ 1, { 0 }},
108 { /* 2 Mbits */ 2, { 1, 0 }},
109 { /* 5.5 Mbits */ 3, { 2, 1, 0 }},
110 { /* 11 Mbits */ 4, { 3, 2, 1, 0 }},
111 { /* 6 Mbits */ 5, { 4, 3, 2, 1, 0 }},
112 { /* 9 Mbits */ 6, { 5, 4, 3, 2, 1, 0}},
113 { /* 12 Mbits */ 5, { 6, 3, 2, 1, 0 }},
114 { /* 18 Mbits */ 6, { 7, 6, 3, 2, 1, 0 }},
115 { /* 24 Mbits */ 6, { 8, 6, 3, 2, 1, 0 }},
116 { /* 36 Mbits */ 7, { 9, 8, 6, 3, 2, 1, 0 }},
117 { /* 48 Mbits */ 8, {10, 9, 8, 6, 3, 2, 1, 0 }},
118 { /* 54 Mbits */ 9, {11, 10, 9, 8, 6, 3, 2, 1, 0 }}
119};
120
459c51ad 121static const struct ieee80211_channel zd_channels[] = {
8318d78a
JB
122 { .center_freq = 2412, .hw_value = 1 },
123 { .center_freq = 2417, .hw_value = 2 },
124 { .center_freq = 2422, .hw_value = 3 },
125 { .center_freq = 2427, .hw_value = 4 },
126 { .center_freq = 2432, .hw_value = 5 },
127 { .center_freq = 2437, .hw_value = 6 },
128 { .center_freq = 2442, .hw_value = 7 },
129 { .center_freq = 2447, .hw_value = 8 },
130 { .center_freq = 2452, .hw_value = 9 },
131 { .center_freq = 2457, .hw_value = 10 },
132 { .center_freq = 2462, .hw_value = 11 },
133 { .center_freq = 2467, .hw_value = 12 },
134 { .center_freq = 2472, .hw_value = 13 },
135 { .center_freq = 2484, .hw_value = 14 },
459c51ad 136};
e85d0918 137
583afd1e
UK
138static void housekeeping_init(struct zd_mac *mac);
139static void housekeeping_enable(struct zd_mac *mac);
140static void housekeeping_disable(struct zd_mac *mac);
9be23256
JK
141static void beacon_init(struct zd_mac *mac);
142static void beacon_enable(struct zd_mac *mac);
143static void beacon_disable(struct zd_mac *mac);
583afd1e 144
e83a1070
LR
145static int zd_reg2alpha2(u8 regdomain, char *alpha2)
146{
147 unsigned int i;
148 struct zd_reg_alpha2_map *reg_map;
149 for (i = 0; i < ARRAY_SIZE(reg_alpha2_map); i++) {
150 reg_map = &reg_alpha2_map[i];
151 if (regdomain == reg_map->reg) {
152 alpha2[0] = reg_map->alpha2[0];
153 alpha2[1] = reg_map->alpha2[1];
154 return 0;
155 }
156 }
157 return 1;
158}
159
459c51ad 160int zd_mac_preinit_hw(struct ieee80211_hw *hw)
e85d0918
DD
161{
162 int r;
e85d0918 163 u8 addr[ETH_ALEN];
459c51ad 164 struct zd_mac *mac = zd_hw_mac(hw);
74553aed
DD
165
166 r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
167 if (r)
168 return r;
169
459c51ad
DD
170 SET_IEEE80211_PERM_ADDR(hw, addr);
171
74553aed
DD
172 return 0;
173}
174
459c51ad 175int zd_mac_init_hw(struct ieee80211_hw *hw)
74553aed
DD
176{
177 int r;
459c51ad 178 struct zd_mac *mac = zd_hw_mac(hw);
74553aed 179 struct zd_chip *chip = &mac->chip;
e83a1070 180 char alpha2[2];
e85d0918
DD
181 u8 default_regdomain;
182
183 r = zd_chip_enable_int(chip);
184 if (r)
185 goto out;
74553aed 186 r = zd_chip_init_hw(chip);
e85d0918
DD
187 if (r)
188 goto disable_int;
189
e85d0918 190 ZD_ASSERT(!irqs_disabled());
e85d0918
DD
191
192 r = zd_read_regdomain(chip, &default_regdomain);
193 if (r)
194 goto disable_int;
e85d0918
DD
195 spin_lock_irq(&mac->lock);
196 mac->regdomain = mac->default_regdomain = default_regdomain;
197 spin_unlock_irq(&mac->lock);
e85d0918 198
40da08bc
DD
199 /* We must inform the device that we are doing encryption/decryption in
200 * software at the moment. */
201 r = zd_set_encryption_type(chip, ENC_SNIFFER);
e85d0918
DD
202 if (r)
203 goto disable_int;
204
e83a1070 205 r = zd_reg2alpha2(mac->regdomain, alpha2);
fe33eb39
LR
206 if (r)
207 goto disable_int;
e85d0918 208
fe33eb39 209 r = regulatory_hint(hw->wiphy, alpha2);
e85d0918
DD
210disable_int:
211 zd_chip_disable_int(chip);
212out:
213 return r;
214}
215
216void zd_mac_clear(struct zd_mac *mac)
217{
9cdac965 218 flush_workqueue(zd_workqueue);
e85d0918 219 zd_chip_clear(&mac->chip);
c48cf125
UK
220 ZD_ASSERT(!spin_is_locked(&mac->lock));
221 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
e85d0918
DD
222}
223
c5691235 224static int set_rx_filter(struct zd_mac *mac)
e85d0918 225{
459c51ad
DD
226 unsigned long flags;
227 u32 filter = STA_RX_FILTER;
e85d0918 228
459c51ad
DD
229 spin_lock_irqsave(&mac->lock, flags);
230 if (mac->pass_ctrl)
231 filter |= RX_FILTER_CTRL;
232 spin_unlock_irqrestore(&mac->lock, flags);
233
234 return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
c5691235
UK
235}
236
c2fadcb3
JK
237static int set_mac_and_bssid(struct zd_mac *mac)
238{
239 int r;
240
241 if (!mac->vif)
242 return -1;
243
244 r = zd_write_mac_addr(&mac->chip, mac->vif->addr);
245 if (r)
246 return r;
247
248 /* Vendor driver after setting MAC either sets BSSID for AP or
249 * filter for other modes.
250 */
251 if (mac->type != NL80211_IFTYPE_AP)
252 return set_rx_filter(mac);
253 else
254 return zd_write_bssid(&mac->chip, mac->vif->addr);
255}
256
c5691235
UK
257static int set_mc_hash(struct zd_mac *mac)
258{
259 struct zd_mc_hash hash;
c5691235 260 zd_mc_clear(&hash);
c5691235
UK
261 return zd_chip_set_multicast_hash(&mac->chip, &hash);
262}
263
459c51ad 264static int zd_op_start(struct ieee80211_hw *hw)
e85d0918 265{
459c51ad 266 struct zd_mac *mac = zd_hw_mac(hw);
e85d0918 267 struct zd_chip *chip = &mac->chip;
74553aed 268 struct zd_usb *usb = &chip->usb;
e85d0918
DD
269 int r;
270
74553aed
DD
271 if (!usb->initialized) {
272 r = zd_usb_init_hw(usb);
273 if (r)
274 goto out;
275 }
276
e85d0918
DD
277 r = zd_chip_enable_int(chip);
278 if (r < 0)
279 goto out;
280
281 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
282 if (r < 0)
283 goto disable_int;
c5691235 284 r = set_rx_filter(mac);
c5691235
UK
285 if (r)
286 goto disable_int;
287 r = set_mc_hash(mac);
e85d0918
DD
288 if (r)
289 goto disable_int;
290 r = zd_chip_switch_radio_on(chip);
291 if (r < 0)
292 goto disable_int;
459c51ad 293 r = zd_chip_enable_rxtx(chip);
e85d0918
DD
294 if (r < 0)
295 goto disable_radio;
296 r = zd_chip_enable_hwint(chip);
297 if (r < 0)
459c51ad 298 goto disable_rxtx;
e85d0918 299
583afd1e 300 housekeeping_enable(mac);
9be23256
JK
301 beacon_enable(mac);
302 set_bit(ZD_DEVICE_RUNNING, &mac->flags);
e85d0918 303 return 0;
459c51ad
DD
304disable_rxtx:
305 zd_chip_disable_rxtx(chip);
e85d0918
DD
306disable_radio:
307 zd_chip_switch_radio_off(chip);
308disable_int:
309 zd_chip_disable_int(chip);
310out:
311 return r;
312}
313
459c51ad
DD
314static void zd_op_stop(struct ieee80211_hw *hw)
315{
316 struct zd_mac *mac = zd_hw_mac(hw);
317 struct zd_chip *chip = &mac->chip;
318 struct sk_buff *skb;
319 struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
c9a4b35d 320
9be23256
JK
321 clear_bit(ZD_DEVICE_RUNNING, &mac->flags);
322
459c51ad 323 /* The order here deliberately is a little different from the open()
e85d0918 324 * method, since we need to make sure there is no opportunity for RX
459c51ad 325 * frames to be processed by mac80211 after we have stopped it.
e85d0918
DD
326 */
327
459c51ad 328 zd_chip_disable_rxtx(chip);
9be23256 329 beacon_disable(mac);
583afd1e 330 housekeeping_disable(mac);
b1382ede 331 flush_workqueue(zd_workqueue);
b1382ede 332
e85d0918
DD
333 zd_chip_disable_hwint(chip);
334 zd_chip_switch_radio_off(chip);
335 zd_chip_disable_int(chip);
336
e85d0918 337
459c51ad 338 while ((skb = skb_dequeue(ack_wait_queue)))
e039fa4a 339 dev_kfree_skb_any(skb);
e85d0918
DD
340}
341
459c51ad 342/**
7f4013f0 343 * zd_mac_tx_status - reports tx status of a packet if required
459c51ad
DD
344 * @hw - a &struct ieee80211_hw pointer
345 * @skb - a sk-buffer
e039fa4a
JB
346 * @flags: extra flags to set in the TX status info
347 * @ackssi: ACK signal strength
73ac36ea 348 * @success - True for successful transmission of the frame
459c51ad
DD
349 *
350 * This information calls ieee80211_tx_status_irqsafe() if required by the
351 * control information. It copies the control information into the status
352 * information.
353 *
354 * If no status information has been requested, the skb is freed.
355 */
7f4013f0
BP
356static void zd_mac_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
357 int ackssi, struct tx_status *tx_status)
b1382ede 358{
e039fa4a 359 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
7f4013f0
BP
360 int i;
361 int success = 1, retry = 1;
362 int first_idx;
363 const struct tx_retry_rate *retries;
e039fa4a 364
e6a9854b 365 ieee80211_tx_info_clear_status(info);
b1382ede 366
7f4013f0
BP
367 if (tx_status) {
368 success = !tx_status->failure;
369 retry = tx_status->retry + success;
370 }
371
372 if (success) {
373 /* success */
e6a9854b 374 info->flags |= IEEE80211_TX_STAT_ACK;
7f4013f0
BP
375 } else {
376 /* failure */
377 info->flags &= ~IEEE80211_TX_STAT_ACK;
378 }
379
380 first_idx = info->status.rates[0].idx;
381 ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
382 retries = &zd_retry_rates[first_idx];
86baf712 383 ZD_ASSERT(1 <= retry && retry <= retries->count);
7f4013f0
BP
384
385 info->status.rates[0].idx = retries->rate[0];
386 info->status.rates[0].count = 1; // (retry > 1 ? 2 : 1);
387
388 for (i=1; i<IEEE80211_TX_MAX_RATES-1 && i<retry; i++) {
389 info->status.rates[i].idx = retries->rate[i];
390 info->status.rates[i].count = 1; // ((i==retry-1) && success ? 1:2);
391 }
392 for (; i<IEEE80211_TX_MAX_RATES && i<retry; i++) {
86baf712 393 info->status.rates[i].idx = retries->rate[retry - 1];
7f4013f0
BP
394 info->status.rates[i].count = 1; // (success ? 1:2);
395 }
396 if (i<IEEE80211_TX_MAX_RATES)
397 info->status.rates[i].idx = -1; /* terminate */
398
e039fa4a
JB
399 info->status.ack_signal = ackssi;
400 ieee80211_tx_status_irqsafe(hw, skb);
b1382ede
DD
401}
402
459c51ad
DD
403/**
404 * zd_mac_tx_failed - callback for failed frames
405 * @dev: the mac80211 wireless device
406 *
303863f4 407 * This function is called if a frame couldn't be successfully
459c51ad
DD
408 * transferred. The first frame from the tx queue, will be selected and
409 * reported as error to the upper layers.
410 */
7f4013f0 411void zd_mac_tx_failed(struct urb *urb)
b1382ede 412{
7f4013f0
BP
413 struct ieee80211_hw * hw = zd_usb_to_hw(urb->context);
414 struct zd_mac *mac = zd_hw_mac(hw);
415 struct sk_buff_head *q = &mac->ack_wait_queue;
459c51ad 416 struct sk_buff *skb;
7f4013f0
BP
417 struct tx_status *tx_status = (struct tx_status *)urb->transfer_buffer;
418 unsigned long flags;
419 int success = !tx_status->failure;
420 int retry = tx_status->retry + success;
421 int found = 0;
422 int i, position = 0;
b1382ede 423
7f4013f0
BP
424 q = &mac->ack_wait_queue;
425 spin_lock_irqsave(&q->lock, flags);
426
427 skb_queue_walk(q, skb) {
428 struct ieee80211_hdr *tx_hdr;
429 struct ieee80211_tx_info *info;
430 int first_idx, final_idx;
431 const struct tx_retry_rate *retries;
432 u8 final_rate;
433
434 position ++;
435
436 /* if the hardware reports a failure and we had a 802.11 ACK
437 * pending, then we skip the first skb when searching for a
438 * matching frame */
439 if (tx_status->failure && mac->ack_pending &&
440 skb_queue_is_first(q, skb)) {
441 continue;
442 }
443
444 tx_hdr = (struct ieee80211_hdr *)skb->data;
445
446 /* we skip all frames not matching the reported destination */
447 if (unlikely(memcmp(tx_hdr->addr1, tx_status->mac, ETH_ALEN))) {
448 continue;
449 }
450
451 /* we skip all frames not matching the reported final rate */
5078ed50 452
7f4013f0
BP
453 info = IEEE80211_SKB_CB(skb);
454 first_idx = info->status.rates[0].idx;
455 ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
456 retries = &zd_retry_rates[first_idx];
86baf712 457 if (retry <= 0 || retry > retries->count)
7f4013f0 458 continue;
7f4013f0 459
86baf712 460 final_idx = retries->rate[retry - 1];
7f4013f0
BP
461 final_rate = zd_rates[final_idx].hw_value;
462
463 if (final_rate != tx_status->rate) {
464 continue;
465 }
466
467 found = 1;
468 break;
469 }
470
471 if (found) {
472 for (i=1; i<=position; i++) {
473 skb = __skb_dequeue(q);
474 zd_mac_tx_status(hw, skb,
475 mac->ack_pending ? mac->ack_signal : 0,
476 i == position ? tx_status : NULL);
477 mac->ack_pending = 0;
478 }
479 }
480
481 spin_unlock_irqrestore(&q->lock, flags);
b1382ede
DD
482}
483
459c51ad
DD
484/**
485 * zd_mac_tx_to_dev - callback for USB layer
486 * @skb: a &sk_buff pointer
487 * @error: error value, 0 if transmission successful
488 *
489 * Informs the MAC layer that the frame has successfully transferred to the
490 * device. If an ACK is required and the transfer to the device has been
491 * successful, the packets are put on the @ack_wait_queue with
492 * the control set removed.
493 */
494void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
495{
e039fa4a 496 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
e6a9854b 497 struct ieee80211_hw *hw = info->rate_driver_data[0];
7f4013f0
BP
498 struct zd_mac *mac = zd_hw_mac(hw);
499
500 ieee80211_tx_info_clear_status(info);
b1382ede 501
e039fa4a
JB
502 skb_pull(skb, sizeof(struct zd_ctrlset));
503 if (unlikely(error ||
504 (info->flags & IEEE80211_TX_CTL_NO_ACK))) {
7f4013f0
BP
505 /*
506 * FIXME : do we need to fill in anything ?
507 */
508 ieee80211_tx_status_irqsafe(hw, skb);
459c51ad 509 } else {
7f4013f0 510 struct sk_buff_head *q = &mac->ack_wait_queue;
e039fa4a
JB
511
512 skb_queue_tail(q, skb);
7f4013f0
BP
513 while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS) {
514 zd_mac_tx_status(hw, skb_dequeue(q),
515 mac->ack_pending ? mac->ack_signal : 0,
516 NULL);
517 mac->ack_pending = 0;
518 }
e85d0918 519 }
e85d0918
DD
520}
521
b1cd8416 522static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
e85d0918 523{
64f222cc 524 /* ZD_PURE_RATE() must be used to remove the modulation type flag of
459c51ad
DD
525 * the zd-rate values.
526 */
e85d0918 527 static const u8 rate_divisor[] = {
459c51ad
DD
528 [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
529 [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
530 /* Bits must be doubled. */
531 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
532 [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
533 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
534 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
535 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
536 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
537 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
538 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
539 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
540 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
e85d0918
DD
541 };
542
543 u32 bits = (u32)tx_length * 8;
544 u32 divisor;
545
64f222cc 546 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
e85d0918
DD
547 if (divisor == 0)
548 return -EINVAL;
549
b1cd8416
DD
550 switch (zd_rate) {
551 case ZD_CCK_RATE_5_5M:
e85d0918
DD
552 bits = (2*bits) + 10; /* round up to the next integer */
553 break;
b1cd8416 554 case ZD_CCK_RATE_11M:
e85d0918
DD
555 if (service) {
556 u32 t = bits % 11;
557 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
558 if (0 < t && t <= 3) {
559 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
560 }
561 }
562 bits += 10; /* round up to the next integer */
563 break;
564 }
565
566 return bits/divisor;
567}
568
e85d0918 569static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
e6a9854b
JB
570 struct ieee80211_hdr *header,
571 struct ieee80211_tx_info *info)
e85d0918 572{
e85d0918 573 /*
b1382ede 574 * CONTROL TODO:
e85d0918
DD
575 * - if backoff needed, enable bit 0
576 * - if burst (backoff not needed) disable bit 0
e85d0918
DD
577 */
578
579 cs->control = 0;
580
581 /* First fragment */
e6a9854b 582 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
e85d0918
DD
583 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
584
13bdcd90
GS
585 /* No ACK expected (multicast, etc.) */
586 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
587 cs->control |= ZD_CS_NO_ACK;
e85d0918
DD
588
589 /* PS-POLL */
85365820 590 if (ieee80211_is_pspoll(header->frame_control))
e85d0918
DD
591 cs->control |= ZD_CS_PS_POLL_FRAME;
592
e6a9854b 593 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
b1382ede
DD
594 cs->control |= ZD_CS_RTS;
595
e6a9854b 596 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
b1382ede 597 cs->control |= ZD_CS_SELF_CTS;
e85d0918
DD
598
599 /* FIXME: Management frame? */
600}
601
f2cae6c5 602static int zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon)
72e77a8a
LCC
603{
604 struct zd_mac *mac = zd_hw_mac(hw);
51272292 605 int r, ret, num_cmds, req_pos = 0;
72e77a8a
LCC
606 u32 tmp, j = 0;
607 /* 4 more bytes for tail CRC */
608 u32 full_len = beacon->len + 4;
9be23256 609 unsigned long end_jiffies, message_jiffies;
51272292
JK
610 struct zd_ioreq32 *ioreqs;
611
612 /* Alloc memory for full beacon write at once. */
613 num_cmds = 1 + zd_chip_is_zd1211b(&mac->chip) + full_len;
614 ioreqs = kmalloc(num_cmds * sizeof(struct zd_ioreq32), GFP_KERNEL);
615 if (!ioreqs)
616 return -ENOMEM;
9be23256
JK
617
618 mutex_lock(&mac->chip.mutex);
f2cae6c5 619
9be23256 620 r = zd_iowrite32_locked(&mac->chip, 0, CR_BCN_FIFO_SEMAPHORE);
f2cae6c5 621 if (r < 0)
9be23256
JK
622 goto out;
623 r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
f2cae6c5 624 if (r < 0)
9be23256 625 goto release_sema;
f2cae6c5 626
9be23256
JK
627 end_jiffies = jiffies + HZ / 2; /*~500ms*/
628 message_jiffies = jiffies + HZ / 10; /*~100ms*/
72e77a8a 629 while (tmp & 0x2) {
9be23256 630 r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
f2cae6c5 631 if (r < 0)
9be23256
JK
632 goto release_sema;
633 if (time_is_before_eq_jiffies(message_jiffies)) {
634 message_jiffies = jiffies + HZ / 10;
635 dev_err(zd_mac_dev(mac),
636 "CR_BCN_FIFO_SEMAPHORE not ready\n");
637 if (time_is_before_eq_jiffies(end_jiffies)) {
638 dev_err(zd_mac_dev(mac),
639 "Giving up beacon config.\n");
640 r = -ETIMEDOUT;
641 goto release_sema;
72e77a8a
LCC
642 }
643 }
9be23256 644 msleep(20);
72e77a8a
LCC
645 }
646
51272292
JK
647 ioreqs[req_pos].addr = CR_BCN_FIFO;
648 ioreqs[req_pos].value = full_len - 1;
649 req_pos++;
f2cae6c5 650 if (zd_chip_is_zd1211b(&mac->chip)) {
51272292
JK
651 ioreqs[req_pos].addr = CR_BCN_LENGTH;
652 ioreqs[req_pos].value = full_len - 1;
653 req_pos++;
f2cae6c5 654 }
72e77a8a 655
f2cae6c5 656 for (j = 0 ; j < beacon->len; j++) {
51272292
JK
657 ioreqs[req_pos].addr = CR_BCN_FIFO;
658 ioreqs[req_pos].value = *((u8 *)(beacon->data + j));
659 req_pos++;
f2cae6c5 660 }
72e77a8a 661
f2cae6c5 662 for (j = 0; j < 4; j++) {
51272292
JK
663 ioreqs[req_pos].addr = CR_BCN_FIFO;
664 ioreqs[req_pos].value = 0x0;
665 req_pos++;
f2cae6c5
DD
666 }
667
51272292
JK
668 BUG_ON(req_pos != num_cmds);
669
670 r = zd_iowrite32a_locked(&mac->chip, ioreqs, num_cmds);
671
9be23256
JK
672release_sema:
673 /*
674 * Try very hard to release device beacon semaphore, as otherwise
675 * device/driver can be left in unusable state.
676 */
677 end_jiffies = jiffies + HZ / 2; /*~500ms*/
678 ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
679 while (ret < 0) {
680 if (time_is_before_eq_jiffies(end_jiffies)) {
681 ret = -ETIMEDOUT;
682 break;
683 }
684
685 msleep(20);
686 ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
687 }
688
689 if (ret < 0)
690 dev_err(zd_mac_dev(mac), "Could not release "
691 "CR_BCN_FIFO_SEMAPHORE!\n");
692 if (r < 0 || ret < 0) {
693 if (r >= 0)
694 r = ret;
695 goto out;
696 }
72e77a8a 697
72e77a8a
LCC
698 /* 802.11b/g 2.4G CCK 1Mb
699 * 802.11a, not yet implemented, uses different values (see GPL vendor
700 * driver)
701 */
9be23256
JK
702 r = zd_iowrite32_locked(&mac->chip, 0x00000400 | (full_len << 19),
703 CR_BCN_PLCP_CFG);
704out:
705 mutex_unlock(&mac->chip.mutex);
51272292 706 kfree(ioreqs);
9be23256 707 return r;
72e77a8a
LCC
708}
709
e85d0918 710static int fill_ctrlset(struct zd_mac *mac,
e039fa4a 711 struct sk_buff *skb)
e85d0918
DD
712{
713 int r;
459c51ad
DD
714 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
715 unsigned int frag_len = skb->len + FCS_LEN;
e85d0918 716 unsigned int packet_length;
2e92e6f2 717 struct ieee80211_rate *txrate;
e85d0918
DD
718 struct zd_ctrlset *cs = (struct zd_ctrlset *)
719 skb_push(skb, sizeof(struct zd_ctrlset));
e039fa4a 720 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
e85d0918 721
e85d0918 722 ZD_ASSERT(frag_len <= 0xffff);
e85d0918 723
e039fa4a 724 txrate = ieee80211_get_tx_rate(mac->hw, info);
2e92e6f2
JB
725
726 cs->modulation = txrate->hw_value;
e6a9854b 727 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
2e92e6f2 728 cs->modulation = txrate->hw_value_short;
e85d0918
DD
729
730 cs->tx_length = cpu_to_le16(frag_len);
731
e6a9854b 732 cs_set_control(mac, cs, hdr, info);
e85d0918
DD
733
734 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
735 ZD_ASSERT(packet_length <= 0xffff);
736 /* ZD1211B: Computing the length difference this way, gives us
737 * flexibility to compute the packet length.
738 */
74553aed 739 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
e85d0918
DD
740 packet_length - frag_len : packet_length);
741
742 /*
743 * CURRENT LENGTH:
744 * - transmit frame length in microseconds
745 * - seems to be derived from frame length
746 * - see Cal_Us_Service() in zdinlinef.h
747 * - if macp->bTxBurstEnable is enabled, then multiply by 4
748 * - bTxBurstEnable is never set in the vendor driver
749 *
750 * SERVICE:
751 * - "for PLCP configuration"
752 * - always 0 except in some situations at 802.11b 11M
753 * - see line 53 of zdinlinef.h
754 */
755 cs->service = 0;
64f222cc 756 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
e85d0918
DD
757 le16_to_cpu(cs->tx_length));
758 if (r < 0)
759 return r;
760 cs->current_length = cpu_to_le16(r);
459c51ad 761 cs->next_frame_length = 0;
e85d0918
DD
762
763 return 0;
764}
765
459c51ad
DD
766/**
767 * zd_op_tx - transmits a network frame to the device
768 *
769 * @dev: mac80211 hardware device
770 * @skb: socket buffer
771 * @control: the control structure
772 *
773 * This function transmit an IEEE 802.11 network frame to the device. The
774 * control block of the skbuff will be initialized. If necessary the incoming
775 * mac80211 queues will be stopped.
776 */
e039fa4a 777static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
e85d0918 778{
459c51ad 779 struct zd_mac *mac = zd_hw_mac(hw);
e039fa4a 780 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
459c51ad 781 int r;
e85d0918 782
e039fa4a 783 r = fill_ctrlset(mac, skb);
459c51ad 784 if (r)
640c65ea 785 goto fail;
e85d0918 786
e6a9854b 787 info->rate_driver_data[0] = hw;
e039fa4a 788
459c51ad 789 r = zd_usb_tx(&mac->chip.usb, skb);
e039fa4a 790 if (r)
640c65ea
JM
791 goto fail;
792 return 0;
793
794fail:
795 dev_kfree_skb(skb);
e85d0918
DD
796 return 0;
797}
798
459c51ad
DD
799/**
800 * filter_ack - filters incoming packets for acknowledgements
801 * @dev: the mac80211 device
802 * @rx_hdr: received header
803 * @stats: the status for the received packet
741fec53 804 *
459c51ad
DD
805 * This functions looks for ACK packets and tries to match them with the
806 * frames in the tx queue. If a match is found the frame will be dequeued and
807 * the upper layers is informed about the successful transmission. If
808 * mac80211 queues have been stopped and the number of frames still to be
809 * transmitted is low the queues will be opened again.
e85d0918 810 *
459c51ad 811 * Returns 1 if the frame was an ACK, 0 if it was ignored.
e85d0918 812 */
459c51ad
DD
813static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
814 struct ieee80211_rx_status *stats)
e85d0918 815{
7f4013f0 816 struct zd_mac *mac = zd_hw_mac(hw);
459c51ad
DD
817 struct sk_buff *skb;
818 struct sk_buff_head *q;
819 unsigned long flags;
7f4013f0
BP
820 int found = 0;
821 int i, position = 0;
e85d0918 822
85365820 823 if (!ieee80211_is_ack(rx_hdr->frame_control))
e85d0918 824 return 0;
e85d0918 825
7f4013f0 826 q = &mac->ack_wait_queue;
459c51ad 827 spin_lock_irqsave(&q->lock, flags);
47a227db 828 skb_queue_walk(q, skb) {
459c51ad
DD
829 struct ieee80211_hdr *tx_hdr;
830
7f4013f0
BP
831 position ++;
832
833 if (mac->ack_pending && skb_queue_is_first(q, skb))
834 continue;
835
459c51ad 836 tx_hdr = (struct ieee80211_hdr *)skb->data;
cde6901b 837 if (likely(!memcmp(tx_hdr->addr2, rx_hdr->addr1, ETH_ALEN)))
459c51ad 838 {
7f4013f0
BP
839 found = 1;
840 break;
459c51ad
DD
841 }
842 }
7f4013f0
BP
843
844 if (found) {
845 for (i=1; i<position; i++) {
846 skb = __skb_dequeue(q);
847 zd_mac_tx_status(hw, skb,
848 mac->ack_pending ? mac->ack_signal : 0,
849 NULL);
850 mac->ack_pending = 0;
851 }
852
853 mac->ack_pending = 1;
854 mac->ack_signal = stats->signal;
f773e409
JK
855
856 /* Prevent pending tx-packet on AP-mode */
857 if (mac->type == NL80211_IFTYPE_AP) {
858 skb = __skb_dequeue(q);
859 zd_mac_tx_status(hw, skb, mac->ack_signal, NULL);
860 mac->ack_pending = 0;
861 }
7f4013f0
BP
862 }
863
459c51ad
DD
864 spin_unlock_irqrestore(&q->lock, flags);
865 return 1;
e85d0918
DD
866}
867
459c51ad 868int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
e85d0918 869{
459c51ad
DD
870 struct zd_mac *mac = zd_hw_mac(hw);
871 struct ieee80211_rx_status stats;
872 const struct rx_status *status;
873 struct sk_buff *skb;
874 int bad_frame = 0;
85365820
HH
875 __le16 fc;
876 int need_padding;
8318d78a
JB
877 int i;
878 u8 rate;
db888aed 879
459c51ad
DD
880 if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
881 FCS_LEN + sizeof(struct rx_status))
882 return -EINVAL;
e85d0918 883
459c51ad 884 memset(&stats, 0, sizeof(stats));
e85d0918 885
459c51ad
DD
886 /* Note about pass_failed_fcs and pass_ctrl access below:
887 * mac locking intentionally omitted here, as this is the only unlocked
888 * reader and the only writer is configure_filter. Plus, if there were
889 * any races accessing these variables, it wouldn't really matter.
890 * If mac80211 ever provides a way for us to access filter flags
891 * from outside configure_filter, we could improve on this. Also, this
892 * situation may change once we implement some kind of DMA-into-skb
893 * RX path. */
e85d0918 894
459c51ad
DD
895 /* Caller has to ensure that length >= sizeof(struct rx_status). */
896 status = (struct rx_status *)
937a049d 897 (buffer + (length - sizeof(struct rx_status)));
e85d0918 898 if (status->frame_status & ZD_RX_ERROR) {
459c51ad
DD
899 if (mac->pass_failed_fcs &&
900 (status->frame_status & ZD_RX_CRC32_ERROR)) {
901 stats.flag |= RX_FLAG_FAILED_FCS_CRC;
902 bad_frame = 1;
903 } else {
904 return -EINVAL;
22d3405f 905 }
e85d0918 906 }
22d3405f 907
8318d78a
JB
908 stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
909 stats.band = IEEE80211_BAND_2GHZ;
566bfe5a 910 stats.signal = status->signal_strength;
8318d78a
JB
911
912 rate = zd_rx_rate(buffer, status);
913
914 /* todo: return index in the big switches in zd_rx_rate instead */
915 for (i = 0; i < mac->band.n_bitrates; i++)
916 if (rate == mac->band.bitrates[i].hw_value)
917 stats.rate_idx = i;
459c51ad
DD
918
919 length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
920 buffer += ZD_PLCP_HEADER_SIZE;
921
922 /* Except for bad frames, filter each frame to see if it is an ACK, in
923 * which case our internal TX tracking is updated. Normally we then
924 * bail here as there's no need to pass ACKs on up to the stack, but
925 * there is also the case where the stack has requested us to pass
926 * control frames on up (pass_ctrl) which we must consider. */
927 if (!bad_frame &&
928 filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
929 && !mac->pass_ctrl)
930 return 0;
e85d0918 931
42935eca 932 fc = get_unaligned((__le16*)buffer);
85365820 933 need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
9081728b
MB
934
935 skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
459c51ad
DD
936 if (skb == NULL)
937 return -ENOMEM;
9081728b 938 if (need_padding) {
77c2061d 939 /* Make sure the payload data is 4 byte aligned. */
9081728b
MB
940 skb_reserve(skb, 2);
941 }
942
7f4013f0 943 /* FIXME : could we avoid this big memcpy ? */
459c51ad
DD
944 memcpy(skb_put(skb, length), buffer, length);
945
f1d58c25
JB
946 memcpy(IEEE80211_SKB_RXCB(skb), &stats, sizeof(stats));
947 ieee80211_rx_irqsafe(hw, skb);
e85d0918
DD
948 return 0;
949}
950
459c51ad 951static int zd_op_add_interface(struct ieee80211_hw *hw,
1ed32e4f 952 struct ieee80211_vif *vif)
e85d0918 953{
459c51ad 954 struct zd_mac *mac = zd_hw_mac(hw);
e85d0918 955
05c914fe
JB
956 /* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */
957 if (mac->type != NL80211_IFTYPE_UNSPECIFIED)
459c51ad 958 return -EOPNOTSUPP;
e85d0918 959
1ed32e4f 960 switch (vif->type) {
05c914fe
JB
961 case NL80211_IFTYPE_MONITOR:
962 case NL80211_IFTYPE_MESH_POINT:
963 case NL80211_IFTYPE_STATION:
964 case NL80211_IFTYPE_ADHOC:
1ed32e4f 965 mac->type = vif->type;
459c51ad
DD
966 break;
967 default:
968 return -EOPNOTSUPP;
4d1feabc 969 }
e85d0918 970
c2fadcb3
JK
971 mac->vif = vif;
972
973 return set_mac_and_bssid(mac);
459c51ad 974}
e85d0918 975
459c51ad 976static void zd_op_remove_interface(struct ieee80211_hw *hw,
1ed32e4f 977 struct ieee80211_vif *vif)
459c51ad
DD
978{
979 struct zd_mac *mac = zd_hw_mac(hw);
05c914fe 980 mac->type = NL80211_IFTYPE_UNSPECIFIED;
c2fadcb3 981 mac->vif = NULL;
b91a515d 982 zd_set_beacon_interval(&mac->chip, 0, 0, NL80211_IFTYPE_UNSPECIFIED);
459c51ad
DD
983 zd_write_mac_addr(&mac->chip, NULL);
984}
93137943 985
e8975581 986static int zd_op_config(struct ieee80211_hw *hw, u32 changed)
459c51ad
DD
987{
988 struct zd_mac *mac = zd_hw_mac(hw);
e8975581
JB
989 struct ieee80211_conf *conf = &hw->conf;
990
8318d78a 991 return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
459c51ad 992}
db888aed 993
4099e2f4
JK
994static void zd_beacon_done(struct zd_mac *mac)
995{
996 struct sk_buff *skb, *beacon;
997
9be23256
JK
998 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
999 return;
4099e2f4
JK
1000 if (!mac->vif || mac->vif->type != NL80211_IFTYPE_AP)
1001 return;
1002
1003 /*
1004 * Send out buffered broad- and multicast frames.
1005 */
1006 while (!ieee80211_queue_stopped(mac->hw, 0)) {
1007 skb = ieee80211_get_buffered_bc(mac->hw, mac->vif);
1008 if (!skb)
1009 break;
1010 zd_op_tx(mac->hw, skb);
1011 }
1012
1013 /*
1014 * Fetch next beacon so that tim_count is updated.
1015 */
1016 beacon = ieee80211_beacon_get(mac->hw, mac->vif);
9be23256
JK
1017 if (beacon) {
1018 zd_mac_config_beacon(mac->hw, beacon);
1019 kfree_skb(beacon);
1020 }
4099e2f4 1021
9be23256
JK
1022 spin_lock_irq(&mac->lock);
1023 mac->beacon.last_update = jiffies;
1024 spin_unlock_irq(&mac->lock);
4099e2f4
JK
1025}
1026
e83a1070 1027static void zd_process_intr(struct work_struct *work)
72e77a8a
LCC
1028{
1029 u16 int_status;
8b17f75c 1030 unsigned long flags;
72e77a8a
LCC
1031 struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
1032
8b17f75c
JK
1033 spin_lock_irqsave(&mac->lock, flags);
1034 int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer + 4));
1035 spin_unlock_irqrestore(&mac->lock, flags);
1036
4099e2f4
JK
1037 if (int_status & INT_CFG_NEXT_BCN) {
1038 /*dev_dbg_f_limit(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");*/
1039 zd_beacon_done(mac);
1040 } else {
72e77a8a 1041 dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
4099e2f4 1042 }
72e77a8a
LCC
1043
1044 zd_chip_enable_hwint(&mac->chip);
1045}
1046
1047
3ac64bee 1048static u64 zd_op_prepare_multicast(struct ieee80211_hw *hw,
22bedad3 1049 struct netdev_hw_addr_list *mc_list)
3ac64bee
JB
1050{
1051 struct zd_mac *mac = zd_hw_mac(hw);
1052 struct zd_mc_hash hash;
22bedad3 1053 struct netdev_hw_addr *ha;
3ac64bee
JB
1054
1055 zd_mc_clear(&hash);
1056
22bedad3
JP
1057 netdev_hw_addr_list_for_each(ha, mc_list) {
1058 dev_dbg_f(zd_mac_dev(mac), "mc addr %pM\n", ha->addr);
1059 zd_mc_add_addr(&hash, ha->addr);
3ac64bee
JB
1060 }
1061
1062 return hash.low | ((u64)hash.high << 32);
1063}
1064
459c51ad
DD
1065#define SUPPORTED_FIF_FLAGS \
1066 (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
2c1a1b12 1067 FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
459c51ad
DD
1068static void zd_op_configure_filter(struct ieee80211_hw *hw,
1069 unsigned int changed_flags,
1070 unsigned int *new_flags,
3ac64bee 1071 u64 multicast)
e85d0918 1072{
3ac64bee
JB
1073 struct zd_mc_hash hash = {
1074 .low = multicast,
1075 .high = multicast >> 32,
1076 };
459c51ad
DD
1077 struct zd_mac *mac = zd_hw_mac(hw);
1078 unsigned long flags;
a6fb071b 1079 int r;
e85d0918 1080
459c51ad
DD
1081 /* Only deal with supported flags */
1082 changed_flags &= SUPPORTED_FIF_FLAGS;
1083 *new_flags &= SUPPORTED_FIF_FLAGS;
1084
7de3c5dc
BP
1085 /*
1086 * If multicast parameter (as returned by zd_op_prepare_multicast)
1087 * has changed, no bit in changed_flags is set. To handle this
1088 * situation, we do not return if changed_flags is 0. If we do so,
1089 * we will have some issue with IPv6 which uses multicast for link
1090 * layer address resolution.
1091 */
3ac64bee 1092 if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI))
459c51ad 1093 zd_mc_add_all(&hash);
459c51ad
DD
1094
1095 spin_lock_irqsave(&mac->lock, flags);
1096 mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
1097 mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
1098 mac->multicast_hash = hash;
1099 spin_unlock_irqrestore(&mac->lock, flags);
3ac64bee 1100
a6fb071b 1101 zd_chip_set_multicast_hash(&mac->chip, &hash);
459c51ad 1102
a6fb071b
JK
1103 if (changed_flags & FIF_CONTROL) {
1104 r = set_rx_filter(mac);
1105 if (r)
1106 dev_err(zd_mac_dev(mac), "set_rx_filter error %d\n", r);
1107 }
459c51ad
DD
1108
1109 /* no handling required for FIF_OTHER_BSS as we don't currently
1110 * do BSSID filtering */
1111 /* FIXME: in future it would be nice to enable the probe response
1112 * filter (so that the driver doesn't see them) until
1113 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
1114 * have to schedule work to enable prbresp reception, which might
1115 * happen too late. For now we'll just listen and forward them all the
1116 * time. */
e85d0918
DD
1117}
1118
5cf6cf81 1119static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble)
e85d0918 1120{
459c51ad 1121 mutex_lock(&mac->chip.mutex);
459c51ad
DD
1122 zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
1123 mutex_unlock(&mac->chip.mutex);
e85d0918
DD
1124}
1125
471b3efd
JB
1126static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
1127 struct ieee80211_vif *vif,
1128 struct ieee80211_bss_conf *bss_conf,
1129 u32 changes)
e85d0918 1130{
459c51ad 1131 struct zd_mac *mac = zd_hw_mac(hw);
2d0ddec5 1132 int associated;
459c51ad
DD
1133
1134 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
1135
2d0ddec5
JB
1136 if (mac->type == NL80211_IFTYPE_MESH_POINT ||
1137 mac->type == NL80211_IFTYPE_ADHOC) {
1138 associated = true;
1139 if (changes & BSS_CHANGED_BEACON) {
1140 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
1141
1142 if (beacon) {
9be23256 1143 zd_chip_disable_hwint(&mac->chip);
2d0ddec5 1144 zd_mac_config_beacon(hw, beacon);
9be23256 1145 zd_chip_enable_hwint(&mac->chip);
2d0ddec5
JB
1146 kfree_skb(beacon);
1147 }
1148 }
1149
1150 if (changes & BSS_CHANGED_BEACON_ENABLED) {
b91a515d
JK
1151 u16 interval = 0;
1152 u8 period = 0;
2d0ddec5 1153
b91a515d
JK
1154 if (bss_conf->enable_beacon) {
1155 period = bss_conf->dtim_period;
1156 interval = bss_conf->beacon_int;
1157 }
2d0ddec5 1158
9be23256
JK
1159 spin_lock_irq(&mac->lock);
1160 mac->beacon.period = period;
1161 mac->beacon.interval = interval;
1162 mac->beacon.last_update = jiffies;
1163 spin_unlock_irq(&mac->lock);
1164
b91a515d
JK
1165 zd_set_beacon_interval(&mac->chip, interval, period,
1166 mac->type);
2d0ddec5
JB
1167 }
1168 } else
1169 associated = is_valid_ether_addr(bss_conf->bssid);
1170
1171 spin_lock_irq(&mac->lock);
1172 mac->associated = associated;
1173 spin_unlock_irq(&mac->lock);
1174
1175 /* TODO: do hardware bssid filtering */
1176
471b3efd 1177 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
5cf6cf81 1178 spin_lock_irq(&mac->lock);
471b3efd 1179 mac->short_preamble = bss_conf->use_short_preamble;
5cf6cf81
JK
1180 spin_unlock_irq(&mac->lock);
1181
1182 set_rts_cts(mac, bss_conf->use_short_preamble);
459c51ad 1183 }
e85d0918
DD
1184}
1185
5fe73197
AF
1186static u64 zd_op_get_tsf(struct ieee80211_hw *hw)
1187{
1188 struct zd_mac *mac = zd_hw_mac(hw);
1189 return zd_chip_get_tsf(&mac->chip);
1190}
1191
459c51ad
DD
1192static const struct ieee80211_ops zd_ops = {
1193 .tx = zd_op_tx,
1194 .start = zd_op_start,
1195 .stop = zd_op_stop,
1196 .add_interface = zd_op_add_interface,
1197 .remove_interface = zd_op_remove_interface,
1198 .config = zd_op_config,
3ac64bee 1199 .prepare_multicast = zd_op_prepare_multicast,
459c51ad 1200 .configure_filter = zd_op_configure_filter,
471b3efd 1201 .bss_info_changed = zd_op_bss_info_changed,
5fe73197 1202 .get_tsf = zd_op_get_tsf,
459c51ad
DD
1203};
1204
1205struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
e85d0918 1206{
459c51ad
DD
1207 struct zd_mac *mac;
1208 struct ieee80211_hw *hw;
e85d0918 1209
459c51ad
DD
1210 hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
1211 if (!hw) {
1212 dev_dbg_f(&intf->dev, "out of memory\n");
1213 return NULL;
db888aed 1214 }
459c51ad
DD
1215
1216 mac = zd_hw_mac(hw);
1217
1218 memset(mac, 0, sizeof(*mac));
1219 spin_lock_init(&mac->lock);
1220 mac->hw = hw;
1221
05c914fe 1222 mac->type = NL80211_IFTYPE_UNSPECIFIED;
459c51ad
DD
1223
1224 memcpy(mac->channels, zd_channels, sizeof(zd_channels));
1225 memcpy(mac->rates, zd_rates, sizeof(zd_rates));
8318d78a
JB
1226 mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
1227 mac->band.bitrates = mac->rates;
1228 mac->band.n_channels = ARRAY_SIZE(zd_channels);
1229 mac->band.channels = mac->channels;
1230
1231 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
1232
72e77a8a 1233 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
4099e2f4
JK
1234 IEEE80211_HW_SIGNAL_UNSPEC |
1235 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
459c51ad 1236
f59ac048
LR
1237 hw->wiphy->interface_modes =
1238 BIT(NL80211_IFTYPE_MESH_POINT) |
1239 BIT(NL80211_IFTYPE_STATION) |
1240 BIT(NL80211_IFTYPE_ADHOC);
1241
566bfe5a 1242 hw->max_signal = 100;
459c51ad
DD
1243 hw->queues = 1;
1244 hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
1245
7f4013f0
BP
1246 /*
1247 * Tell mac80211 that we support multi rate retries
1248 */
1249 hw->max_rates = IEEE80211_TX_MAX_RATES;
1250 hw->max_rate_tries = 18; /* 9 rates * 2 retries/rate */
1251
459c51ad 1252 skb_queue_head_init(&mac->ack_wait_queue);
7f4013f0 1253 mac->ack_pending = 0;
459c51ad 1254
459c51ad
DD
1255 zd_chip_init(&mac->chip, hw, intf);
1256 housekeeping_init(mac);
9be23256 1257 beacon_init(mac);
72e77a8a 1258 INIT_WORK(&mac->process_intr, zd_process_intr);
459c51ad
DD
1259
1260 SET_IEEE80211_DEV(hw, &intf->dev);
1261 return hw;
e85d0918
DD
1262}
1263
9be23256
JK
1264#define BEACON_WATCHDOG_DELAY round_jiffies_relative(HZ)
1265
1266static void beacon_watchdog_handler(struct work_struct *work)
1267{
1268 struct zd_mac *mac =
1269 container_of(work, struct zd_mac, beacon.watchdog_work.work);
1270 struct sk_buff *beacon;
1271 unsigned long timeout;
1272 int interval, period;
1273
1274 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1275 goto rearm;
1276 if (mac->type != NL80211_IFTYPE_AP || !mac->vif)
1277 goto rearm;
1278
1279 spin_lock_irq(&mac->lock);
1280 interval = mac->beacon.interval;
1281 period = mac->beacon.period;
1282 timeout = mac->beacon.last_update + msecs_to_jiffies(interval) + HZ;
1283 spin_unlock_irq(&mac->lock);
1284
1285 if (interval > 0 && time_is_before_jiffies(timeout)) {
1286 dev_dbg_f(zd_mac_dev(mac), "beacon interrupt stalled, "
1287 "restarting. "
1288 "(interval: %d, dtim: %d)\n",
1289 interval, period);
1290
1291 zd_chip_disable_hwint(&mac->chip);
1292
1293 beacon = ieee80211_beacon_get(mac->hw, mac->vif);
1294 if (beacon) {
1295 zd_mac_config_beacon(mac->hw, beacon);
1296 kfree_skb(beacon);
1297 }
1298
1299 zd_set_beacon_interval(&mac->chip, interval, period, mac->type);
1300
1301 zd_chip_enable_hwint(&mac->chip);
1302
1303 spin_lock_irq(&mac->lock);
1304 mac->beacon.last_update = jiffies;
1305 spin_unlock_irq(&mac->lock);
1306 }
1307
1308rearm:
1309 queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
1310 BEACON_WATCHDOG_DELAY);
1311}
1312
1313static void beacon_init(struct zd_mac *mac)
1314{
1315 INIT_DELAYED_WORK(&mac->beacon.watchdog_work, beacon_watchdog_handler);
1316}
1317
1318static void beacon_enable(struct zd_mac *mac)
1319{
1320 dev_dbg_f(zd_mac_dev(mac), "\n");
1321
1322 mac->beacon.last_update = jiffies;
1323 queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
1324 BEACON_WATCHDOG_DELAY);
1325}
1326
1327static void beacon_disable(struct zd_mac *mac)
1328{
1329 dev_dbg_f(zd_mac_dev(mac), "\n");
1330 cancel_delayed_work_sync(&mac->beacon.watchdog_work);
1331}
1332
583afd1e
UK
1333#define LINK_LED_WORK_DELAY HZ
1334
c4028958 1335static void link_led_handler(struct work_struct *work)
583afd1e 1336{
c4028958
DH
1337 struct zd_mac *mac =
1338 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
583afd1e 1339 struct zd_chip *chip = &mac->chip;
583afd1e
UK
1340 int is_associated;
1341 int r;
1342
1343 spin_lock_irq(&mac->lock);
459c51ad 1344 is_associated = mac->associated;
583afd1e
UK
1345 spin_unlock_irq(&mac->lock);
1346
1347 r = zd_chip_control_leds(chip,
14b46c8a 1348 is_associated ? ZD_LED_ASSOCIATED : ZD_LED_SCANNING);
583afd1e 1349 if (r)
459c51ad 1350 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
583afd1e
UK
1351
1352 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1353 LINK_LED_WORK_DELAY);
1354}
1355
1356static void housekeeping_init(struct zd_mac *mac)
1357{
c4028958 1358 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
583afd1e
UK
1359}
1360
1361static void housekeeping_enable(struct zd_mac *mac)
1362{
1363 dev_dbg_f(zd_mac_dev(mac), "\n");
1364 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1365 0);
1366}
1367
1368static void housekeeping_disable(struct zd_mac *mac)
1369{
1370 dev_dbg_f(zd_mac_dev(mac), "\n");
afe2c511 1371 cancel_delayed_work_sync(&mac->housekeeping.link_led_work);
14b46c8a 1372 zd_chip_control_leds(&mac->chip, ZD_LED_OFF);
583afd1e 1373}