blob: eff79a37bc2aa2faa5ae35891dd84de560ee8da3 [file] [log] [blame]
Daniel Drake66bb42f2007-11-19 16:20:12 +00001/* ZD1211 USB-WLAN driver for Linux
Daniel Drakee85d0912006-06-02 17:11:32 +01002 *
Daniel Drake66bb42f2007-11-19 16:20:12 +00003 * 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>
Luis R. Rodrigueze83a1072008-09-09 23:19:49 -07006 * Copyright (C) 2007-2008 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
Daniel Drake459c51a2007-11-19 15:00:29 +00007 *
Daniel Drakee85d0912006-06-02 17:11:32 +01008 * 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
Jeff Kirsherf47cdae2013-12-06 03:32:10 -080019 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Daniel Drakee85d0912006-06-02 17:11:32 +010020 */
21
22#include <linux/netdevice.h>
23#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Daniel Drakee85d0912006-06-02 17:11:32 +010025#include <linux/usb.h>
26#include <linux/jiffies.h>
27#include <net/ieee80211_radiotap.h>
28
29#include "zd_def.h"
30#include "zd_chip.h"
31#include "zd_mac.h"
Daniel Drakee85d0912006-06-02 17:11:32 +010032#include "zd_rf.h"
Daniel Drakee85d0912006-06-02 17:11:32 +010033
Luis R. Rodrigueze83a1072008-09-09 23:19:49 -070034struct zd_reg_alpha2_map {
35 u32 reg;
36 char alpha2[2];
37};
38
39static struct zd_reg_alpha2_map reg_alpha2_map[] = {
40 { ZD_REGDOMAIN_FCC, "US" },
41 { ZD_REGDOMAIN_IC, "CA" },
42 { ZD_REGDOMAIN_ETSI, "DE" }, /* Generic ETSI, use most restrictive */
43 { ZD_REGDOMAIN_JAPAN, "JP" },
John W. Linville3d3b33b2010-06-16 14:41:36 -040044 { ZD_REGDOMAIN_JAPAN_2, "JP" },
45 { ZD_REGDOMAIN_JAPAN_3, "JP" },
Luis R. Rodrigueze83a1072008-09-09 23:19:49 -070046 { ZD_REGDOMAIN_SPAIN, "ES" },
47 { ZD_REGDOMAIN_FRANCE, "FR" },
48};
49
Daniel Drake459c51a2007-11-19 15:00:29 +000050/* This table contains the hardware specific values for the modulation rates. */
51static const struct ieee80211_rate zd_rates[] = {
Johannes Berg8318d782008-01-24 19:38:38 +010052 { .bitrate = 10,
53 .hw_value = ZD_CCK_RATE_1M, },
54 { .bitrate = 20,
55 .hw_value = ZD_CCK_RATE_2M,
56 .hw_value_short = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
57 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
58 { .bitrate = 55,
59 .hw_value = ZD_CCK_RATE_5_5M,
60 .hw_value_short = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
61 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
62 { .bitrate = 110,
63 .hw_value = ZD_CCK_RATE_11M,
64 .hw_value_short = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
65 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
66 { .bitrate = 60,
67 .hw_value = ZD_OFDM_RATE_6M,
68 .flags = 0 },
69 { .bitrate = 90,
70 .hw_value = ZD_OFDM_RATE_9M,
71 .flags = 0 },
72 { .bitrate = 120,
73 .hw_value = ZD_OFDM_RATE_12M,
74 .flags = 0 },
75 { .bitrate = 180,
76 .hw_value = ZD_OFDM_RATE_18M,
77 .flags = 0 },
78 { .bitrate = 240,
79 .hw_value = ZD_OFDM_RATE_24M,
80 .flags = 0 },
81 { .bitrate = 360,
82 .hw_value = ZD_OFDM_RATE_36M,
83 .flags = 0 },
84 { .bitrate = 480,
85 .hw_value = ZD_OFDM_RATE_48M,
86 .flags = 0 },
87 { .bitrate = 540,
88 .hw_value = ZD_OFDM_RATE_54M,
89 .flags = 0 },
Daniel Drake459c51a2007-11-19 15:00:29 +000090};
91
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +020092/*
93 * Zydas retry rates table. Each line is listed in the same order as
94 * in zd_rates[] and contains all the rate used when a packet is sent
95 * starting with a given rates. Let's consider an example :
96 *
97 * "11 Mbits : 4, 3, 2, 1, 0" means :
98 * - packet is sent using 4 different rates
99 * - 1st rate is index 3 (ie 11 Mbits)
100 * - 2nd rate is index 2 (ie 5.5 Mbits)
101 * - 3rd rate is index 1 (ie 2 Mbits)
102 * - 4th rate is index 0 (ie 1 Mbits)
103 */
104
105static const struct tx_retry_rate zd_retry_rates[] = {
106 { /* 1 Mbits */ 1, { 0 }},
107 { /* 2 Mbits */ 2, { 1, 0 }},
108 { /* 5.5 Mbits */ 3, { 2, 1, 0 }},
109 { /* 11 Mbits */ 4, { 3, 2, 1, 0 }},
110 { /* 6 Mbits */ 5, { 4, 3, 2, 1, 0 }},
111 { /* 9 Mbits */ 6, { 5, 4, 3, 2, 1, 0}},
112 { /* 12 Mbits */ 5, { 6, 3, 2, 1, 0 }},
113 { /* 18 Mbits */ 6, { 7, 6, 3, 2, 1, 0 }},
114 { /* 24 Mbits */ 6, { 8, 6, 3, 2, 1, 0 }},
115 { /* 36 Mbits */ 7, { 9, 8, 6, 3, 2, 1, 0 }},
116 { /* 48 Mbits */ 8, {10, 9, 8, 6, 3, 2, 1, 0 }},
117 { /* 54 Mbits */ 9, {11, 10, 9, 8, 6, 3, 2, 1, 0 }}
118};
119
Daniel Drake459c51a2007-11-19 15:00:29 +0000120static const struct ieee80211_channel zd_channels[] = {
Johannes Berg8318d782008-01-24 19:38:38 +0100121 { .center_freq = 2412, .hw_value = 1 },
122 { .center_freq = 2417, .hw_value = 2 },
123 { .center_freq = 2422, .hw_value = 3 },
124 { .center_freq = 2427, .hw_value = 4 },
125 { .center_freq = 2432, .hw_value = 5 },
126 { .center_freq = 2437, .hw_value = 6 },
127 { .center_freq = 2442, .hw_value = 7 },
128 { .center_freq = 2447, .hw_value = 8 },
129 { .center_freq = 2452, .hw_value = 9 },
130 { .center_freq = 2457, .hw_value = 10 },
131 { .center_freq = 2462, .hw_value = 11 },
132 { .center_freq = 2467, .hw_value = 12 },
133 { .center_freq = 2472, .hw_value = 13 },
134 { .center_freq = 2484, .hw_value = 14 },
Daniel Drake459c51a2007-11-19 15:00:29 +0000135};
Daniel Drakee85d0912006-06-02 17:11:32 +0100136
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100137static void housekeeping_init(struct zd_mac *mac);
138static void housekeeping_enable(struct zd_mac *mac);
139static void housekeeping_disable(struct zd_mac *mac);
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200140static void beacon_init(struct zd_mac *mac);
141static void beacon_enable(struct zd_mac *mac);
142static void beacon_disable(struct zd_mac *mac);
Jussi Kivilinna212e1a52011-01-31 20:49:43 +0200143static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble);
144static int zd_mac_config_beacon(struct ieee80211_hw *hw,
Jussi Kivilinnadde46732011-06-20 14:42:49 +0300145 struct sk_buff *beacon, bool in_intr);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100146
Luis R. Rodrigueze83a1072008-09-09 23:19:49 -0700147static int zd_reg2alpha2(u8 regdomain, char *alpha2)
148{
149 unsigned int i;
150 struct zd_reg_alpha2_map *reg_map;
151 for (i = 0; i < ARRAY_SIZE(reg_alpha2_map); i++) {
152 reg_map = &reg_alpha2_map[i];
153 if (regdomain == reg_map->reg) {
154 alpha2[0] = reg_map->alpha2[0];
155 alpha2[1] = reg_map->alpha2[1];
156 return 0;
157 }
158 }
159 return 1;
160}
161
Jussi Kivilinna7a1d6562011-06-20 14:42:28 +0300162static int zd_check_signal(struct ieee80211_hw *hw, int signal)
163{
164 struct zd_mac *mac = zd_hw_mac(hw);
165
166 dev_dbg_f_cond(zd_mac_dev(mac), signal < 0 || signal > 100,
167 "%s: signal value from device not in range 0..100, "
168 "but %d.\n", __func__, signal);
169
170 if (signal < 0)
171 signal = 0;
172 else if (signal > 100)
173 signal = 100;
174
175 return signal;
176}
177
Daniel Drake459c51a2007-11-19 15:00:29 +0000178int zd_mac_preinit_hw(struct ieee80211_hw *hw)
Daniel Drake74553ae2007-07-01 18:22:32 +0100179{
180 int r;
181 u8 addr[ETH_ALEN];
Daniel Drake459c51a2007-11-19 15:00:29 +0000182 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drake74553ae2007-07-01 18:22:32 +0100183
184 r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
185 if (r)
186 return r;
187
Daniel Drake459c51a2007-11-19 15:00:29 +0000188 SET_IEEE80211_PERM_ADDR(hw, addr);
189
Daniel Drake74553ae2007-07-01 18:22:32 +0100190 return 0;
191}
192
Daniel Drake459c51a2007-11-19 15:00:29 +0000193int zd_mac_init_hw(struct ieee80211_hw *hw)
Daniel Drakee85d0912006-06-02 17:11:32 +0100194{
195 int r;
Daniel Drake459c51a2007-11-19 15:00:29 +0000196 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100197 struct zd_chip *chip = &mac->chip;
Luis R. Rodrigueze83a1072008-09-09 23:19:49 -0700198 char alpha2[2];
Daniel Drakee85d0912006-06-02 17:11:32 +0100199 u8 default_regdomain;
200
201 r = zd_chip_enable_int(chip);
202 if (r)
203 goto out;
Daniel Drake74553ae2007-07-01 18:22:32 +0100204 r = zd_chip_init_hw(chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100205 if (r)
206 goto disable_int;
207
Daniel Drakee85d0912006-06-02 17:11:32 +0100208 ZD_ASSERT(!irqs_disabled());
Daniel Drakee85d0912006-06-02 17:11:32 +0100209
210 r = zd_read_regdomain(chip, &default_regdomain);
211 if (r)
212 goto disable_int;
Daniel Drakee85d0912006-06-02 17:11:32 +0100213 spin_lock_irq(&mac->lock);
214 mac->regdomain = mac->default_regdomain = default_regdomain;
215 spin_unlock_irq(&mac->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +0100216
Daniel Drake40da08b2006-08-01 23:43:32 +0200217 /* We must inform the device that we are doing encryption/decryption in
218 * software at the moment. */
219 r = zd_set_encryption_type(chip, ENC_SNIFFER);
Daniel Drakee85d0912006-06-02 17:11:32 +0100220 if (r)
221 goto disable_int;
222
Luis R. Rodrigueze83a1072008-09-09 23:19:49 -0700223 r = zd_reg2alpha2(mac->regdomain, alpha2);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500224 if (r)
225 goto disable_int;
Daniel Drakee85d0912006-06-02 17:11:32 +0100226
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500227 r = regulatory_hint(hw->wiphy, alpha2);
Daniel Drakee85d0912006-06-02 17:11:32 +0100228disable_int:
229 zd_chip_disable_int(chip);
230out:
231 return r;
232}
233
234void zd_mac_clear(struct zd_mac *mac)
235{
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000236 flush_workqueue(zd_workqueue);
Daniel Drakee85d0912006-06-02 17:11:32 +0100237 zd_chip_clear(&mac->chip);
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100238 ZD_ASSERT(!spin_is_locked(&mac->lock));
239 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
Daniel Drakee85d0912006-06-02 17:11:32 +0100240}
241
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100242static int set_rx_filter(struct zd_mac *mac)
Daniel Drakee85d0912006-06-02 17:11:32 +0100243{
Daniel Drake459c51a2007-11-19 15:00:29 +0000244 unsigned long flags;
245 u32 filter = STA_RX_FILTER;
Daniel Drakee85d0912006-06-02 17:11:32 +0100246
Daniel Drake459c51a2007-11-19 15:00:29 +0000247 spin_lock_irqsave(&mac->lock, flags);
248 if (mac->pass_ctrl)
249 filter |= RX_FILTER_CTRL;
250 spin_unlock_irqrestore(&mac->lock, flags);
251
252 return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100253}
254
Jussi Kivilinnac2fadcb2011-01-31 20:48:06 +0200255static int set_mac_and_bssid(struct zd_mac *mac)
256{
257 int r;
258
259 if (!mac->vif)
260 return -1;
261
262 r = zd_write_mac_addr(&mac->chip, mac->vif->addr);
263 if (r)
264 return r;
265
266 /* Vendor driver after setting MAC either sets BSSID for AP or
267 * filter for other modes.
268 */
269 if (mac->type != NL80211_IFTYPE_AP)
270 return set_rx_filter(mac);
271 else
272 return zd_write_bssid(&mac->chip, mac->vif->addr);
273}
274
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100275static int set_mc_hash(struct zd_mac *mac)
276{
277 struct zd_mc_hash hash;
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100278 zd_mc_clear(&hash);
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100279 return zd_chip_set_multicast_hash(&mac->chip, &hash);
280}
281
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +0200282int zd_op_start(struct ieee80211_hw *hw)
Daniel Drakee85d0912006-06-02 17:11:32 +0100283{
Daniel Drake459c51a2007-11-19 15:00:29 +0000284 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100285 struct zd_chip *chip = &mac->chip;
Daniel Drake74553ae2007-07-01 18:22:32 +0100286 struct zd_usb *usb = &chip->usb;
Daniel Drakee85d0912006-06-02 17:11:32 +0100287 int r;
288
Daniel Drake74553ae2007-07-01 18:22:32 +0100289 if (!usb->initialized) {
290 r = zd_usb_init_hw(usb);
291 if (r)
292 goto out;
293 }
294
Daniel Drakee85d0912006-06-02 17:11:32 +0100295 r = zd_chip_enable_int(chip);
296 if (r < 0)
297 goto out;
298
299 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
300 if (r < 0)
301 goto disable_int;
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100302 r = set_rx_filter(mac);
303 if (r)
304 goto disable_int;
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100305 r = set_mc_hash(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100306 if (r)
307 goto disable_int;
Florian Fainelli02f14342012-02-29 14:00:13 +0100308
309 /* Wait after setting the multicast hash table and powering on
310 * the radio otherwise interface bring up will fail. This matches
311 * what the vendor driver did.
312 */
313 msleep(10);
314
Daniel Drakee85d0912006-06-02 17:11:32 +0100315 r = zd_chip_switch_radio_on(chip);
Florian Fainelli02f14342012-02-29 14:00:13 +0100316 if (r < 0) {
317 dev_err(zd_chip_dev(chip),
318 "%s: failed to set radio on\n", __func__);
Daniel Drakee85d0912006-06-02 17:11:32 +0100319 goto disable_int;
Florian Fainelli02f14342012-02-29 14:00:13 +0100320 }
Daniel Drake459c51a2007-11-19 15:00:29 +0000321 r = zd_chip_enable_rxtx(chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100322 if (r < 0)
323 goto disable_radio;
324 r = zd_chip_enable_hwint(chip);
325 if (r < 0)
Daniel Drake459c51a2007-11-19 15:00:29 +0000326 goto disable_rxtx;
Daniel Drakee85d0912006-06-02 17:11:32 +0100327
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100328 housekeeping_enable(mac);
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200329 beacon_enable(mac);
330 set_bit(ZD_DEVICE_RUNNING, &mac->flags);
Daniel Drakee85d0912006-06-02 17:11:32 +0100331 return 0;
Daniel Drake459c51a2007-11-19 15:00:29 +0000332disable_rxtx:
333 zd_chip_disable_rxtx(chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100334disable_radio:
335 zd_chip_switch_radio_off(chip);
336disable_int:
337 zd_chip_disable_int(chip);
338out:
339 return r;
340}
341
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +0200342void zd_op_stop(struct ieee80211_hw *hw)
Daniel Drake459c51a2007-11-19 15:00:29 +0000343{
344 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100345 struct zd_chip *chip = &mac->chip;
Daniel Drake459c51a2007-11-19 15:00:29 +0000346 struct sk_buff *skb;
347 struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
Daniel Drakee85d0912006-06-02 17:11:32 +0100348
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200349 clear_bit(ZD_DEVICE_RUNNING, &mac->flags);
350
Daniel Drake459c51a2007-11-19 15:00:29 +0000351 /* The order here deliberately is a little different from the open()
Daniel Drakee85d0912006-06-02 17:11:32 +0100352 * method, since we need to make sure there is no opportunity for RX
Daniel Drake459c51a2007-11-19 15:00:29 +0000353 * frames to be processed by mac80211 after we have stopped it.
Daniel Drakee85d0912006-06-02 17:11:32 +0100354 */
355
Daniel Drake459c51a2007-11-19 15:00:29 +0000356 zd_chip_disable_rxtx(chip);
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200357 beacon_disable(mac);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100358 housekeeping_disable(mac);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000359 flush_workqueue(zd_workqueue);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000360
Daniel Drakee85d0912006-06-02 17:11:32 +0100361 zd_chip_disable_hwint(chip);
362 zd_chip_switch_radio_off(chip);
363 zd_chip_disable_int(chip);
364
Daniel Drake459c51a2007-11-19 15:00:29 +0000365
366 while ((skb = skb_dequeue(ack_wait_queue)))
Johannes Berge039fa42008-05-15 12:55:29 +0200367 dev_kfree_skb_any(skb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100368}
369
Jussi Kivilinna212e1a52011-01-31 20:49:43 +0200370int zd_restore_settings(struct zd_mac *mac)
371{
372 struct sk_buff *beacon;
373 struct zd_mc_hash multicast_hash;
374 unsigned int short_preamble;
375 int r, beacon_interval, beacon_period;
376 u8 channel;
377
378 dev_dbg_f(zd_mac_dev(mac), "\n");
379
380 spin_lock_irq(&mac->lock);
381 multicast_hash = mac->multicast_hash;
382 short_preamble = mac->short_preamble;
383 beacon_interval = mac->beacon.interval;
384 beacon_period = mac->beacon.period;
385 channel = mac->channel;
386 spin_unlock_irq(&mac->lock);
387
388 r = set_mac_and_bssid(mac);
389 if (r < 0) {
390 dev_dbg_f(zd_mac_dev(mac), "set_mac_and_bssid failed, %d\n", r);
391 return r;
392 }
393
394 r = zd_chip_set_channel(&mac->chip, channel);
395 if (r < 0) {
396 dev_dbg_f(zd_mac_dev(mac), "zd_chip_set_channel failed, %d\n",
397 r);
398 return r;
399 }
400
401 set_rts_cts(mac, short_preamble);
402
403 r = zd_chip_set_multicast_hash(&mac->chip, &multicast_hash);
404 if (r < 0) {
405 dev_dbg_f(zd_mac_dev(mac),
406 "zd_chip_set_multicast_hash failed, %d\n", r);
407 return r;
408 }
409
410 if (mac->type == NL80211_IFTYPE_MESH_POINT ||
411 mac->type == NL80211_IFTYPE_ADHOC ||
412 mac->type == NL80211_IFTYPE_AP) {
413 if (mac->vif != NULL) {
414 beacon = ieee80211_beacon_get(mac->hw, mac->vif);
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +0300415 if (beacon)
Jussi Kivilinnadde46732011-06-20 14:42:49 +0300416 zd_mac_config_beacon(mac->hw, beacon, false);
Jussi Kivilinna212e1a52011-01-31 20:49:43 +0200417 }
418
419 zd_set_beacon_interval(&mac->chip, beacon_interval,
420 beacon_period, mac->type);
421
422 spin_lock_irq(&mac->lock);
423 mac->beacon.last_update = jiffies;
424 spin_unlock_irq(&mac->lock);
425 }
426
427 return 0;
428}
429
Daniel Drake459c51a2007-11-19 15:00:29 +0000430/**
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200431 * zd_mac_tx_status - reports tx status of a packet if required
Daniel Drake459c51a2007-11-19 15:00:29 +0000432 * @hw - a &struct ieee80211_hw pointer
433 * @skb - a sk-buffer
Johannes Berge039fa42008-05-15 12:55:29 +0200434 * @flags: extra flags to set in the TX status info
435 * @ackssi: ACK signal strength
Coly Li73ac36e2009-01-07 18:09:16 -0800436 * @success - True for successful transmission of the frame
Daniel Drake459c51a2007-11-19 15:00:29 +0000437 *
438 * This information calls ieee80211_tx_status_irqsafe() if required by the
439 * control information. It copies the control information into the status
440 * information.
441 *
442 * If no status information has been requested, the skb is freed.
443 */
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200444static void zd_mac_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
445 int ackssi, struct tx_status *tx_status)
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000446{
Johannes Berge039fa42008-05-15 12:55:29 +0200447 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200448 int i;
449 int success = 1, retry = 1;
450 int first_idx;
451 const struct tx_retry_rate *retries;
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000452
Johannes Berge6a98542008-10-21 12:40:02 +0200453 ieee80211_tx_info_clear_status(info);
Johannes Berge039fa42008-05-15 12:55:29 +0200454
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200455 if (tx_status) {
456 success = !tx_status->failure;
457 retry = tx_status->retry + success;
458 }
459
460 if (success) {
461 /* success */
Johannes Berge6a98542008-10-21 12:40:02 +0200462 info->flags |= IEEE80211_TX_STAT_ACK;
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200463 } else {
464 /* failure */
465 info->flags &= ~IEEE80211_TX_STAT_ACK;
466 }
467
468 first_idx = info->status.rates[0].idx;
469 ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
470 retries = &zd_retry_rates[first_idx];
Dan Carpenter86baf712010-02-27 09:12:34 +0300471 ZD_ASSERT(1 <= retry && retry <= retries->count);
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200472
473 info->status.rates[0].idx = retries->rate[0];
474 info->status.rates[0].count = 1; // (retry > 1 ? 2 : 1);
475
476 for (i=1; i<IEEE80211_TX_MAX_RATES-1 && i<retry; i++) {
477 info->status.rates[i].idx = retries->rate[i];
478 info->status.rates[i].count = 1; // ((i==retry-1) && success ? 1:2);
479 }
480 for (; i<IEEE80211_TX_MAX_RATES && i<retry; i++) {
Dan Carpenter86baf712010-02-27 09:12:34 +0300481 info->status.rates[i].idx = retries->rate[retry - 1];
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200482 info->status.rates[i].count = 1; // (success ? 1:2);
483 }
484 if (i<IEEE80211_TX_MAX_RATES)
485 info->status.rates[i].idx = -1; /* terminate */
486
Jussi Kivilinna7a1d6562011-06-20 14:42:28 +0300487 info->status.ack_signal = zd_check_signal(hw, ackssi);
Johannes Berge039fa42008-05-15 12:55:29 +0200488 ieee80211_tx_status_irqsafe(hw, skb);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000489}
490
Daniel Drake459c51a2007-11-19 15:00:29 +0000491/**
492 * zd_mac_tx_failed - callback for failed frames
493 * @dev: the mac80211 wireless device
494 *
Adam Buchbinder303863f2009-12-11 16:35:37 -0500495 * This function is called if a frame couldn't be successfully
Daniel Drake459c51a2007-11-19 15:00:29 +0000496 * transferred. The first frame from the tx queue, will be selected and
497 * reported as error to the upper layers.
498 */
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200499void zd_mac_tx_failed(struct urb *urb)
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000500{
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200501 struct ieee80211_hw * hw = zd_usb_to_hw(urb->context);
502 struct zd_mac *mac = zd_hw_mac(hw);
503 struct sk_buff_head *q = &mac->ack_wait_queue;
Daniel Drake459c51a2007-11-19 15:00:29 +0000504 struct sk_buff *skb;
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200505 struct tx_status *tx_status = (struct tx_status *)urb->transfer_buffer;
506 unsigned long flags;
507 int success = !tx_status->failure;
508 int retry = tx_status->retry + success;
509 int found = 0;
510 int i, position = 0;
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000511
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200512 q = &mac->ack_wait_queue;
513 spin_lock_irqsave(&q->lock, flags);
Johannes Berg5078ed52008-02-20 12:09:25 +0100514
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200515 skb_queue_walk(q, skb) {
516 struct ieee80211_hdr *tx_hdr;
517 struct ieee80211_tx_info *info;
518 int first_idx, final_idx;
519 const struct tx_retry_rate *retries;
520 u8 final_rate;
521
522 position ++;
523
524 /* if the hardware reports a failure and we had a 802.11 ACK
525 * pending, then we skip the first skb when searching for a
526 * matching frame */
527 if (tx_status->failure && mac->ack_pending &&
528 skb_queue_is_first(q, skb)) {
529 continue;
530 }
531
532 tx_hdr = (struct ieee80211_hdr *)skb->data;
533
534 /* we skip all frames not matching the reported destination */
535 if (unlikely(memcmp(tx_hdr->addr1, tx_status->mac, ETH_ALEN))) {
536 continue;
537 }
538
539 /* we skip all frames not matching the reported final rate */
540
541 info = IEEE80211_SKB_CB(skb);
542 first_idx = info->status.rates[0].idx;
543 ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
544 retries = &zd_retry_rates[first_idx];
Dan Carpenter86baf712010-02-27 09:12:34 +0300545 if (retry <= 0 || retry > retries->count)
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200546 continue;
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200547
Dan Carpenter86baf712010-02-27 09:12:34 +0300548 final_idx = retries->rate[retry - 1];
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200549 final_rate = zd_rates[final_idx].hw_value;
550
551 if (final_rate != tx_status->rate) {
552 continue;
553 }
554
555 found = 1;
556 break;
557 }
558
559 if (found) {
560 for (i=1; i<=position; i++) {
561 skb = __skb_dequeue(q);
562 zd_mac_tx_status(hw, skb,
563 mac->ack_pending ? mac->ack_signal : 0,
564 i == position ? tx_status : NULL);
565 mac->ack_pending = 0;
566 }
567 }
568
569 spin_unlock_irqrestore(&q->lock, flags);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000570}
571
Daniel Drake459c51a2007-11-19 15:00:29 +0000572/**
573 * zd_mac_tx_to_dev - callback for USB layer
574 * @skb: a &sk_buff pointer
575 * @error: error value, 0 if transmission successful
576 *
577 * Informs the MAC layer that the frame has successfully transferred to the
578 * device. If an ACK is required and the transfer to the device has been
579 * successful, the packets are put on the @ack_wait_queue with
580 * the control set removed.
581 */
582void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
Daniel Drakee85d0912006-06-02 17:11:32 +0100583{
Johannes Berge039fa42008-05-15 12:55:29 +0200584 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200585 struct ieee80211_hw *hw = info->rate_driver_data[0];
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200586 struct zd_mac *mac = zd_hw_mac(hw);
587
588 ieee80211_tx_info_clear_status(info);
Daniel Drakee85d0912006-06-02 17:11:32 +0100589
Johannes Berge039fa42008-05-15 12:55:29 +0200590 skb_pull(skb, sizeof(struct zd_ctrlset));
591 if (unlikely(error ||
592 (info->flags & IEEE80211_TX_CTL_NO_ACK))) {
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200593 /*
594 * FIXME : do we need to fill in anything ?
595 */
596 ieee80211_tx_status_irqsafe(hw, skb);
Daniel Drake459c51a2007-11-19 15:00:29 +0000597 } else {
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200598 struct sk_buff_head *q = &mac->ack_wait_queue;
Johannes Berge039fa42008-05-15 12:55:29 +0200599
600 skb_queue_tail(q, skb);
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200601 while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS) {
602 zd_mac_tx_status(hw, skb_dequeue(q),
603 mac->ack_pending ? mac->ack_signal : 0,
604 NULL);
605 mac->ack_pending = 0;
606 }
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000607 }
Daniel Drakee85d0912006-06-02 17:11:32 +0100608}
609
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000610static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
Daniel Drakee85d0912006-06-02 17:11:32 +0100611{
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100612 /* ZD_PURE_RATE() must be used to remove the modulation type flag of
Daniel Drake459c51a2007-11-19 15:00:29 +0000613 * the zd-rate values.
614 */
Daniel Drakee85d0912006-06-02 17:11:32 +0100615 static const u8 rate_divisor[] = {
Daniel Drake459c51a2007-11-19 15:00:29 +0000616 [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
617 [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
618 /* Bits must be doubled. */
619 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
620 [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
621 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
622 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
623 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
624 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
625 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
626 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
627 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
628 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
Daniel Drakee85d0912006-06-02 17:11:32 +0100629 };
630
631 u32 bits = (u32)tx_length * 8;
632 u32 divisor;
633
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100634 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
Daniel Drakee85d0912006-06-02 17:11:32 +0100635 if (divisor == 0)
636 return -EINVAL;
637
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000638 switch (zd_rate) {
639 case ZD_CCK_RATE_5_5M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100640 bits = (2*bits) + 10; /* round up to the next integer */
641 break;
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000642 case ZD_CCK_RATE_11M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100643 if (service) {
644 u32 t = bits % 11;
645 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
646 if (0 < t && t <= 3) {
647 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
648 }
649 }
650 bits += 10; /* round up to the next integer */
651 break;
652 }
653
654 return bits/divisor;
655}
656
Daniel Drakee85d0912006-06-02 17:11:32 +0100657static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
Johannes Berge6a98542008-10-21 12:40:02 +0200658 struct ieee80211_hdr *header,
659 struct ieee80211_tx_info *info)
Daniel Drakee85d0912006-06-02 17:11:32 +0100660{
Daniel Drakee85d0912006-06-02 17:11:32 +0100661 /*
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000662 * CONTROL TODO:
Daniel Drakee85d0912006-06-02 17:11:32 +0100663 * - if backoff needed, enable bit 0
664 * - if burst (backoff not needed) disable bit 0
Daniel Drakee85d0912006-06-02 17:11:32 +0100665 */
666
667 cs->control = 0;
668
669 /* First fragment */
Johannes Berge6a98542008-10-21 12:40:02 +0200670 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
Daniel Drakee85d0912006-06-02 17:11:32 +0100671 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
672
Gábor Stefanik13bdcd92009-05-14 17:34:59 +0000673 /* No ACK expected (multicast, etc.) */
674 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
675 cs->control |= ZD_CS_NO_ACK;
Daniel Drakee85d0912006-06-02 17:11:32 +0100676
677 /* PS-POLL */
Harvey Harrison85365822008-06-14 23:33:39 -0700678 if (ieee80211_is_pspoll(header->frame_control))
Daniel Drakee85d0912006-06-02 17:11:32 +0100679 cs->control |= ZD_CS_PS_POLL_FRAME;
680
Johannes Berge6a98542008-10-21 12:40:02 +0200681 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000682 cs->control |= ZD_CS_RTS;
683
Johannes Berge6a98542008-10-21 12:40:02 +0200684 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000685 cs->control |= ZD_CS_SELF_CTS;
Daniel Drakee85d0912006-06-02 17:11:32 +0100686
687 /* FIXME: Management frame? */
688}
689
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +0300690static bool zd_mac_match_cur_beacon(struct zd_mac *mac, struct sk_buff *beacon)
691{
692 if (!mac->beacon.cur_beacon)
693 return false;
694
695 if (mac->beacon.cur_beacon->len != beacon->len)
696 return false;
697
698 return !memcmp(beacon->data, mac->beacon.cur_beacon->data, beacon->len);
699}
700
701static void zd_mac_free_cur_beacon_locked(struct zd_mac *mac)
702{
703 ZD_ASSERT(mutex_is_locked(&mac->chip.mutex));
704
705 kfree_skb(mac->beacon.cur_beacon);
706 mac->beacon.cur_beacon = NULL;
707}
708
709static void zd_mac_free_cur_beacon(struct zd_mac *mac)
710{
711 mutex_lock(&mac->chip.mutex);
712 zd_mac_free_cur_beacon_locked(mac);
713 mutex_unlock(&mac->chip.mutex);
714}
715
Jussi Kivilinnadde46732011-06-20 14:42:49 +0300716static int zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon,
717 bool in_intr)
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800718{
719 struct zd_mac *mac = zd_hw_mac(hw);
Jussi Kivilinna51272292011-01-31 20:49:05 +0200720 int r, ret, num_cmds, req_pos = 0;
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800721 u32 tmp, j = 0;
722 /* 4 more bytes for tail CRC */
723 u32 full_len = beacon->len + 4;
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200724 unsigned long end_jiffies, message_jiffies;
Jussi Kivilinna51272292011-01-31 20:49:05 +0200725 struct zd_ioreq32 *ioreqs;
726
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +0300727 mutex_lock(&mac->chip.mutex);
728
729 /* Check if hw already has this beacon. */
730 if (zd_mac_match_cur_beacon(mac, beacon)) {
731 r = 0;
732 goto out_nofree;
733 }
734
Jussi Kivilinna51272292011-01-31 20:49:05 +0200735 /* Alloc memory for full beacon write at once. */
736 num_cmds = 1 + zd_chip_is_zd1211b(&mac->chip) + full_len;
737 ioreqs = kmalloc(num_cmds * sizeof(struct zd_ioreq32), GFP_KERNEL);
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +0300738 if (!ioreqs) {
739 r = -ENOMEM;
740 goto out_nofree;
741 }
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100742
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200743 r = zd_iowrite32_locked(&mac->chip, 0, CR_BCN_FIFO_SEMAPHORE);
744 if (r < 0)
745 goto out;
746 r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
747 if (r < 0)
748 goto release_sema;
Jussi Kivilinnadde46732011-06-20 14:42:49 +0300749 if (in_intr && tmp & 0x2) {
750 r = -EBUSY;
751 goto release_sema;
752 }
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200753
754 end_jiffies = jiffies + HZ / 2; /*~500ms*/
755 message_jiffies = jiffies + HZ / 10; /*~100ms*/
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800756 while (tmp & 0x2) {
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200757 r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100758 if (r < 0)
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200759 goto release_sema;
760 if (time_is_before_eq_jiffies(message_jiffies)) {
761 message_jiffies = jiffies + HZ / 10;
762 dev_err(zd_mac_dev(mac),
763 "CR_BCN_FIFO_SEMAPHORE not ready\n");
764 if (time_is_before_eq_jiffies(end_jiffies)) {
765 dev_err(zd_mac_dev(mac),
766 "Giving up beacon config.\n");
767 r = -ETIMEDOUT;
Jussi Kivilinna3985a462011-01-31 20:50:02 +0200768 goto reset_device;
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800769 }
770 }
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200771 msleep(20);
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800772 }
773
Jussi Kivilinna51272292011-01-31 20:49:05 +0200774 ioreqs[req_pos].addr = CR_BCN_FIFO;
775 ioreqs[req_pos].value = full_len - 1;
776 req_pos++;
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100777 if (zd_chip_is_zd1211b(&mac->chip)) {
Jussi Kivilinna51272292011-01-31 20:49:05 +0200778 ioreqs[req_pos].addr = CR_BCN_LENGTH;
779 ioreqs[req_pos].value = full_len - 1;
780 req_pos++;
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100781 }
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800782
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100783 for (j = 0 ; j < beacon->len; j++) {
Jussi Kivilinna51272292011-01-31 20:49:05 +0200784 ioreqs[req_pos].addr = CR_BCN_FIFO;
785 ioreqs[req_pos].value = *((u8 *)(beacon->data + j));
786 req_pos++;
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100787 }
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800788
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100789 for (j = 0; j < 4; j++) {
Jussi Kivilinna51272292011-01-31 20:49:05 +0200790 ioreqs[req_pos].addr = CR_BCN_FIFO;
791 ioreqs[req_pos].value = 0x0;
792 req_pos++;
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100793 }
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800794
Jussi Kivilinna51272292011-01-31 20:49:05 +0200795 BUG_ON(req_pos != num_cmds);
796
797 r = zd_iowrite32a_locked(&mac->chip, ioreqs, num_cmds);
798
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200799release_sema:
800 /*
801 * Try very hard to release device beacon semaphore, as otherwise
802 * device/driver can be left in unusable state.
803 */
804 end_jiffies = jiffies + HZ / 2; /*~500ms*/
805 ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
806 while (ret < 0) {
Jussi Kivilinnadde46732011-06-20 14:42:49 +0300807 if (in_intr || time_is_before_eq_jiffies(end_jiffies)) {
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200808 ret = -ETIMEDOUT;
809 break;
810 }
811
812 msleep(20);
813 ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
814 }
815
816 if (ret < 0)
817 dev_err(zd_mac_dev(mac), "Could not release "
818 "CR_BCN_FIFO_SEMAPHORE!\n");
819 if (r < 0 || ret < 0) {
820 if (r >= 0)
821 r = ret;
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +0300822
823 /* We don't know if beacon was written successfully or not,
824 * so clear current. */
825 zd_mac_free_cur_beacon_locked(mac);
826
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200827 goto out;
828 }
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100829
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +0300830 /* Beacon has now been written successfully, update current. */
831 zd_mac_free_cur_beacon_locked(mac);
832 mac->beacon.cur_beacon = beacon;
833 beacon = NULL;
834
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800835 /* 802.11b/g 2.4G CCK 1Mb
836 * 802.11a, not yet implemented, uses different values (see GPL vendor
837 * driver)
838 */
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200839 r = zd_iowrite32_locked(&mac->chip, 0x00000400 | (full_len << 19),
840 CR_BCN_PLCP_CFG);
841out:
Jussi Kivilinna51272292011-01-31 20:49:05 +0200842 kfree(ioreqs);
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +0300843out_nofree:
844 kfree_skb(beacon);
845 mutex_unlock(&mac->chip.mutex);
846
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200847 return r;
Jussi Kivilinna3985a462011-01-31 20:50:02 +0200848
849reset_device:
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +0300850 zd_mac_free_cur_beacon_locked(mac);
851 kfree_skb(beacon);
852
Jussi Kivilinna3985a462011-01-31 20:50:02 +0200853 mutex_unlock(&mac->chip.mutex);
854 kfree(ioreqs);
855
856 /* semaphore stuck, reset device to avoid fw freeze later */
857 dev_warn(zd_mac_dev(mac), "CR_BCN_FIFO_SEMAPHORE stuck, "
Masanari Iida50db7fa2012-02-10 00:29:32 +0900858 "resetting device...");
Jussi Kivilinna3985a462011-01-31 20:50:02 +0200859 usb_queue_reset_device(mac->chip.usb.intf);
860
861 return r;
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800862}
863
Daniel Drakee85d0912006-06-02 17:11:32 +0100864static int fill_ctrlset(struct zd_mac *mac,
Johannes Berge039fa42008-05-15 12:55:29 +0200865 struct sk_buff *skb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100866{
867 int r;
Daniel Drake459c51a2007-11-19 15:00:29 +0000868 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
869 unsigned int frag_len = skb->len + FCS_LEN;
Daniel Drakee85d0912006-06-02 17:11:32 +0100870 unsigned int packet_length;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200871 struct ieee80211_rate *txrate;
Daniel Drakee85d0912006-06-02 17:11:32 +0100872 struct zd_ctrlset *cs = (struct zd_ctrlset *)
873 skb_push(skb, sizeof(struct zd_ctrlset));
Johannes Berge039fa42008-05-15 12:55:29 +0200874 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100875
Daniel Drakee85d0912006-06-02 17:11:32 +0100876 ZD_ASSERT(frag_len <= 0xffff);
Daniel Drakee85d0912006-06-02 17:11:32 +0100877
Tomas Vaneke81a7bd2012-02-05 15:51:53 +0200878 /*
879 * Firmware computes the duration itself (for all frames except PSPoll)
880 * and needs the field set to 0 at input, otherwise firmware messes up
881 * duration_id and sets bits 14 and 15 on.
882 */
883 if (!ieee80211_is_pspoll(hdr->frame_control))
884 hdr->duration_id = 0;
885
Johannes Berge039fa42008-05-15 12:55:29 +0200886 txrate = ieee80211_get_tx_rate(mac->hw, info);
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200887
888 cs->modulation = txrate->hw_value;
Johannes Berge6a98542008-10-21 12:40:02 +0200889 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200890 cs->modulation = txrate->hw_value_short;
Daniel Drakee85d0912006-06-02 17:11:32 +0100891
892 cs->tx_length = cpu_to_le16(frag_len);
893
Johannes Berge6a98542008-10-21 12:40:02 +0200894 cs_set_control(mac, cs, hdr, info);
Daniel Drakee85d0912006-06-02 17:11:32 +0100895
896 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
897 ZD_ASSERT(packet_length <= 0xffff);
898 /* ZD1211B: Computing the length difference this way, gives us
899 * flexibility to compute the packet length.
900 */
Daniel Drake74553ae2007-07-01 18:22:32 +0100901 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
Daniel Drakee85d0912006-06-02 17:11:32 +0100902 packet_length - frag_len : packet_length);
903
904 /*
905 * CURRENT LENGTH:
906 * - transmit frame length in microseconds
907 * - seems to be derived from frame length
908 * - see Cal_Us_Service() in zdinlinef.h
909 * - if macp->bTxBurstEnable is enabled, then multiply by 4
910 * - bTxBurstEnable is never set in the vendor driver
911 *
912 * SERVICE:
913 * - "for PLCP configuration"
914 * - always 0 except in some situations at 802.11b 11M
915 * - see line 53 of zdinlinef.h
916 */
917 cs->service = 0;
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100918 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
Daniel Drakee85d0912006-06-02 17:11:32 +0100919 le16_to_cpu(cs->tx_length));
920 if (r < 0)
921 return r;
922 cs->current_length = cpu_to_le16(r);
Daniel Drake459c51a2007-11-19 15:00:29 +0000923 cs->next_frame_length = 0;
Daniel Drakee85d0912006-06-02 17:11:32 +0100924
925 return 0;
926}
927
Daniel Drake459c51a2007-11-19 15:00:29 +0000928/**
929 * zd_op_tx - transmits a network frame to the device
Ulrich Kunitz741fec52006-11-22 00:05:53 +0000930 *
Daniel Drake459c51a2007-11-19 15:00:29 +0000931 * @dev: mac80211 hardware device
932 * @skb: socket buffer
933 * @control: the control structure
Daniel Drakee85d0912006-06-02 17:11:32 +0100934 *
Daniel Drake459c51a2007-11-19 15:00:29 +0000935 * This function transmit an IEEE 802.11 network frame to the device. The
936 * control block of the skbuff will be initialized. If necessary the incoming
937 * mac80211 queues will be stopped.
Daniel Drakee85d0912006-06-02 17:11:32 +0100938 */
Thomas Huehn36323f82012-07-23 21:33:42 +0200939static void zd_op_tx(struct ieee80211_hw *hw,
940 struct ieee80211_tx_control *control,
941 struct sk_buff *skb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100942{
Daniel Drake459c51a2007-11-19 15:00:29 +0000943 struct zd_mac *mac = zd_hw_mac(hw);
Johannes Berge039fa42008-05-15 12:55:29 +0200944 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Daniel Drake459c51a2007-11-19 15:00:29 +0000945 int r;
Daniel Drakee85d0912006-06-02 17:11:32 +0100946
Johannes Berge039fa42008-05-15 12:55:29 +0200947 r = fill_ctrlset(mac, skb);
Daniel Drake459c51a2007-11-19 15:00:29 +0000948 if (r)
Jouni Malinen640c65e2009-03-16 21:47:33 +0200949 goto fail;
Daniel Drakee85d0912006-06-02 17:11:32 +0100950
Johannes Berge6a98542008-10-21 12:40:02 +0200951 info->rate_driver_data[0] = hw;
Johannes Berge039fa42008-05-15 12:55:29 +0200952
953 r = zd_usb_tx(&mac->chip.usb, skb);
Daniel Drake459c51a2007-11-19 15:00:29 +0000954 if (r)
Jouni Malinen640c65e2009-03-16 21:47:33 +0200955 goto fail;
Johannes Berg7bb45682011-02-24 14:42:06 +0100956 return;
Jouni Malinen640c65e2009-03-16 21:47:33 +0200957
958fail:
959 dev_kfree_skb(skb);
Daniel Drake459c51a2007-11-19 15:00:29 +0000960}
961
962/**
963 * filter_ack - filters incoming packets for acknowledgements
964 * @dev: the mac80211 device
965 * @rx_hdr: received header
966 * @stats: the status for the received packet
967 *
968 * This functions looks for ACK packets and tries to match them with the
969 * frames in the tx queue. If a match is found the frame will be dequeued and
970 * the upper layers is informed about the successful transmission. If
971 * mac80211 queues have been stopped and the number of frames still to be
972 * transmitted is low the queues will be opened again.
973 *
974 * Returns 1 if the frame was an ACK, 0 if it was ignored.
975 */
976static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
977 struct ieee80211_rx_status *stats)
978{
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200979 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drake459c51a2007-11-19 15:00:29 +0000980 struct sk_buff *skb;
981 struct sk_buff_head *q;
982 unsigned long flags;
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200983 int found = 0;
984 int i, position = 0;
Daniel Drake459c51a2007-11-19 15:00:29 +0000985
Harvey Harrison85365822008-06-14 23:33:39 -0700986 if (!ieee80211_is_ack(rx_hdr->frame_control))
Daniel Drake459c51a2007-11-19 15:00:29 +0000987 return 0;
988
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200989 q = &mac->ack_wait_queue;
Daniel Drake459c51a2007-11-19 15:00:29 +0000990 spin_lock_irqsave(&q->lock, flags);
David S. Miller47a227d2008-09-23 00:23:30 -0700991 skb_queue_walk(q, skb) {
Daniel Drake459c51a2007-11-19 15:00:29 +0000992 struct ieee80211_hdr *tx_hdr;
993
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200994 position ++;
995
996 if (mac->ack_pending && skb_queue_is_first(q, skb))
997 continue;
998
Daniel Drake459c51a2007-11-19 15:00:29 +0000999 tx_hdr = (struct ieee80211_hdr *)skb->data;
Shaddy Baddahcde69012008-11-28 17:10:45 +11001000 if (likely(!memcmp(tx_hdr->addr2, rx_hdr->addr1, ETH_ALEN)))
Daniel Drake459c51a2007-11-19 15:00:29 +00001001 {
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +02001002 found = 1;
1003 break;
Daniel Drake459c51a2007-11-19 15:00:29 +00001004 }
1005 }
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +02001006
1007 if (found) {
1008 for (i=1; i<position; i++) {
1009 skb = __skb_dequeue(q);
1010 zd_mac_tx_status(hw, skb,
1011 mac->ack_pending ? mac->ack_signal : 0,
1012 NULL);
1013 mac->ack_pending = 0;
1014 }
1015
1016 mac->ack_pending = 1;
1017 mac->ack_signal = stats->signal;
Jussi Kivilinnaf773e402011-01-31 20:48:16 +02001018
1019 /* Prevent pending tx-packet on AP-mode */
1020 if (mac->type == NL80211_IFTYPE_AP) {
1021 skb = __skb_dequeue(q);
1022 zd_mac_tx_status(hw, skb, mac->ack_signal, NULL);
1023 mac->ack_pending = 0;
1024 }
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +02001025 }
1026
Daniel Drake459c51a2007-11-19 15:00:29 +00001027 spin_unlock_irqrestore(&q->lock, flags);
1028 return 1;
1029}
1030
1031int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
1032{
1033 struct zd_mac *mac = zd_hw_mac(hw);
1034 struct ieee80211_rx_status stats;
1035 const struct rx_status *status;
1036 struct sk_buff *skb;
1037 int bad_frame = 0;
Harvey Harrison85365822008-06-14 23:33:39 -07001038 __le16 fc;
1039 int need_padding;
Johannes Berg8318d782008-01-24 19:38:38 +01001040 int i;
1041 u8 rate;
Daniel Drake459c51a2007-11-19 15:00:29 +00001042
1043 if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
1044 FCS_LEN + sizeof(struct rx_status))
Daniel Drakee85d0912006-06-02 17:11:32 +01001045 return -EINVAL;
1046
Daniel Drake459c51a2007-11-19 15:00:29 +00001047 memset(&stats, 0, sizeof(stats));
Daniel Drakee85d0912006-06-02 17:11:32 +01001048
Daniel Drake459c51a2007-11-19 15:00:29 +00001049 /* Note about pass_failed_fcs and pass_ctrl access below:
1050 * mac locking intentionally omitted here, as this is the only unlocked
1051 * reader and the only writer is configure_filter. Plus, if there were
1052 * any races accessing these variables, it wouldn't really matter.
1053 * If mac80211 ever provides a way for us to access filter flags
1054 * from outside configure_filter, we could improve on this. Also, this
1055 * situation may change once we implement some kind of DMA-into-skb
1056 * RX path. */
Daniel Drakee85d0912006-06-02 17:11:32 +01001057
Daniel Drake459c51a2007-11-19 15:00:29 +00001058 /* Caller has to ensure that length >= sizeof(struct rx_status). */
1059 status = (struct rx_status *)
Ulrich Kunitz937a0492007-10-02 18:36:53 +01001060 (buffer + (length - sizeof(struct rx_status)));
Daniel Drakee85d0912006-06-02 17:11:32 +01001061 if (status->frame_status & ZD_RX_ERROR) {
Daniel Drake459c51a2007-11-19 15:00:29 +00001062 if (mac->pass_failed_fcs &&
1063 (status->frame_status & ZD_RX_CRC32_ERROR)) {
1064 stats.flag |= RX_FLAG_FAILED_FCS_CRC;
1065 bad_frame = 1;
1066 } else {
1067 return -EINVAL;
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001068 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001069 }
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001070
Johannes Berg8318d782008-01-24 19:38:38 +01001071 stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
1072 stats.band = IEEE80211_BAND_2GHZ;
Jussi Kivilinna7a1d6562011-06-20 14:42:28 +03001073 stats.signal = zd_check_signal(hw, status->signal_strength);
Johannes Berg8318d782008-01-24 19:38:38 +01001074
1075 rate = zd_rx_rate(buffer, status);
1076
1077 /* todo: return index in the big switches in zd_rx_rate instead */
1078 for (i = 0; i < mac->band.n_bitrates; i++)
1079 if (rate == mac->band.bitrates[i].hw_value)
1080 stats.rate_idx = i;
Daniel Drakee85d0912006-06-02 17:11:32 +01001081
Daniel Drake459c51a2007-11-19 15:00:29 +00001082 length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
1083 buffer += ZD_PLCP_HEADER_SIZE;
Daniel Drakee85d0912006-06-02 17:11:32 +01001084
Daniel Drake459c51a2007-11-19 15:00:29 +00001085 /* Except for bad frames, filter each frame to see if it is an ACK, in
1086 * which case our internal TX tracking is updated. Normally we then
1087 * bail here as there's no need to pass ACKs on up to the stack, but
1088 * there is also the case where the stack has requested us to pass
1089 * control frames on up (pass_ctrl) which we must consider. */
1090 if (!bad_frame &&
1091 filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
1092 && !mac->pass_ctrl)
1093 return 0;
Daniel Drakee85d0912006-06-02 17:11:32 +01001094
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001095 fc = get_unaligned((__le16*)buffer);
Harvey Harrison85365822008-06-14 23:33:39 -07001096 need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
Michael Buesch90817282007-12-29 17:24:23 +01001097
1098 skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
Daniel Drake459c51a2007-11-19 15:00:29 +00001099 if (skb == NULL)
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001100 return -ENOMEM;
Michael Buesch90817282007-12-29 17:24:23 +01001101 if (need_padding) {
Walter Goldens77c20612010-05-18 04:44:54 -07001102 /* Make sure the payload data is 4 byte aligned. */
Michael Buesch90817282007-12-29 17:24:23 +01001103 skb_reserve(skb, 2);
1104 }
1105
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +02001106 /* FIXME : could we avoid this big memcpy ? */
Daniel Drake459c51a2007-11-19 15:00:29 +00001107 memcpy(skb_put(skb, length), buffer, length);
1108
Johannes Bergf1d58c22009-06-17 13:13:00 +02001109 memcpy(IEEE80211_SKB_RXCB(skb), &stats, sizeof(stats));
1110 ieee80211_rx_irqsafe(hw, skb);
Daniel Drakee85d0912006-06-02 17:11:32 +01001111 return 0;
1112}
1113
Daniel Drake459c51a2007-11-19 15:00:29 +00001114static int zd_op_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001115 struct ieee80211_vif *vif)
Daniel Drakee85d0912006-06-02 17:11:32 +01001116{
Daniel Drake459c51a2007-11-19 15:00:29 +00001117 struct zd_mac *mac = zd_hw_mac(hw);
1118
Johannes Berg05c914f2008-09-11 00:01:58 +02001119 /* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */
1120 if (mac->type != NL80211_IFTYPE_UNSPECIFIED)
Daniel Drake459c51a2007-11-19 15:00:29 +00001121 return -EOPNOTSUPP;
1122
Johannes Berg1ed32e42009-12-23 13:15:45 +01001123 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001124 case NL80211_IFTYPE_MONITOR:
1125 case NL80211_IFTYPE_MESH_POINT:
1126 case NL80211_IFTYPE_STATION:
1127 case NL80211_IFTYPE_ADHOC:
Jussi Kivilinnaab419e92011-01-31 20:50:21 +02001128 case NL80211_IFTYPE_AP:
Johannes Berg1ed32e42009-12-23 13:15:45 +01001129 mac->type = vif->type;
Daniel Drake459c51a2007-11-19 15:00:29 +00001130 break;
1131 default:
1132 return -EOPNOTSUPP;
1133 }
1134
Jussi Kivilinnac2fadcb2011-01-31 20:48:06 +02001135 mac->vif = vif;
1136
1137 return set_mac_and_bssid(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +01001138}
1139
Daniel Drake459c51a2007-11-19 15:00:29 +00001140static void zd_op_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001141 struct ieee80211_vif *vif)
Daniel Drakee85d0912006-06-02 17:11:32 +01001142{
Daniel Drake459c51a2007-11-19 15:00:29 +00001143 struct zd_mac *mac = zd_hw_mac(hw);
Johannes Berg05c914f2008-09-11 00:01:58 +02001144 mac->type = NL80211_IFTYPE_UNSPECIFIED;
Jussi Kivilinnac2fadcb2011-01-31 20:48:06 +02001145 mac->vif = NULL;
Jussi Kivilinnab91a5152011-01-31 20:48:25 +02001146 zd_set_beacon_interval(&mac->chip, 0, 0, NL80211_IFTYPE_UNSPECIFIED);
Daniel Drake459c51a2007-11-19 15:00:29 +00001147 zd_write_mac_addr(&mac->chip, NULL);
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +03001148
1149 zd_mac_free_cur_beacon(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +01001150}
1151
Johannes Berge8975582008-10-09 12:18:51 +02001152static int zd_op_config(struct ieee80211_hw *hw, u32 changed)
Daniel Drakee85d0912006-06-02 17:11:32 +01001153{
Daniel Drake459c51a2007-11-19 15:00:29 +00001154 struct zd_mac *mac = zd_hw_mac(hw);
Johannes Berge8975582008-10-09 12:18:51 +02001155 struct ieee80211_conf *conf = &hw->conf;
1156
Jussi Kivilinna212e1a52011-01-31 20:49:43 +02001157 spin_lock_irq(&mac->lock);
Karl Beldan675a0b02013-03-25 16:26:57 +01001158 mac->channel = conf->chandef.chan->hw_value;
Jussi Kivilinna212e1a52011-01-31 20:49:43 +02001159 spin_unlock_irq(&mac->lock);
1160
Karl Beldan675a0b02013-03-25 16:26:57 +01001161 return zd_chip_set_channel(&mac->chip, conf->chandef.chan->hw_value);
Daniel Drakee85d0912006-06-02 17:11:32 +01001162}
1163
Jussi Kivilinna4099e2f2011-01-31 20:48:35 +02001164static void zd_beacon_done(struct zd_mac *mac)
1165{
1166 struct sk_buff *skb, *beacon;
1167
Jussi Kivilinna9be23252011-01-31 20:48:55 +02001168 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1169 return;
Jussi Kivilinna4099e2f2011-01-31 20:48:35 +02001170 if (!mac->vif || mac->vif->type != NL80211_IFTYPE_AP)
1171 return;
1172
1173 /*
1174 * Send out buffered broad- and multicast frames.
1175 */
1176 while (!ieee80211_queue_stopped(mac->hw, 0)) {
1177 skb = ieee80211_get_buffered_bc(mac->hw, mac->vif);
1178 if (!skb)
1179 break;
Thomas Huehn36323f82012-07-23 21:33:42 +02001180 zd_op_tx(mac->hw, NULL, skb);
Jussi Kivilinna4099e2f2011-01-31 20:48:35 +02001181 }
1182
1183 /*
1184 * Fetch next beacon so that tim_count is updated.
1185 */
1186 beacon = ieee80211_beacon_get(mac->hw, mac->vif);
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +03001187 if (beacon)
Jussi Kivilinnadde46732011-06-20 14:42:49 +03001188 zd_mac_config_beacon(mac->hw, beacon, true);
Jussi Kivilinna4099e2f2011-01-31 20:48:35 +02001189
Jussi Kivilinna9be23252011-01-31 20:48:55 +02001190 spin_lock_irq(&mac->lock);
1191 mac->beacon.last_update = jiffies;
1192 spin_unlock_irq(&mac->lock);
Jussi Kivilinna4099e2f2011-01-31 20:48:35 +02001193}
1194
Luis R. Rodrigueze83a1072008-09-09 23:19:49 -07001195static void zd_process_intr(struct work_struct *work)
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -08001196{
1197 u16 int_status;
Jussi Kivilinna8b17f752011-01-31 20:47:27 +02001198 unsigned long flags;
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -08001199 struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
1200
Jussi Kivilinna8b17f752011-01-31 20:47:27 +02001201 spin_lock_irqsave(&mac->lock, flags);
1202 int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer + 4));
1203 spin_unlock_irqrestore(&mac->lock, flags);
1204
Jussi Kivilinna4099e2f2011-01-31 20:48:35 +02001205 if (int_status & INT_CFG_NEXT_BCN) {
1206 /*dev_dbg_f_limit(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");*/
1207 zd_beacon_done(mac);
1208 } else {
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -08001209 dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
Jussi Kivilinna4099e2f2011-01-31 20:48:35 +02001210 }
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -08001211
1212 zd_chip_enable_hwint(&mac->chip);
1213}
1214
1215
Johannes Berg3ac64be2009-08-17 16:16:53 +02001216static u64 zd_op_prepare_multicast(struct ieee80211_hw *hw,
Jiri Pirko22bedad32010-04-01 21:22:57 +00001217 struct netdev_hw_addr_list *mc_list)
Johannes Berg3ac64be2009-08-17 16:16:53 +02001218{
1219 struct zd_mac *mac = zd_hw_mac(hw);
1220 struct zd_mc_hash hash;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001221 struct netdev_hw_addr *ha;
Johannes Berg3ac64be2009-08-17 16:16:53 +02001222
1223 zd_mc_clear(&hash);
1224
Jiri Pirko22bedad32010-04-01 21:22:57 +00001225 netdev_hw_addr_list_for_each(ha, mc_list) {
1226 dev_dbg_f(zd_mac_dev(mac), "mc addr %pM\n", ha->addr);
1227 zd_mc_add_addr(&hash, ha->addr);
Johannes Berg3ac64be2009-08-17 16:16:53 +02001228 }
1229
1230 return hash.low | ((u64)hash.high << 32);
1231}
1232
Daniel Drake459c51a2007-11-19 15:00:29 +00001233#define SUPPORTED_FIF_FLAGS \
1234 (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
Michael Buesch2c1a1b12008-02-10 16:03:55 +01001235 FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
Daniel Drake459c51a2007-11-19 15:00:29 +00001236static void zd_op_configure_filter(struct ieee80211_hw *hw,
1237 unsigned int changed_flags,
1238 unsigned int *new_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001239 u64 multicast)
Daniel Drake459c51a2007-11-19 15:00:29 +00001240{
Johannes Berg3ac64be2009-08-17 16:16:53 +02001241 struct zd_mc_hash hash = {
1242 .low = multicast,
1243 .high = multicast >> 32,
1244 };
Daniel Drake459c51a2007-11-19 15:00:29 +00001245 struct zd_mac *mac = zd_hw_mac(hw);
1246 unsigned long flags;
Jussi Kivilinnaa6fb0712011-01-31 20:47:46 +02001247 int r;
Daniel Drake459c51a2007-11-19 15:00:29 +00001248
1249 /* Only deal with supported flags */
1250 changed_flags &= SUPPORTED_FIF_FLAGS;
1251 *new_flags &= SUPPORTED_FIF_FLAGS;
1252
Benoit Papillault7de3c5d2010-01-03 10:20:01 +01001253 /*
1254 * If multicast parameter (as returned by zd_op_prepare_multicast)
1255 * has changed, no bit in changed_flags is set. To handle this
1256 * situation, we do not return if changed_flags is 0. If we do so,
1257 * we will have some issue with IPv6 which uses multicast for link
1258 * layer address resolution.
1259 */
Johannes Berg3ac64be2009-08-17 16:16:53 +02001260 if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI))
Daniel Drake459c51a2007-11-19 15:00:29 +00001261 zd_mc_add_all(&hash);
Daniel Drake459c51a2007-11-19 15:00:29 +00001262
1263 spin_lock_irqsave(&mac->lock, flags);
1264 mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
1265 mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
1266 mac->multicast_hash = hash;
1267 spin_unlock_irqrestore(&mac->lock, flags);
Johannes Berg3ac64be2009-08-17 16:16:53 +02001268
Jussi Kivilinnaa6fb0712011-01-31 20:47:46 +02001269 zd_chip_set_multicast_hash(&mac->chip, &hash);
Daniel Drake459c51a2007-11-19 15:00:29 +00001270
Jussi Kivilinnaa6fb0712011-01-31 20:47:46 +02001271 if (changed_flags & FIF_CONTROL) {
1272 r = set_rx_filter(mac);
1273 if (r)
1274 dev_err(zd_mac_dev(mac), "set_rx_filter error %d\n", r);
1275 }
Daniel Drake459c51a2007-11-19 15:00:29 +00001276
1277 /* no handling required for FIF_OTHER_BSS as we don't currently
1278 * do BSSID filtering */
1279 /* FIXME: in future it would be nice to enable the probe response
1280 * filter (so that the driver doesn't see them) until
1281 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
1282 * have to schedule work to enable prbresp reception, which might
1283 * happen too late. For now we'll just listen and forward them all the
1284 * time. */
1285}
1286
Jussi Kivilinna5cf6cf82011-01-31 20:47:56 +02001287static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble)
Daniel Drake459c51a2007-11-19 15:00:29 +00001288{
Daniel Drake459c51a2007-11-19 15:00:29 +00001289 mutex_lock(&mac->chip.mutex);
Daniel Drake459c51a2007-11-19 15:00:29 +00001290 zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
1291 mutex_unlock(&mac->chip.mutex);
1292}
1293
Johannes Berg471b3ef2007-12-28 14:32:58 +01001294static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
1295 struct ieee80211_vif *vif,
1296 struct ieee80211_bss_conf *bss_conf,
1297 u32 changes)
Daniel Drake459c51a2007-11-19 15:00:29 +00001298{
1299 struct zd_mac *mac = zd_hw_mac(hw);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001300 int associated;
Daniel Drake459c51a2007-11-19 15:00:29 +00001301
1302 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
1303
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001304 if (mac->type == NL80211_IFTYPE_MESH_POINT ||
Jussi Kivilinnaab419e92011-01-31 20:50:21 +02001305 mac->type == NL80211_IFTYPE_ADHOC ||
1306 mac->type == NL80211_IFTYPE_AP) {
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001307 associated = true;
1308 if (changes & BSS_CHANGED_BEACON) {
1309 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
1310
1311 if (beacon) {
Jussi Kivilinna9be23252011-01-31 20:48:55 +02001312 zd_chip_disable_hwint(&mac->chip);
Jussi Kivilinnadde46732011-06-20 14:42:49 +03001313 zd_mac_config_beacon(hw, beacon, false);
Jussi Kivilinna9be23252011-01-31 20:48:55 +02001314 zd_chip_enable_hwint(&mac->chip);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001315 }
1316 }
1317
1318 if (changes & BSS_CHANGED_BEACON_ENABLED) {
Jussi Kivilinnab91a5152011-01-31 20:48:25 +02001319 u16 interval = 0;
1320 u8 period = 0;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001321
Jussi Kivilinnab91a5152011-01-31 20:48:25 +02001322 if (bss_conf->enable_beacon) {
1323 period = bss_conf->dtim_period;
1324 interval = bss_conf->beacon_int;
1325 }
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001326
Jussi Kivilinna9be23252011-01-31 20:48:55 +02001327 spin_lock_irq(&mac->lock);
1328 mac->beacon.period = period;
1329 mac->beacon.interval = interval;
1330 mac->beacon.last_update = jiffies;
1331 spin_unlock_irq(&mac->lock);
1332
Jussi Kivilinnab91a5152011-01-31 20:48:25 +02001333 zd_set_beacon_interval(&mac->chip, interval, period,
1334 mac->type);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001335 }
1336 } else
1337 associated = is_valid_ether_addr(bss_conf->bssid);
1338
1339 spin_lock_irq(&mac->lock);
1340 mac->associated = associated;
1341 spin_unlock_irq(&mac->lock);
1342
1343 /* TODO: do hardware bssid filtering */
1344
Johannes Berg471b3ef2007-12-28 14:32:58 +01001345 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Jussi Kivilinna5cf6cf82011-01-31 20:47:56 +02001346 spin_lock_irq(&mac->lock);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001347 mac->short_preamble = bss_conf->use_short_preamble;
Jussi Kivilinna5cf6cf82011-01-31 20:47:56 +02001348 spin_unlock_irq(&mac->lock);
1349
1350 set_rts_cts(mac, bss_conf->use_short_preamble);
Daniel Drake459c51a2007-11-19 15:00:29 +00001351 }
1352}
1353
Eliad Peller37a41b42011-09-21 14:06:11 +03001354static u64 zd_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Alina Friedrichsen5fe73192009-02-25 00:49:18 +01001355{
1356 struct zd_mac *mac = zd_hw_mac(hw);
1357 return zd_chip_get_tsf(&mac->chip);
1358}
1359
Daniel Drake459c51a2007-11-19 15:00:29 +00001360static const struct ieee80211_ops zd_ops = {
1361 .tx = zd_op_tx,
1362 .start = zd_op_start,
1363 .stop = zd_op_stop,
1364 .add_interface = zd_op_add_interface,
1365 .remove_interface = zd_op_remove_interface,
1366 .config = zd_op_config,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001367 .prepare_multicast = zd_op_prepare_multicast,
Daniel Drake459c51a2007-11-19 15:00:29 +00001368 .configure_filter = zd_op_configure_filter,
Johannes Berg471b3ef2007-12-28 14:32:58 +01001369 .bss_info_changed = zd_op_bss_info_changed,
Alina Friedrichsen5fe73192009-02-25 00:49:18 +01001370 .get_tsf = zd_op_get_tsf,
Daniel Drake459c51a2007-11-19 15:00:29 +00001371};
1372
1373struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
1374{
1375 struct zd_mac *mac;
1376 struct ieee80211_hw *hw;
Daniel Drake459c51a2007-11-19 15:00:29 +00001377
1378 hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
1379 if (!hw) {
1380 dev_dbg_f(&intf->dev, "out of memory\n");
1381 return NULL;
1382 }
1383
1384 mac = zd_hw_mac(hw);
1385
1386 memset(mac, 0, sizeof(*mac));
1387 spin_lock_init(&mac->lock);
1388 mac->hw = hw;
1389
Johannes Berg05c914f2008-09-11 00:01:58 +02001390 mac->type = NL80211_IFTYPE_UNSPECIFIED;
Daniel Drake459c51a2007-11-19 15:00:29 +00001391
1392 memcpy(mac->channels, zd_channels, sizeof(zd_channels));
1393 memcpy(mac->rates, zd_rates, sizeof(zd_rates));
Johannes Berg8318d782008-01-24 19:38:38 +01001394 mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
1395 mac->band.bitrates = mac->rates;
1396 mac->band.n_channels = ARRAY_SIZE(zd_channels);
1397 mac->band.channels = mac->channels;
Daniel Drake459c51a2007-11-19 15:00:29 +00001398
Johannes Berg8318d782008-01-24 19:38:38 +01001399 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
1400
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -08001401 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
Jussi Kivilinna4099e2f2011-01-31 20:48:35 +02001402 IEEE80211_HW_SIGNAL_UNSPEC |
Jussi Kivilinna959cd682012-09-02 21:10:44 +03001403 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
1404 IEEE80211_HW_MFP_CAPABLE;
Daniel Drake459c51a2007-11-19 15:00:29 +00001405
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07001406 hw->wiphy->interface_modes =
1407 BIT(NL80211_IFTYPE_MESH_POINT) |
1408 BIT(NL80211_IFTYPE_STATION) |
Jussi Kivilinnaab419e92011-01-31 20:50:21 +02001409 BIT(NL80211_IFTYPE_ADHOC) |
1410 BIT(NL80211_IFTYPE_AP);
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07001411
Bruno Randolf566bfe52008-05-08 19:15:40 +02001412 hw->max_signal = 100;
Daniel Drake459c51a2007-11-19 15:00:29 +00001413 hw->queues = 1;
1414 hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
1415
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +02001416 /*
1417 * Tell mac80211 that we support multi rate retries
1418 */
1419 hw->max_rates = IEEE80211_TX_MAX_RATES;
1420 hw->max_rate_tries = 18; /* 9 rates * 2 retries/rate */
1421
Daniel Drake459c51a2007-11-19 15:00:29 +00001422 skb_queue_head_init(&mac->ack_wait_queue);
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +02001423 mac->ack_pending = 0;
Daniel Drake459c51a2007-11-19 15:00:29 +00001424
Daniel Drake459c51a2007-11-19 15:00:29 +00001425 zd_chip_init(&mac->chip, hw, intf);
1426 housekeeping_init(mac);
Jussi Kivilinna9be23252011-01-31 20:48:55 +02001427 beacon_init(mac);
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -08001428 INIT_WORK(&mac->process_intr, zd_process_intr);
Daniel Drake459c51a2007-11-19 15:00:29 +00001429
1430 SET_IEEE80211_DEV(hw, &intf->dev);
1431 return hw;
Daniel Drakee85d0912006-06-02 17:11:32 +01001432}
1433
Jussi Kivilinna9be23252011-01-31 20:48:55 +02001434#define BEACON_WATCHDOG_DELAY round_jiffies_relative(HZ)
1435
1436static void beacon_watchdog_handler(struct work_struct *work)
1437{
1438 struct zd_mac *mac =
1439 container_of(work, struct zd_mac, beacon.watchdog_work.work);
1440 struct sk_buff *beacon;
1441 unsigned long timeout;
1442 int interval, period;
1443
1444 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1445 goto rearm;
1446 if (mac->type != NL80211_IFTYPE_AP || !mac->vif)
1447 goto rearm;
1448
1449 spin_lock_irq(&mac->lock);
1450 interval = mac->beacon.interval;
1451 period = mac->beacon.period;
Jussi Kivilinna55f77822011-06-20 14:42:54 +03001452 timeout = mac->beacon.last_update +
1453 msecs_to_jiffies(interval * 1024 / 1000) * 3;
Jussi Kivilinna9be23252011-01-31 20:48:55 +02001454 spin_unlock_irq(&mac->lock);
1455
1456 if (interval > 0 && time_is_before_jiffies(timeout)) {
1457 dev_dbg_f(zd_mac_dev(mac), "beacon interrupt stalled, "
1458 "restarting. "
1459 "(interval: %d, dtim: %d)\n",
1460 interval, period);
1461
1462 zd_chip_disable_hwint(&mac->chip);
1463
1464 beacon = ieee80211_beacon_get(mac->hw, mac->vif);
1465 if (beacon) {
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +03001466 zd_mac_free_cur_beacon(mac);
1467
Jussi Kivilinnadde46732011-06-20 14:42:49 +03001468 zd_mac_config_beacon(mac->hw, beacon, false);
Jussi Kivilinna9be23252011-01-31 20:48:55 +02001469 }
1470
1471 zd_set_beacon_interval(&mac->chip, interval, period, mac->type);
1472
1473 zd_chip_enable_hwint(&mac->chip);
1474
1475 spin_lock_irq(&mac->lock);
1476 mac->beacon.last_update = jiffies;
1477 spin_unlock_irq(&mac->lock);
1478 }
1479
1480rearm:
1481 queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
1482 BEACON_WATCHDOG_DELAY);
1483}
1484
1485static void beacon_init(struct zd_mac *mac)
1486{
1487 INIT_DELAYED_WORK(&mac->beacon.watchdog_work, beacon_watchdog_handler);
1488}
1489
1490static void beacon_enable(struct zd_mac *mac)
1491{
1492 dev_dbg_f(zd_mac_dev(mac), "\n");
1493
1494 mac->beacon.last_update = jiffies;
1495 queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
1496 BEACON_WATCHDOG_DELAY);
1497}
1498
1499static void beacon_disable(struct zd_mac *mac)
1500{
1501 dev_dbg_f(zd_mac_dev(mac), "\n");
1502 cancel_delayed_work_sync(&mac->beacon.watchdog_work);
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +03001503
1504 zd_mac_free_cur_beacon(mac);
Jussi Kivilinna9be23252011-01-31 20:48:55 +02001505}
1506
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001507#define LINK_LED_WORK_DELAY HZ
1508
David Howellsc4028952006-11-22 14:57:56 +00001509static void link_led_handler(struct work_struct *work)
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001510{
David Howellsc4028952006-11-22 14:57:56 +00001511 struct zd_mac *mac =
1512 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001513 struct zd_chip *chip = &mac->chip;
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001514 int is_associated;
1515 int r;
1516
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +02001517 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1518 goto requeue;
1519
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001520 spin_lock_irq(&mac->lock);
Daniel Drake459c51a2007-11-19 15:00:29 +00001521 is_associated = mac->associated;
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001522 spin_unlock_irq(&mac->lock);
1523
1524 r = zd_chip_control_leds(chip,
Luis R. Rodriguez14b46c82009-08-04 14:04:17 -07001525 is_associated ? ZD_LED_ASSOCIATED : ZD_LED_SCANNING);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001526 if (r)
Daniel Drake459c51a2007-11-19 15:00:29 +00001527 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001528
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +02001529requeue:
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001530 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1531 LINK_LED_WORK_DELAY);
1532}
1533
1534static void housekeeping_init(struct zd_mac *mac)
1535{
David Howellsc4028952006-11-22 14:57:56 +00001536 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001537}
1538
1539static void housekeeping_enable(struct zd_mac *mac)
1540{
1541 dev_dbg_f(zd_mac_dev(mac), "\n");
1542 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1543 0);
1544}
1545
1546static void housekeeping_disable(struct zd_mac *mac)
1547{
1548 dev_dbg_f(zd_mac_dev(mac), "\n");
Tejun Heoafe2c512010-12-14 16:21:17 +01001549 cancel_delayed_work_sync(&mac->housekeeping.link_led_work);
Luis R. Rodriguez14b46c82009-08-04 14:04:17 -07001550 zd_chip_control_leds(&mac->chip, ZD_LED_OFF);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001551}