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