blob: 98a574a4a465004728792a293551ae867f450d6c [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
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Daniel Drakee85d0912006-06-02 17:11:32 +010026#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"
Daniel Drakee85d0912006-06-02 17:11:32 +010033#include "zd_rf.h"
Daniel Drakee85d0912006-06-02 17:11:32 +010034
Luis R. Rodrigueze83a1072008-09-09 23:19:49 -070035struct 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" },
John W. Linville3d3b33b2010-06-16 14:41:36 -040045 { ZD_REGDOMAIN_JAPAN_2, "JP" },
46 { ZD_REGDOMAIN_JAPAN_3, "JP" },
Luis R. Rodrigueze83a1072008-09-09 23:19:49 -070047 { ZD_REGDOMAIN_SPAIN, "ES" },
48 { ZD_REGDOMAIN_FRANCE, "FR" },
49};
50
Daniel Drake459c51a2007-11-19 15:00:29 +000051/* This table contains the hardware specific values for the modulation rates. */
52static const struct ieee80211_rate zd_rates[] = {
Johannes Berg8318d782008-01-24 19:38:38 +010053 { .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 },
Daniel Drake459c51a2007-11-19 15:00:29 +000091};
92
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +020093/*
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
Daniel Drake459c51a2007-11-19 15:00:29 +0000121static const struct ieee80211_channel zd_channels[] = {
Johannes Berg8318d782008-01-24 19:38:38 +0100122 { .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 },
Daniel Drake459c51a2007-11-19 15:00:29 +0000136};
Daniel Drakee85d0912006-06-02 17:11:32 +0100137
Ulrich Kunitz583afd12006-09-13 02:42:38 +0100138static void housekeeping_init(struct zd_mac *mac);
139static void housekeeping_enable(struct zd_mac *mac);
140static void housekeeping_disable(struct zd_mac *mac);
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200141static void beacon_init(struct zd_mac *mac);
142static void beacon_enable(struct zd_mac *mac);
143static void beacon_disable(struct zd_mac *mac);
Jussi Kivilinna212e1a52011-01-31 20:49:43 +0200144static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble);
145static int zd_mac_config_beacon(struct ieee80211_hw *hw,
Jussi Kivilinnadde46732011-06-20 14:42:49 +0300146 struct sk_buff *beacon, bool in_intr);
Ulrich Kunitz583afd12006-09-13 02:42:38 +0100147
Luis R. Rodrigueze83a1072008-09-09 23:19:49 -0700148static 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
Jussi Kivilinna7a1d6562011-06-20 14:42:28 +0300163static 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
Daniel Drake459c51a2007-11-19 15:00:29 +0000179int zd_mac_preinit_hw(struct ieee80211_hw *hw)
Daniel Drake74553ae2007-07-01 18:22:32 +0100180{
181 int r;
182 u8 addr[ETH_ALEN];
Daniel Drake459c51a2007-11-19 15:00:29 +0000183 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drake74553ae2007-07-01 18:22:32 +0100184
185 r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
186 if (r)
187 return r;
188
Daniel Drake459c51a2007-11-19 15:00:29 +0000189 SET_IEEE80211_PERM_ADDR(hw, addr);
190
Daniel Drake74553ae2007-07-01 18:22:32 +0100191 return 0;
192}
193
Daniel Drake459c51a2007-11-19 15:00:29 +0000194int zd_mac_init_hw(struct ieee80211_hw *hw)
Daniel Drakee85d0912006-06-02 17:11:32 +0100195{
196 int r;
Daniel Drake459c51a2007-11-19 15:00:29 +0000197 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100198 struct zd_chip *chip = &mac->chip;
Luis R. Rodrigueze83a1072008-09-09 23:19:49 -0700199 char alpha2[2];
Daniel Drakee85d0912006-06-02 17:11:32 +0100200 u8 default_regdomain;
201
202 r = zd_chip_enable_int(chip);
203 if (r)
204 goto out;
Daniel Drake74553ae2007-07-01 18:22:32 +0100205 r = zd_chip_init_hw(chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100206 if (r)
207 goto disable_int;
208
Daniel Drakee85d0912006-06-02 17:11:32 +0100209 ZD_ASSERT(!irqs_disabled());
Daniel Drakee85d0912006-06-02 17:11:32 +0100210
211 r = zd_read_regdomain(chip, &default_regdomain);
212 if (r)
213 goto disable_int;
Daniel Drakee85d0912006-06-02 17:11:32 +0100214 spin_lock_irq(&mac->lock);
215 mac->regdomain = mac->default_regdomain = default_regdomain;
216 spin_unlock_irq(&mac->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +0100217
Daniel Drake40da08b2006-08-01 23:43:32 +0200218 /* 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);
Daniel Drakee85d0912006-06-02 17:11:32 +0100221 if (r)
222 goto disable_int;
223
Luis R. Rodrigueze83a1072008-09-09 23:19:49 -0700224 r = zd_reg2alpha2(mac->regdomain, alpha2);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500225 if (r)
226 goto disable_int;
Daniel Drakee85d0912006-06-02 17:11:32 +0100227
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500228 r = regulatory_hint(hw->wiphy, alpha2);
Daniel Drakee85d0912006-06-02 17:11:32 +0100229disable_int:
230 zd_chip_disable_int(chip);
231out:
232 return r;
233}
234
235void zd_mac_clear(struct zd_mac *mac)
236{
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000237 flush_workqueue(zd_workqueue);
Daniel Drakee85d0912006-06-02 17:11:32 +0100238 zd_chip_clear(&mac->chip);
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100239 ZD_ASSERT(!spin_is_locked(&mac->lock));
240 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
Daniel Drakee85d0912006-06-02 17:11:32 +0100241}
242
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100243static int set_rx_filter(struct zd_mac *mac)
Daniel Drakee85d0912006-06-02 17:11:32 +0100244{
Daniel Drake459c51a2007-11-19 15:00:29 +0000245 unsigned long flags;
246 u32 filter = STA_RX_FILTER;
Daniel Drakee85d0912006-06-02 17:11:32 +0100247
Daniel Drake459c51a2007-11-19 15:00:29 +0000248 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);
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100254}
255
Jussi Kivilinnac2fadcb2011-01-31 20:48:06 +0200256static 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
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100276static int set_mc_hash(struct zd_mac *mac)
277{
278 struct zd_mc_hash hash;
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100279 zd_mc_clear(&hash);
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100280 return zd_chip_set_multicast_hash(&mac->chip, &hash);
281}
282
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +0200283int zd_op_start(struct ieee80211_hw *hw)
Daniel Drakee85d0912006-06-02 17:11:32 +0100284{
Daniel Drake459c51a2007-11-19 15:00:29 +0000285 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100286 struct zd_chip *chip = &mac->chip;
Daniel Drake74553ae2007-07-01 18:22:32 +0100287 struct zd_usb *usb = &chip->usb;
Daniel Drakee85d0912006-06-02 17:11:32 +0100288 int r;
289
Daniel Drake74553ae2007-07-01 18:22:32 +0100290 if (!usb->initialized) {
291 r = zd_usb_init_hw(usb);
292 if (r)
293 goto out;
294 }
295
Daniel Drakee85d0912006-06-02 17:11:32 +0100296 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;
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100303 r = set_rx_filter(mac);
304 if (r)
305 goto disable_int;
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100306 r = set_mc_hash(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100307 if (r)
308 goto disable_int;
309 r = zd_chip_switch_radio_on(chip);
310 if (r < 0)
311 goto disable_int;
Daniel Drake459c51a2007-11-19 15:00:29 +0000312 r = zd_chip_enable_rxtx(chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100313 if (r < 0)
314 goto disable_radio;
315 r = zd_chip_enable_hwint(chip);
316 if (r < 0)
Daniel Drake459c51a2007-11-19 15:00:29 +0000317 goto disable_rxtx;
Daniel Drakee85d0912006-06-02 17:11:32 +0100318
Ulrich Kunitz583afd12006-09-13 02:42:38 +0100319 housekeeping_enable(mac);
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200320 beacon_enable(mac);
321 set_bit(ZD_DEVICE_RUNNING, &mac->flags);
Daniel Drakee85d0912006-06-02 17:11:32 +0100322 return 0;
Daniel Drake459c51a2007-11-19 15:00:29 +0000323disable_rxtx:
324 zd_chip_disable_rxtx(chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100325disable_radio:
326 zd_chip_switch_radio_off(chip);
327disable_int:
328 zd_chip_disable_int(chip);
329out:
330 return r;
331}
332
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +0200333void zd_op_stop(struct ieee80211_hw *hw)
Daniel Drake459c51a2007-11-19 15:00:29 +0000334{
335 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100336 struct zd_chip *chip = &mac->chip;
Daniel Drake459c51a2007-11-19 15:00:29 +0000337 struct sk_buff *skb;
338 struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
Daniel Drakee85d0912006-06-02 17:11:32 +0100339
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200340 clear_bit(ZD_DEVICE_RUNNING, &mac->flags);
341
Daniel Drake459c51a2007-11-19 15:00:29 +0000342 /* The order here deliberately is a little different from the open()
Daniel Drakee85d0912006-06-02 17:11:32 +0100343 * method, since we need to make sure there is no opportunity for RX
Daniel Drake459c51a2007-11-19 15:00:29 +0000344 * frames to be processed by mac80211 after we have stopped it.
Daniel Drakee85d0912006-06-02 17:11:32 +0100345 */
346
Daniel Drake459c51a2007-11-19 15:00:29 +0000347 zd_chip_disable_rxtx(chip);
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200348 beacon_disable(mac);
Ulrich Kunitz583afd12006-09-13 02:42:38 +0100349 housekeeping_disable(mac);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000350 flush_workqueue(zd_workqueue);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000351
Daniel Drakee85d0912006-06-02 17:11:32 +0100352 zd_chip_disable_hwint(chip);
353 zd_chip_switch_radio_off(chip);
354 zd_chip_disable_int(chip);
355
Daniel Drake459c51a2007-11-19 15:00:29 +0000356
357 while ((skb = skb_dequeue(ack_wait_queue)))
Johannes Berge039fa42008-05-15 12:55:29 +0200358 dev_kfree_skb_any(skb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100359}
360
Jussi Kivilinna212e1a52011-01-31 20:49:43 +0200361int zd_restore_settings(struct zd_mac *mac)
362{
363 struct sk_buff *beacon;
364 struct zd_mc_hash multicast_hash;
365 unsigned int short_preamble;
366 int r, beacon_interval, beacon_period;
367 u8 channel;
368
369 dev_dbg_f(zd_mac_dev(mac), "\n");
370
371 spin_lock_irq(&mac->lock);
372 multicast_hash = mac->multicast_hash;
373 short_preamble = mac->short_preamble;
374 beacon_interval = mac->beacon.interval;
375 beacon_period = mac->beacon.period;
376 channel = mac->channel;
377 spin_unlock_irq(&mac->lock);
378
379 r = set_mac_and_bssid(mac);
380 if (r < 0) {
381 dev_dbg_f(zd_mac_dev(mac), "set_mac_and_bssid failed, %d\n", r);
382 return r;
383 }
384
385 r = zd_chip_set_channel(&mac->chip, channel);
386 if (r < 0) {
387 dev_dbg_f(zd_mac_dev(mac), "zd_chip_set_channel failed, %d\n",
388 r);
389 return r;
390 }
391
392 set_rts_cts(mac, short_preamble);
393
394 r = zd_chip_set_multicast_hash(&mac->chip, &multicast_hash);
395 if (r < 0) {
396 dev_dbg_f(zd_mac_dev(mac),
397 "zd_chip_set_multicast_hash failed, %d\n", r);
398 return r;
399 }
400
401 if (mac->type == NL80211_IFTYPE_MESH_POINT ||
402 mac->type == NL80211_IFTYPE_ADHOC ||
403 mac->type == NL80211_IFTYPE_AP) {
404 if (mac->vif != NULL) {
405 beacon = ieee80211_beacon_get(mac->hw, mac->vif);
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +0300406 if (beacon)
Jussi Kivilinnadde46732011-06-20 14:42:49 +0300407 zd_mac_config_beacon(mac->hw, beacon, false);
Jussi Kivilinna212e1a52011-01-31 20:49:43 +0200408 }
409
410 zd_set_beacon_interval(&mac->chip, beacon_interval,
411 beacon_period, mac->type);
412
413 spin_lock_irq(&mac->lock);
414 mac->beacon.last_update = jiffies;
415 spin_unlock_irq(&mac->lock);
416 }
417
418 return 0;
419}
420
Daniel Drake459c51a2007-11-19 15:00:29 +0000421/**
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200422 * zd_mac_tx_status - reports tx status of a packet if required
Daniel Drake459c51a2007-11-19 15:00:29 +0000423 * @hw - a &struct ieee80211_hw pointer
424 * @skb - a sk-buffer
Johannes Berge039fa42008-05-15 12:55:29 +0200425 * @flags: extra flags to set in the TX status info
426 * @ackssi: ACK signal strength
Coly Li73ac36e2009-01-07 18:09:16 -0800427 * @success - True for successful transmission of the frame
Daniel Drake459c51a2007-11-19 15:00:29 +0000428 *
429 * This information calls ieee80211_tx_status_irqsafe() if required by the
430 * control information. It copies the control information into the status
431 * information.
432 *
433 * If no status information has been requested, the skb is freed.
434 */
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200435static void zd_mac_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
436 int ackssi, struct tx_status *tx_status)
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000437{
Johannes Berge039fa42008-05-15 12:55:29 +0200438 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200439 int i;
440 int success = 1, retry = 1;
441 int first_idx;
442 const struct tx_retry_rate *retries;
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000443
Johannes Berge6a98542008-10-21 12:40:02 +0200444 ieee80211_tx_info_clear_status(info);
Johannes Berge039fa42008-05-15 12:55:29 +0200445
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200446 if (tx_status) {
447 success = !tx_status->failure;
448 retry = tx_status->retry + success;
449 }
450
451 if (success) {
452 /* success */
Johannes Berge6a98542008-10-21 12:40:02 +0200453 info->flags |= IEEE80211_TX_STAT_ACK;
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200454 } else {
455 /* failure */
456 info->flags &= ~IEEE80211_TX_STAT_ACK;
457 }
458
459 first_idx = info->status.rates[0].idx;
460 ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
461 retries = &zd_retry_rates[first_idx];
Dan Carpenter86baf712010-02-27 09:12:34 +0300462 ZD_ASSERT(1 <= retry && retry <= retries->count);
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200463
464 info->status.rates[0].idx = retries->rate[0];
465 info->status.rates[0].count = 1; // (retry > 1 ? 2 : 1);
466
467 for (i=1; i<IEEE80211_TX_MAX_RATES-1 && i<retry; i++) {
468 info->status.rates[i].idx = retries->rate[i];
469 info->status.rates[i].count = 1; // ((i==retry-1) && success ? 1:2);
470 }
471 for (; i<IEEE80211_TX_MAX_RATES && i<retry; i++) {
Dan Carpenter86baf712010-02-27 09:12:34 +0300472 info->status.rates[i].idx = retries->rate[retry - 1];
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200473 info->status.rates[i].count = 1; // (success ? 1:2);
474 }
475 if (i<IEEE80211_TX_MAX_RATES)
476 info->status.rates[i].idx = -1; /* terminate */
477
Jussi Kivilinna7a1d6562011-06-20 14:42:28 +0300478 info->status.ack_signal = zd_check_signal(hw, ackssi);
Johannes Berge039fa42008-05-15 12:55:29 +0200479 ieee80211_tx_status_irqsafe(hw, skb);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000480}
481
Daniel Drake459c51a2007-11-19 15:00:29 +0000482/**
483 * zd_mac_tx_failed - callback for failed frames
484 * @dev: the mac80211 wireless device
485 *
Adam Buchbinder303863f2009-12-11 16:35:37 -0500486 * This function is called if a frame couldn't be successfully
Daniel Drake459c51a2007-11-19 15:00:29 +0000487 * transferred. The first frame from the tx queue, will be selected and
488 * reported as error to the upper layers.
489 */
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200490void zd_mac_tx_failed(struct urb *urb)
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000491{
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200492 struct ieee80211_hw * hw = zd_usb_to_hw(urb->context);
493 struct zd_mac *mac = zd_hw_mac(hw);
494 struct sk_buff_head *q = &mac->ack_wait_queue;
Daniel Drake459c51a2007-11-19 15:00:29 +0000495 struct sk_buff *skb;
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200496 struct tx_status *tx_status = (struct tx_status *)urb->transfer_buffer;
497 unsigned long flags;
498 int success = !tx_status->failure;
499 int retry = tx_status->retry + success;
500 int found = 0;
501 int i, position = 0;
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000502
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200503 q = &mac->ack_wait_queue;
504 spin_lock_irqsave(&q->lock, flags);
Johannes Berg5078ed52008-02-20 12:09:25 +0100505
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200506 skb_queue_walk(q, skb) {
507 struct ieee80211_hdr *tx_hdr;
508 struct ieee80211_tx_info *info;
509 int first_idx, final_idx;
510 const struct tx_retry_rate *retries;
511 u8 final_rate;
512
513 position ++;
514
515 /* if the hardware reports a failure and we had a 802.11 ACK
516 * pending, then we skip the first skb when searching for a
517 * matching frame */
518 if (tx_status->failure && mac->ack_pending &&
519 skb_queue_is_first(q, skb)) {
520 continue;
521 }
522
523 tx_hdr = (struct ieee80211_hdr *)skb->data;
524
525 /* we skip all frames not matching the reported destination */
526 if (unlikely(memcmp(tx_hdr->addr1, tx_status->mac, ETH_ALEN))) {
527 continue;
528 }
529
530 /* we skip all frames not matching the reported final rate */
531
532 info = IEEE80211_SKB_CB(skb);
533 first_idx = info->status.rates[0].idx;
534 ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
535 retries = &zd_retry_rates[first_idx];
Dan Carpenter86baf712010-02-27 09:12:34 +0300536 if (retry <= 0 || retry > retries->count)
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200537 continue;
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200538
Dan Carpenter86baf712010-02-27 09:12:34 +0300539 final_idx = retries->rate[retry - 1];
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200540 final_rate = zd_rates[final_idx].hw_value;
541
542 if (final_rate != tx_status->rate) {
543 continue;
544 }
545
546 found = 1;
547 break;
548 }
549
550 if (found) {
551 for (i=1; i<=position; i++) {
552 skb = __skb_dequeue(q);
553 zd_mac_tx_status(hw, skb,
554 mac->ack_pending ? mac->ack_signal : 0,
555 i == position ? tx_status : NULL);
556 mac->ack_pending = 0;
557 }
558 }
559
560 spin_unlock_irqrestore(&q->lock, flags);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000561}
562
Daniel Drake459c51a2007-11-19 15:00:29 +0000563/**
564 * zd_mac_tx_to_dev - callback for USB layer
565 * @skb: a &sk_buff pointer
566 * @error: error value, 0 if transmission successful
567 *
568 * Informs the MAC layer that the frame has successfully transferred to the
569 * device. If an ACK is required and the transfer to the device has been
570 * successful, the packets are put on the @ack_wait_queue with
571 * the control set removed.
572 */
573void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
Daniel Drakee85d0912006-06-02 17:11:32 +0100574{
Johannes Berge039fa42008-05-15 12:55:29 +0200575 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200576 struct ieee80211_hw *hw = info->rate_driver_data[0];
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200577 struct zd_mac *mac = zd_hw_mac(hw);
578
579 ieee80211_tx_info_clear_status(info);
Daniel Drakee85d0912006-06-02 17:11:32 +0100580
Johannes Berge039fa42008-05-15 12:55:29 +0200581 skb_pull(skb, sizeof(struct zd_ctrlset));
582 if (unlikely(error ||
583 (info->flags & IEEE80211_TX_CTL_NO_ACK))) {
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200584 /*
585 * FIXME : do we need to fill in anything ?
586 */
587 ieee80211_tx_status_irqsafe(hw, skb);
Daniel Drake459c51a2007-11-19 15:00:29 +0000588 } else {
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200589 struct sk_buff_head *q = &mac->ack_wait_queue;
Johannes Berge039fa42008-05-15 12:55:29 +0200590
591 skb_queue_tail(q, skb);
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200592 while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS) {
593 zd_mac_tx_status(hw, skb_dequeue(q),
594 mac->ack_pending ? mac->ack_signal : 0,
595 NULL);
596 mac->ack_pending = 0;
597 }
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000598 }
Daniel Drakee85d0912006-06-02 17:11:32 +0100599}
600
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000601static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
Daniel Drakee85d0912006-06-02 17:11:32 +0100602{
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100603 /* ZD_PURE_RATE() must be used to remove the modulation type flag of
Daniel Drake459c51a2007-11-19 15:00:29 +0000604 * the zd-rate values.
605 */
Daniel Drakee85d0912006-06-02 17:11:32 +0100606 static const u8 rate_divisor[] = {
Daniel Drake459c51a2007-11-19 15:00:29 +0000607 [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
608 [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
609 /* Bits must be doubled. */
610 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
611 [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
612 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
613 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
614 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
615 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
616 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
617 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
618 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
619 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
Daniel Drakee85d0912006-06-02 17:11:32 +0100620 };
621
622 u32 bits = (u32)tx_length * 8;
623 u32 divisor;
624
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100625 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
Daniel Drakee85d0912006-06-02 17:11:32 +0100626 if (divisor == 0)
627 return -EINVAL;
628
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000629 switch (zd_rate) {
630 case ZD_CCK_RATE_5_5M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100631 bits = (2*bits) + 10; /* round up to the next integer */
632 break;
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000633 case ZD_CCK_RATE_11M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100634 if (service) {
635 u32 t = bits % 11;
636 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
637 if (0 < t && t <= 3) {
638 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
639 }
640 }
641 bits += 10; /* round up to the next integer */
642 break;
643 }
644
645 return bits/divisor;
646}
647
Daniel Drakee85d0912006-06-02 17:11:32 +0100648static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
Johannes Berge6a98542008-10-21 12:40:02 +0200649 struct ieee80211_hdr *header,
650 struct ieee80211_tx_info *info)
Daniel Drakee85d0912006-06-02 17:11:32 +0100651{
Daniel Drakee85d0912006-06-02 17:11:32 +0100652 /*
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000653 * CONTROL TODO:
Daniel Drakee85d0912006-06-02 17:11:32 +0100654 * - if backoff needed, enable bit 0
655 * - if burst (backoff not needed) disable bit 0
Daniel Drakee85d0912006-06-02 17:11:32 +0100656 */
657
658 cs->control = 0;
659
660 /* First fragment */
Johannes Berge6a98542008-10-21 12:40:02 +0200661 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
Daniel Drakee85d0912006-06-02 17:11:32 +0100662 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
663
Gábor Stefanik13bdcd92009-05-14 17:34:59 +0000664 /* No ACK expected (multicast, etc.) */
665 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
666 cs->control |= ZD_CS_NO_ACK;
Daniel Drakee85d0912006-06-02 17:11:32 +0100667
668 /* PS-POLL */
Harvey Harrison85365822008-06-14 23:33:39 -0700669 if (ieee80211_is_pspoll(header->frame_control))
Daniel Drakee85d0912006-06-02 17:11:32 +0100670 cs->control |= ZD_CS_PS_POLL_FRAME;
671
Johannes Berge6a98542008-10-21 12:40:02 +0200672 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000673 cs->control |= ZD_CS_RTS;
674
Johannes Berge6a98542008-10-21 12:40:02 +0200675 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000676 cs->control |= ZD_CS_SELF_CTS;
Daniel Drakee85d0912006-06-02 17:11:32 +0100677
678 /* FIXME: Management frame? */
679}
680
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +0300681static bool zd_mac_match_cur_beacon(struct zd_mac *mac, struct sk_buff *beacon)
682{
683 if (!mac->beacon.cur_beacon)
684 return false;
685
686 if (mac->beacon.cur_beacon->len != beacon->len)
687 return false;
688
689 return !memcmp(beacon->data, mac->beacon.cur_beacon->data, beacon->len);
690}
691
692static void zd_mac_free_cur_beacon_locked(struct zd_mac *mac)
693{
694 ZD_ASSERT(mutex_is_locked(&mac->chip.mutex));
695
696 kfree_skb(mac->beacon.cur_beacon);
697 mac->beacon.cur_beacon = NULL;
698}
699
700static void zd_mac_free_cur_beacon(struct zd_mac *mac)
701{
702 mutex_lock(&mac->chip.mutex);
703 zd_mac_free_cur_beacon_locked(mac);
704 mutex_unlock(&mac->chip.mutex);
705}
706
Jussi Kivilinnadde46732011-06-20 14:42:49 +0300707static int zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon,
708 bool in_intr)
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800709{
710 struct zd_mac *mac = zd_hw_mac(hw);
Jussi Kivilinna51272292011-01-31 20:49:05 +0200711 int r, ret, num_cmds, req_pos = 0;
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800712 u32 tmp, j = 0;
713 /* 4 more bytes for tail CRC */
714 u32 full_len = beacon->len + 4;
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200715 unsigned long end_jiffies, message_jiffies;
Jussi Kivilinna51272292011-01-31 20:49:05 +0200716 struct zd_ioreq32 *ioreqs;
717
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +0300718 mutex_lock(&mac->chip.mutex);
719
720 /* Check if hw already has this beacon. */
721 if (zd_mac_match_cur_beacon(mac, beacon)) {
722 r = 0;
723 goto out_nofree;
724 }
725
Jussi Kivilinna51272292011-01-31 20:49:05 +0200726 /* Alloc memory for full beacon write at once. */
727 num_cmds = 1 + zd_chip_is_zd1211b(&mac->chip) + full_len;
728 ioreqs = kmalloc(num_cmds * sizeof(struct zd_ioreq32), GFP_KERNEL);
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +0300729 if (!ioreqs) {
730 r = -ENOMEM;
731 goto out_nofree;
732 }
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100733
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200734 r = zd_iowrite32_locked(&mac->chip, 0, CR_BCN_FIFO_SEMAPHORE);
735 if (r < 0)
736 goto out;
737 r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
738 if (r < 0)
739 goto release_sema;
Jussi Kivilinnadde46732011-06-20 14:42:49 +0300740 if (in_intr && tmp & 0x2) {
741 r = -EBUSY;
742 goto release_sema;
743 }
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200744
745 end_jiffies = jiffies + HZ / 2; /*~500ms*/
746 message_jiffies = jiffies + HZ / 10; /*~100ms*/
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800747 while (tmp & 0x2) {
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200748 r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100749 if (r < 0)
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200750 goto release_sema;
751 if (time_is_before_eq_jiffies(message_jiffies)) {
752 message_jiffies = jiffies + HZ / 10;
753 dev_err(zd_mac_dev(mac),
754 "CR_BCN_FIFO_SEMAPHORE not ready\n");
755 if (time_is_before_eq_jiffies(end_jiffies)) {
756 dev_err(zd_mac_dev(mac),
757 "Giving up beacon config.\n");
758 r = -ETIMEDOUT;
Jussi Kivilinna3985a462011-01-31 20:50:02 +0200759 goto reset_device;
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800760 }
761 }
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200762 msleep(20);
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800763 }
764
Jussi Kivilinna51272292011-01-31 20:49:05 +0200765 ioreqs[req_pos].addr = CR_BCN_FIFO;
766 ioreqs[req_pos].value = full_len - 1;
767 req_pos++;
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100768 if (zd_chip_is_zd1211b(&mac->chip)) {
Jussi Kivilinna51272292011-01-31 20:49:05 +0200769 ioreqs[req_pos].addr = CR_BCN_LENGTH;
770 ioreqs[req_pos].value = full_len - 1;
771 req_pos++;
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100772 }
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800773
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100774 for (j = 0 ; j < beacon->len; j++) {
Jussi Kivilinna51272292011-01-31 20:49:05 +0200775 ioreqs[req_pos].addr = CR_BCN_FIFO;
776 ioreqs[req_pos].value = *((u8 *)(beacon->data + j));
777 req_pos++;
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100778 }
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800779
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100780 for (j = 0; j < 4; j++) {
Jussi Kivilinna51272292011-01-31 20:49:05 +0200781 ioreqs[req_pos].addr = CR_BCN_FIFO;
782 ioreqs[req_pos].value = 0x0;
783 req_pos++;
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100784 }
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800785
Jussi Kivilinna51272292011-01-31 20:49:05 +0200786 BUG_ON(req_pos != num_cmds);
787
788 r = zd_iowrite32a_locked(&mac->chip, ioreqs, num_cmds);
789
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200790release_sema:
791 /*
792 * Try very hard to release device beacon semaphore, as otherwise
793 * device/driver can be left in unusable state.
794 */
795 end_jiffies = jiffies + HZ / 2; /*~500ms*/
796 ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
797 while (ret < 0) {
Jussi Kivilinnadde46732011-06-20 14:42:49 +0300798 if (in_intr || time_is_before_eq_jiffies(end_jiffies)) {
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200799 ret = -ETIMEDOUT;
800 break;
801 }
802
803 msleep(20);
804 ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
805 }
806
807 if (ret < 0)
808 dev_err(zd_mac_dev(mac), "Could not release "
809 "CR_BCN_FIFO_SEMAPHORE!\n");
810 if (r < 0 || ret < 0) {
811 if (r >= 0)
812 r = ret;
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +0300813
814 /* We don't know if beacon was written successfully or not,
815 * so clear current. */
816 zd_mac_free_cur_beacon_locked(mac);
817
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200818 goto out;
819 }
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100820
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +0300821 /* Beacon has now been written successfully, update current. */
822 zd_mac_free_cur_beacon_locked(mac);
823 mac->beacon.cur_beacon = beacon;
824 beacon = NULL;
825
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800826 /* 802.11b/g 2.4G CCK 1Mb
827 * 802.11a, not yet implemented, uses different values (see GPL vendor
828 * driver)
829 */
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200830 r = zd_iowrite32_locked(&mac->chip, 0x00000400 | (full_len << 19),
831 CR_BCN_PLCP_CFG);
832out:
Jussi Kivilinna51272292011-01-31 20:49:05 +0200833 kfree(ioreqs);
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +0300834out_nofree:
835 kfree_skb(beacon);
836 mutex_unlock(&mac->chip.mutex);
837
Jussi Kivilinna9be23252011-01-31 20:48:55 +0200838 return r;
Jussi Kivilinna3985a462011-01-31 20:50:02 +0200839
840reset_device:
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +0300841 zd_mac_free_cur_beacon_locked(mac);
842 kfree_skb(beacon);
843
Jussi Kivilinna3985a462011-01-31 20:50:02 +0200844 mutex_unlock(&mac->chip.mutex);
845 kfree(ioreqs);
846
847 /* semaphore stuck, reset device to avoid fw freeze later */
848 dev_warn(zd_mac_dev(mac), "CR_BCN_FIFO_SEMAPHORE stuck, "
849 "reseting device...");
850 usb_queue_reset_device(mac->chip.usb.intf);
851
852 return r;
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800853}
854
Daniel Drakee85d0912006-06-02 17:11:32 +0100855static int fill_ctrlset(struct zd_mac *mac,
Johannes Berge039fa42008-05-15 12:55:29 +0200856 struct sk_buff *skb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100857{
858 int r;
Daniel Drake459c51a2007-11-19 15:00:29 +0000859 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
860 unsigned int frag_len = skb->len + FCS_LEN;
Daniel Drakee85d0912006-06-02 17:11:32 +0100861 unsigned int packet_length;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200862 struct ieee80211_rate *txrate;
Daniel Drakee85d0912006-06-02 17:11:32 +0100863 struct zd_ctrlset *cs = (struct zd_ctrlset *)
864 skb_push(skb, sizeof(struct zd_ctrlset));
Johannes Berge039fa42008-05-15 12:55:29 +0200865 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100866
Daniel Drakee85d0912006-06-02 17:11:32 +0100867 ZD_ASSERT(frag_len <= 0xffff);
Daniel Drakee85d0912006-06-02 17:11:32 +0100868
Tomas Vaneke81a7bd2012-02-05 15:51:53 +0200869 /*
870 * Firmware computes the duration itself (for all frames except PSPoll)
871 * and needs the field set to 0 at input, otherwise firmware messes up
872 * duration_id and sets bits 14 and 15 on.
873 */
874 if (!ieee80211_is_pspoll(hdr->frame_control))
875 hdr->duration_id = 0;
876
Johannes Berge039fa42008-05-15 12:55:29 +0200877 txrate = ieee80211_get_tx_rate(mac->hw, info);
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200878
879 cs->modulation = txrate->hw_value;
Johannes Berge6a98542008-10-21 12:40:02 +0200880 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200881 cs->modulation = txrate->hw_value_short;
Daniel Drakee85d0912006-06-02 17:11:32 +0100882
883 cs->tx_length = cpu_to_le16(frag_len);
884
Johannes Berge6a98542008-10-21 12:40:02 +0200885 cs_set_control(mac, cs, hdr, info);
Daniel Drakee85d0912006-06-02 17:11:32 +0100886
887 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
888 ZD_ASSERT(packet_length <= 0xffff);
889 /* ZD1211B: Computing the length difference this way, gives us
890 * flexibility to compute the packet length.
891 */
Daniel Drake74553ae2007-07-01 18:22:32 +0100892 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
Daniel Drakee85d0912006-06-02 17:11:32 +0100893 packet_length - frag_len : packet_length);
894
895 /*
896 * CURRENT LENGTH:
897 * - transmit frame length in microseconds
898 * - seems to be derived from frame length
899 * - see Cal_Us_Service() in zdinlinef.h
900 * - if macp->bTxBurstEnable is enabled, then multiply by 4
901 * - bTxBurstEnable is never set in the vendor driver
902 *
903 * SERVICE:
904 * - "for PLCP configuration"
905 * - always 0 except in some situations at 802.11b 11M
906 * - see line 53 of zdinlinef.h
907 */
908 cs->service = 0;
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100909 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
Daniel Drakee85d0912006-06-02 17:11:32 +0100910 le16_to_cpu(cs->tx_length));
911 if (r < 0)
912 return r;
913 cs->current_length = cpu_to_le16(r);
Daniel Drake459c51a2007-11-19 15:00:29 +0000914 cs->next_frame_length = 0;
Daniel Drakee85d0912006-06-02 17:11:32 +0100915
916 return 0;
917}
918
Daniel Drake459c51a2007-11-19 15:00:29 +0000919/**
920 * zd_op_tx - transmits a network frame to the device
Ulrich Kunitz741fec52006-11-22 00:05:53 +0000921 *
Daniel Drake459c51a2007-11-19 15:00:29 +0000922 * @dev: mac80211 hardware device
923 * @skb: socket buffer
924 * @control: the control structure
Daniel Drakee85d0912006-06-02 17:11:32 +0100925 *
Daniel Drake459c51a2007-11-19 15:00:29 +0000926 * This function transmit an IEEE 802.11 network frame to the device. The
927 * control block of the skbuff will be initialized. If necessary the incoming
928 * mac80211 queues will be stopped.
Daniel Drakee85d0912006-06-02 17:11:32 +0100929 */
Johannes Berg7bb45682011-02-24 14:42:06 +0100930static void zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100931{
Daniel Drake459c51a2007-11-19 15:00:29 +0000932 struct zd_mac *mac = zd_hw_mac(hw);
Johannes Berge039fa42008-05-15 12:55:29 +0200933 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Daniel Drake459c51a2007-11-19 15:00:29 +0000934 int r;
Daniel Drakee85d0912006-06-02 17:11:32 +0100935
Johannes Berge039fa42008-05-15 12:55:29 +0200936 r = fill_ctrlset(mac, skb);
Daniel Drake459c51a2007-11-19 15:00:29 +0000937 if (r)
Jouni Malinen640c65e2009-03-16 21:47:33 +0200938 goto fail;
Daniel Drakee85d0912006-06-02 17:11:32 +0100939
Johannes Berge6a98542008-10-21 12:40:02 +0200940 info->rate_driver_data[0] = hw;
Johannes Berge039fa42008-05-15 12:55:29 +0200941
942 r = zd_usb_tx(&mac->chip.usb, skb);
Daniel Drake459c51a2007-11-19 15:00:29 +0000943 if (r)
Jouni Malinen640c65e2009-03-16 21:47:33 +0200944 goto fail;
Johannes Berg7bb45682011-02-24 14:42:06 +0100945 return;
Jouni Malinen640c65e2009-03-16 21:47:33 +0200946
947fail:
948 dev_kfree_skb(skb);
Daniel Drake459c51a2007-11-19 15:00:29 +0000949}
950
951/**
952 * filter_ack - filters incoming packets for acknowledgements
953 * @dev: the mac80211 device
954 * @rx_hdr: received header
955 * @stats: the status for the received packet
956 *
957 * This functions looks for ACK packets and tries to match them with the
958 * frames in the tx queue. If a match is found the frame will be dequeued and
959 * the upper layers is informed about the successful transmission. If
960 * mac80211 queues have been stopped and the number of frames still to be
961 * transmitted is low the queues will be opened again.
962 *
963 * Returns 1 if the frame was an ACK, 0 if it was ignored.
964 */
965static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
966 struct ieee80211_rx_status *stats)
967{
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200968 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drake459c51a2007-11-19 15:00:29 +0000969 struct sk_buff *skb;
970 struct sk_buff_head *q;
971 unsigned long flags;
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200972 int found = 0;
973 int i, position = 0;
Daniel Drake459c51a2007-11-19 15:00:29 +0000974
Harvey Harrison85365822008-06-14 23:33:39 -0700975 if (!ieee80211_is_ack(rx_hdr->frame_control))
Daniel Drake459c51a2007-11-19 15:00:29 +0000976 return 0;
977
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200978 q = &mac->ack_wait_queue;
Daniel Drake459c51a2007-11-19 15:00:29 +0000979 spin_lock_irqsave(&q->lock, flags);
David S. Miller47a227d2008-09-23 00:23:30 -0700980 skb_queue_walk(q, skb) {
Daniel Drake459c51a2007-11-19 15:00:29 +0000981 struct ieee80211_hdr *tx_hdr;
982
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200983 position ++;
984
985 if (mac->ack_pending && skb_queue_is_first(q, skb))
986 continue;
987
Daniel Drake459c51a2007-11-19 15:00:29 +0000988 tx_hdr = (struct ieee80211_hdr *)skb->data;
Shaddy Baddahcde69012008-11-28 17:10:45 +1100989 if (likely(!memcmp(tx_hdr->addr2, rx_hdr->addr1, ETH_ALEN)))
Daniel Drake459c51a2007-11-19 15:00:29 +0000990 {
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200991 found = 1;
992 break;
Daniel Drake459c51a2007-11-19 15:00:29 +0000993 }
994 }
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +0200995
996 if (found) {
997 for (i=1; i<position; i++) {
998 skb = __skb_dequeue(q);
999 zd_mac_tx_status(hw, skb,
1000 mac->ack_pending ? mac->ack_signal : 0,
1001 NULL);
1002 mac->ack_pending = 0;
1003 }
1004
1005 mac->ack_pending = 1;
1006 mac->ack_signal = stats->signal;
Jussi Kivilinnaf773e402011-01-31 20:48:16 +02001007
1008 /* Prevent pending tx-packet on AP-mode */
1009 if (mac->type == NL80211_IFTYPE_AP) {
1010 skb = __skb_dequeue(q);
1011 zd_mac_tx_status(hw, skb, mac->ack_signal, NULL);
1012 mac->ack_pending = 0;
1013 }
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +02001014 }
1015
Daniel Drake459c51a2007-11-19 15:00:29 +00001016 spin_unlock_irqrestore(&q->lock, flags);
1017 return 1;
1018}
1019
1020int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
1021{
1022 struct zd_mac *mac = zd_hw_mac(hw);
1023 struct ieee80211_rx_status stats;
1024 const struct rx_status *status;
1025 struct sk_buff *skb;
1026 int bad_frame = 0;
Harvey Harrison85365822008-06-14 23:33:39 -07001027 __le16 fc;
1028 int need_padding;
Johannes Berg8318d782008-01-24 19:38:38 +01001029 int i;
1030 u8 rate;
Daniel Drake459c51a2007-11-19 15:00:29 +00001031
1032 if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
1033 FCS_LEN + sizeof(struct rx_status))
Daniel Drakee85d0912006-06-02 17:11:32 +01001034 return -EINVAL;
1035
Daniel Drake459c51a2007-11-19 15:00:29 +00001036 memset(&stats, 0, sizeof(stats));
Daniel Drakee85d0912006-06-02 17:11:32 +01001037
Daniel Drake459c51a2007-11-19 15:00:29 +00001038 /* Note about pass_failed_fcs and pass_ctrl access below:
1039 * mac locking intentionally omitted here, as this is the only unlocked
1040 * reader and the only writer is configure_filter. Plus, if there were
1041 * any races accessing these variables, it wouldn't really matter.
1042 * If mac80211 ever provides a way for us to access filter flags
1043 * from outside configure_filter, we could improve on this. Also, this
1044 * situation may change once we implement some kind of DMA-into-skb
1045 * RX path. */
Daniel Drakee85d0912006-06-02 17:11:32 +01001046
Daniel Drake459c51a2007-11-19 15:00:29 +00001047 /* Caller has to ensure that length >= sizeof(struct rx_status). */
1048 status = (struct rx_status *)
Ulrich Kunitz937a0492007-10-02 18:36:53 +01001049 (buffer + (length - sizeof(struct rx_status)));
Daniel Drakee85d0912006-06-02 17:11:32 +01001050 if (status->frame_status & ZD_RX_ERROR) {
Daniel Drake459c51a2007-11-19 15:00:29 +00001051 if (mac->pass_failed_fcs &&
1052 (status->frame_status & ZD_RX_CRC32_ERROR)) {
1053 stats.flag |= RX_FLAG_FAILED_FCS_CRC;
1054 bad_frame = 1;
1055 } else {
1056 return -EINVAL;
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001057 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001058 }
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001059
Johannes Berg8318d782008-01-24 19:38:38 +01001060 stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
1061 stats.band = IEEE80211_BAND_2GHZ;
Jussi Kivilinna7a1d6562011-06-20 14:42:28 +03001062 stats.signal = zd_check_signal(hw, status->signal_strength);
Johannes Berg8318d782008-01-24 19:38:38 +01001063
1064 rate = zd_rx_rate(buffer, status);
1065
1066 /* todo: return index in the big switches in zd_rx_rate instead */
1067 for (i = 0; i < mac->band.n_bitrates; i++)
1068 if (rate == mac->band.bitrates[i].hw_value)
1069 stats.rate_idx = i;
Daniel Drakee85d0912006-06-02 17:11:32 +01001070
Daniel Drake459c51a2007-11-19 15:00:29 +00001071 length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
1072 buffer += ZD_PLCP_HEADER_SIZE;
Daniel Drakee85d0912006-06-02 17:11:32 +01001073
Daniel Drake459c51a2007-11-19 15:00:29 +00001074 /* Except for bad frames, filter each frame to see if it is an ACK, in
1075 * which case our internal TX tracking is updated. Normally we then
1076 * bail here as there's no need to pass ACKs on up to the stack, but
1077 * there is also the case where the stack has requested us to pass
1078 * control frames on up (pass_ctrl) which we must consider. */
1079 if (!bad_frame &&
1080 filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
1081 && !mac->pass_ctrl)
1082 return 0;
Daniel Drakee85d0912006-06-02 17:11:32 +01001083
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001084 fc = get_unaligned((__le16*)buffer);
Harvey Harrison85365822008-06-14 23:33:39 -07001085 need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
Michael Buesch90817282007-12-29 17:24:23 +01001086
1087 skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
Daniel Drake459c51a2007-11-19 15:00:29 +00001088 if (skb == NULL)
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001089 return -ENOMEM;
Michael Buesch90817282007-12-29 17:24:23 +01001090 if (need_padding) {
Walter Goldens77c20612010-05-18 04:44:54 -07001091 /* Make sure the payload data is 4 byte aligned. */
Michael Buesch90817282007-12-29 17:24:23 +01001092 skb_reserve(skb, 2);
1093 }
1094
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +02001095 /* FIXME : could we avoid this big memcpy ? */
Daniel Drake459c51a2007-11-19 15:00:29 +00001096 memcpy(skb_put(skb, length), buffer, length);
1097
Johannes Bergf1d58c22009-06-17 13:13:00 +02001098 memcpy(IEEE80211_SKB_RXCB(skb), &stats, sizeof(stats));
1099 ieee80211_rx_irqsafe(hw, skb);
Daniel Drakee85d0912006-06-02 17:11:32 +01001100 return 0;
1101}
1102
Daniel Drake459c51a2007-11-19 15:00:29 +00001103static int zd_op_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001104 struct ieee80211_vif *vif)
Daniel Drakee85d0912006-06-02 17:11:32 +01001105{
Daniel Drake459c51a2007-11-19 15:00:29 +00001106 struct zd_mac *mac = zd_hw_mac(hw);
1107
Johannes Berg05c914f2008-09-11 00:01:58 +02001108 /* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */
1109 if (mac->type != NL80211_IFTYPE_UNSPECIFIED)
Daniel Drake459c51a2007-11-19 15:00:29 +00001110 return -EOPNOTSUPP;
1111
Johannes Berg1ed32e42009-12-23 13:15:45 +01001112 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001113 case NL80211_IFTYPE_MONITOR:
1114 case NL80211_IFTYPE_MESH_POINT:
1115 case NL80211_IFTYPE_STATION:
1116 case NL80211_IFTYPE_ADHOC:
Jussi Kivilinnaab419e92011-01-31 20:50:21 +02001117 case NL80211_IFTYPE_AP:
Johannes Berg1ed32e42009-12-23 13:15:45 +01001118 mac->type = vif->type;
Daniel Drake459c51a2007-11-19 15:00:29 +00001119 break;
1120 default:
1121 return -EOPNOTSUPP;
1122 }
1123
Jussi Kivilinnac2fadcb2011-01-31 20:48:06 +02001124 mac->vif = vif;
1125
1126 return set_mac_and_bssid(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +01001127}
1128
Daniel Drake459c51a2007-11-19 15:00:29 +00001129static void zd_op_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001130 struct ieee80211_vif *vif)
Daniel Drakee85d0912006-06-02 17:11:32 +01001131{
Daniel Drake459c51a2007-11-19 15:00:29 +00001132 struct zd_mac *mac = zd_hw_mac(hw);
Johannes Berg05c914f2008-09-11 00:01:58 +02001133 mac->type = NL80211_IFTYPE_UNSPECIFIED;
Jussi Kivilinnac2fadcb2011-01-31 20:48:06 +02001134 mac->vif = NULL;
Jussi Kivilinnab91a5152011-01-31 20:48:25 +02001135 zd_set_beacon_interval(&mac->chip, 0, 0, NL80211_IFTYPE_UNSPECIFIED);
Daniel Drake459c51a2007-11-19 15:00:29 +00001136 zd_write_mac_addr(&mac->chip, NULL);
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +03001137
1138 zd_mac_free_cur_beacon(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +01001139}
1140
Johannes Berge8975582008-10-09 12:18:51 +02001141static int zd_op_config(struct ieee80211_hw *hw, u32 changed)
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 Berge8975582008-10-09 12:18:51 +02001144 struct ieee80211_conf *conf = &hw->conf;
1145
Jussi Kivilinna212e1a52011-01-31 20:49:43 +02001146 spin_lock_irq(&mac->lock);
1147 mac->channel = conf->channel->hw_value;
1148 spin_unlock_irq(&mac->lock);
1149
Johannes Berg8318d782008-01-24 19:38:38 +01001150 return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
Daniel Drakee85d0912006-06-02 17:11:32 +01001151}
1152
Jussi Kivilinna4099e2f2011-01-31 20:48:35 +02001153static void zd_beacon_done(struct zd_mac *mac)
1154{
1155 struct sk_buff *skb, *beacon;
1156
Jussi Kivilinna9be23252011-01-31 20:48:55 +02001157 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1158 return;
Jussi Kivilinna4099e2f2011-01-31 20:48:35 +02001159 if (!mac->vif || mac->vif->type != NL80211_IFTYPE_AP)
1160 return;
1161
1162 /*
1163 * Send out buffered broad- and multicast frames.
1164 */
1165 while (!ieee80211_queue_stopped(mac->hw, 0)) {
1166 skb = ieee80211_get_buffered_bc(mac->hw, mac->vif);
1167 if (!skb)
1168 break;
1169 zd_op_tx(mac->hw, skb);
1170 }
1171
1172 /*
1173 * Fetch next beacon so that tim_count is updated.
1174 */
1175 beacon = ieee80211_beacon_get(mac->hw, mac->vif);
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +03001176 if (beacon)
Jussi Kivilinnadde46732011-06-20 14:42:49 +03001177 zd_mac_config_beacon(mac->hw, beacon, true);
Jussi Kivilinna4099e2f2011-01-31 20:48:35 +02001178
Jussi Kivilinna9be23252011-01-31 20:48:55 +02001179 spin_lock_irq(&mac->lock);
1180 mac->beacon.last_update = jiffies;
1181 spin_unlock_irq(&mac->lock);
Jussi Kivilinna4099e2f2011-01-31 20:48:35 +02001182}
1183
Luis R. Rodrigueze83a1072008-09-09 23:19:49 -07001184static void zd_process_intr(struct work_struct *work)
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -08001185{
1186 u16 int_status;
Jussi Kivilinna8b17f752011-01-31 20:47:27 +02001187 unsigned long flags;
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -08001188 struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
1189
Jussi Kivilinna8b17f752011-01-31 20:47:27 +02001190 spin_lock_irqsave(&mac->lock, flags);
1191 int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer + 4));
1192 spin_unlock_irqrestore(&mac->lock, flags);
1193
Jussi Kivilinna4099e2f2011-01-31 20:48:35 +02001194 if (int_status & INT_CFG_NEXT_BCN) {
1195 /*dev_dbg_f_limit(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");*/
1196 zd_beacon_done(mac);
1197 } else {
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -08001198 dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
Jussi Kivilinna4099e2f2011-01-31 20:48:35 +02001199 }
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -08001200
1201 zd_chip_enable_hwint(&mac->chip);
1202}
1203
1204
Johannes Berg3ac64be2009-08-17 16:16:53 +02001205static u64 zd_op_prepare_multicast(struct ieee80211_hw *hw,
Jiri Pirko22bedad2010-04-01 21:22:57 +00001206 struct netdev_hw_addr_list *mc_list)
Johannes Berg3ac64be2009-08-17 16:16:53 +02001207{
1208 struct zd_mac *mac = zd_hw_mac(hw);
1209 struct zd_mc_hash hash;
Jiri Pirko22bedad2010-04-01 21:22:57 +00001210 struct netdev_hw_addr *ha;
Johannes Berg3ac64be2009-08-17 16:16:53 +02001211
1212 zd_mc_clear(&hash);
1213
Jiri Pirko22bedad2010-04-01 21:22:57 +00001214 netdev_hw_addr_list_for_each(ha, mc_list) {
1215 dev_dbg_f(zd_mac_dev(mac), "mc addr %pM\n", ha->addr);
1216 zd_mc_add_addr(&hash, ha->addr);
Johannes Berg3ac64be2009-08-17 16:16:53 +02001217 }
1218
1219 return hash.low | ((u64)hash.high << 32);
1220}
1221
Daniel Drake459c51a2007-11-19 15:00:29 +00001222#define SUPPORTED_FIF_FLAGS \
1223 (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
Michael Buesch2c1a1b12008-02-10 16:03:55 +01001224 FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
Daniel Drake459c51a2007-11-19 15:00:29 +00001225static void zd_op_configure_filter(struct ieee80211_hw *hw,
1226 unsigned int changed_flags,
1227 unsigned int *new_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001228 u64 multicast)
Daniel Drake459c51a2007-11-19 15:00:29 +00001229{
Johannes Berg3ac64be2009-08-17 16:16:53 +02001230 struct zd_mc_hash hash = {
1231 .low = multicast,
1232 .high = multicast >> 32,
1233 };
Daniel Drake459c51a2007-11-19 15:00:29 +00001234 struct zd_mac *mac = zd_hw_mac(hw);
1235 unsigned long flags;
Jussi Kivilinnaa6fb0712011-01-31 20:47:46 +02001236 int r;
Daniel Drake459c51a2007-11-19 15:00:29 +00001237
1238 /* Only deal with supported flags */
1239 changed_flags &= SUPPORTED_FIF_FLAGS;
1240 *new_flags &= SUPPORTED_FIF_FLAGS;
1241
Benoit Papillault7de3c5d2010-01-03 10:20:01 +01001242 /*
1243 * If multicast parameter (as returned by zd_op_prepare_multicast)
1244 * has changed, no bit in changed_flags is set. To handle this
1245 * situation, we do not return if changed_flags is 0. If we do so,
1246 * we will have some issue with IPv6 which uses multicast for link
1247 * layer address resolution.
1248 */
Johannes Berg3ac64be2009-08-17 16:16:53 +02001249 if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI))
Daniel Drake459c51a2007-11-19 15:00:29 +00001250 zd_mc_add_all(&hash);
Daniel Drake459c51a2007-11-19 15:00:29 +00001251
1252 spin_lock_irqsave(&mac->lock, flags);
1253 mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
1254 mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
1255 mac->multicast_hash = hash;
1256 spin_unlock_irqrestore(&mac->lock, flags);
Johannes Berg3ac64be2009-08-17 16:16:53 +02001257
Jussi Kivilinnaa6fb0712011-01-31 20:47:46 +02001258 zd_chip_set_multicast_hash(&mac->chip, &hash);
Daniel Drake459c51a2007-11-19 15:00:29 +00001259
Jussi Kivilinnaa6fb0712011-01-31 20:47:46 +02001260 if (changed_flags & FIF_CONTROL) {
1261 r = set_rx_filter(mac);
1262 if (r)
1263 dev_err(zd_mac_dev(mac), "set_rx_filter error %d\n", r);
1264 }
Daniel Drake459c51a2007-11-19 15:00:29 +00001265
1266 /* no handling required for FIF_OTHER_BSS as we don't currently
1267 * do BSSID filtering */
1268 /* FIXME: in future it would be nice to enable the probe response
1269 * filter (so that the driver doesn't see them) until
1270 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
1271 * have to schedule work to enable prbresp reception, which might
1272 * happen too late. For now we'll just listen and forward them all the
1273 * time. */
1274}
1275
Jussi Kivilinna5cf6cf82011-01-31 20:47:56 +02001276static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble)
Daniel Drake459c51a2007-11-19 15:00:29 +00001277{
Daniel Drake459c51a2007-11-19 15:00:29 +00001278 mutex_lock(&mac->chip.mutex);
Daniel Drake459c51a2007-11-19 15:00:29 +00001279 zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
1280 mutex_unlock(&mac->chip.mutex);
1281}
1282
Johannes Berg471b3ef2007-12-28 14:32:58 +01001283static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
1284 struct ieee80211_vif *vif,
1285 struct ieee80211_bss_conf *bss_conf,
1286 u32 changes)
Daniel Drake459c51a2007-11-19 15:00:29 +00001287{
1288 struct zd_mac *mac = zd_hw_mac(hw);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001289 int associated;
Daniel Drake459c51a2007-11-19 15:00:29 +00001290
1291 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
1292
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001293 if (mac->type == NL80211_IFTYPE_MESH_POINT ||
Jussi Kivilinnaab419e92011-01-31 20:50:21 +02001294 mac->type == NL80211_IFTYPE_ADHOC ||
1295 mac->type == NL80211_IFTYPE_AP) {
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001296 associated = true;
1297 if (changes & BSS_CHANGED_BEACON) {
1298 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
1299
1300 if (beacon) {
Jussi Kivilinna9be23252011-01-31 20:48:55 +02001301 zd_chip_disable_hwint(&mac->chip);
Jussi Kivilinnadde46732011-06-20 14:42:49 +03001302 zd_mac_config_beacon(hw, beacon, false);
Jussi Kivilinna9be23252011-01-31 20:48:55 +02001303 zd_chip_enable_hwint(&mac->chip);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001304 }
1305 }
1306
1307 if (changes & BSS_CHANGED_BEACON_ENABLED) {
Jussi Kivilinnab91a5152011-01-31 20:48:25 +02001308 u16 interval = 0;
1309 u8 period = 0;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001310
Jussi Kivilinnab91a5152011-01-31 20:48:25 +02001311 if (bss_conf->enable_beacon) {
1312 period = bss_conf->dtim_period;
1313 interval = bss_conf->beacon_int;
1314 }
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001315
Jussi Kivilinna9be23252011-01-31 20:48:55 +02001316 spin_lock_irq(&mac->lock);
1317 mac->beacon.period = period;
1318 mac->beacon.interval = interval;
1319 mac->beacon.last_update = jiffies;
1320 spin_unlock_irq(&mac->lock);
1321
Jussi Kivilinnab91a5152011-01-31 20:48:25 +02001322 zd_set_beacon_interval(&mac->chip, interval, period,
1323 mac->type);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001324 }
1325 } else
1326 associated = is_valid_ether_addr(bss_conf->bssid);
1327
1328 spin_lock_irq(&mac->lock);
1329 mac->associated = associated;
1330 spin_unlock_irq(&mac->lock);
1331
1332 /* TODO: do hardware bssid filtering */
1333
Johannes Berg471b3ef2007-12-28 14:32:58 +01001334 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Jussi Kivilinna5cf6cf82011-01-31 20:47:56 +02001335 spin_lock_irq(&mac->lock);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001336 mac->short_preamble = bss_conf->use_short_preamble;
Jussi Kivilinna5cf6cf82011-01-31 20:47:56 +02001337 spin_unlock_irq(&mac->lock);
1338
1339 set_rts_cts(mac, bss_conf->use_short_preamble);
Daniel Drake459c51a2007-11-19 15:00:29 +00001340 }
1341}
1342
Eliad Peller37a41b42011-09-21 14:06:11 +03001343static u64 zd_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Alina Friedrichsen5fe73192009-02-25 00:49:18 +01001344{
1345 struct zd_mac *mac = zd_hw_mac(hw);
1346 return zd_chip_get_tsf(&mac->chip);
1347}
1348
Daniel Drake459c51a2007-11-19 15:00:29 +00001349static const struct ieee80211_ops zd_ops = {
1350 .tx = zd_op_tx,
1351 .start = zd_op_start,
1352 .stop = zd_op_stop,
1353 .add_interface = zd_op_add_interface,
1354 .remove_interface = zd_op_remove_interface,
1355 .config = zd_op_config,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001356 .prepare_multicast = zd_op_prepare_multicast,
Daniel Drake459c51a2007-11-19 15:00:29 +00001357 .configure_filter = zd_op_configure_filter,
Johannes Berg471b3ef2007-12-28 14:32:58 +01001358 .bss_info_changed = zd_op_bss_info_changed,
Alina Friedrichsen5fe73192009-02-25 00:49:18 +01001359 .get_tsf = zd_op_get_tsf,
Daniel Drake459c51a2007-11-19 15:00:29 +00001360};
1361
1362struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
1363{
1364 struct zd_mac *mac;
1365 struct ieee80211_hw *hw;
Daniel Drake459c51a2007-11-19 15:00:29 +00001366
1367 hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
1368 if (!hw) {
1369 dev_dbg_f(&intf->dev, "out of memory\n");
1370 return NULL;
1371 }
1372
1373 mac = zd_hw_mac(hw);
1374
1375 memset(mac, 0, sizeof(*mac));
1376 spin_lock_init(&mac->lock);
1377 mac->hw = hw;
1378
Johannes Berg05c914f2008-09-11 00:01:58 +02001379 mac->type = NL80211_IFTYPE_UNSPECIFIED;
Daniel Drake459c51a2007-11-19 15:00:29 +00001380
1381 memcpy(mac->channels, zd_channels, sizeof(zd_channels));
1382 memcpy(mac->rates, zd_rates, sizeof(zd_rates));
Johannes Berg8318d782008-01-24 19:38:38 +01001383 mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
1384 mac->band.bitrates = mac->rates;
1385 mac->band.n_channels = ARRAY_SIZE(zd_channels);
1386 mac->band.channels = mac->channels;
Daniel Drake459c51a2007-11-19 15:00:29 +00001387
Johannes Berg8318d782008-01-24 19:38:38 +01001388 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
1389
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -08001390 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
Jussi Kivilinna4099e2f2011-01-31 20:48:35 +02001391 IEEE80211_HW_SIGNAL_UNSPEC |
1392 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
Daniel Drake459c51a2007-11-19 15:00:29 +00001393
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07001394 hw->wiphy->interface_modes =
1395 BIT(NL80211_IFTYPE_MESH_POINT) |
1396 BIT(NL80211_IFTYPE_STATION) |
Jussi Kivilinnaab419e92011-01-31 20:50:21 +02001397 BIT(NL80211_IFTYPE_ADHOC) |
1398 BIT(NL80211_IFTYPE_AP);
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07001399
Bruno Randolf566bfe52008-05-08 19:15:40 +02001400 hw->max_signal = 100;
Daniel Drake459c51a2007-11-19 15:00:29 +00001401 hw->queues = 1;
1402 hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
1403
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +02001404 /*
1405 * Tell mac80211 that we support multi rate retries
1406 */
1407 hw->max_rates = IEEE80211_TX_MAX_RATES;
1408 hw->max_rate_tries = 18; /* 9 rates * 2 retries/rate */
1409
Daniel Drake459c51a2007-11-19 15:00:29 +00001410 skb_queue_head_init(&mac->ack_wait_queue);
Benoit PAPILLAULT7f4013f2009-10-22 12:04:52 +02001411 mac->ack_pending = 0;
Daniel Drake459c51a2007-11-19 15:00:29 +00001412
Daniel Drake459c51a2007-11-19 15:00:29 +00001413 zd_chip_init(&mac->chip, hw, intf);
1414 housekeeping_init(mac);
Jussi Kivilinna9be23252011-01-31 20:48:55 +02001415 beacon_init(mac);
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -08001416 INIT_WORK(&mac->process_intr, zd_process_intr);
Daniel Drake459c51a2007-11-19 15:00:29 +00001417
1418 SET_IEEE80211_DEV(hw, &intf->dev);
1419 return hw;
Daniel Drakee85d0912006-06-02 17:11:32 +01001420}
1421
Jussi Kivilinna9be23252011-01-31 20:48:55 +02001422#define BEACON_WATCHDOG_DELAY round_jiffies_relative(HZ)
1423
1424static void beacon_watchdog_handler(struct work_struct *work)
1425{
1426 struct zd_mac *mac =
1427 container_of(work, struct zd_mac, beacon.watchdog_work.work);
1428 struct sk_buff *beacon;
1429 unsigned long timeout;
1430 int interval, period;
1431
1432 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1433 goto rearm;
1434 if (mac->type != NL80211_IFTYPE_AP || !mac->vif)
1435 goto rearm;
1436
1437 spin_lock_irq(&mac->lock);
1438 interval = mac->beacon.interval;
1439 period = mac->beacon.period;
Jussi Kivilinna55f77822011-06-20 14:42:54 +03001440 timeout = mac->beacon.last_update +
1441 msecs_to_jiffies(interval * 1024 / 1000) * 3;
Jussi Kivilinna9be23252011-01-31 20:48:55 +02001442 spin_unlock_irq(&mac->lock);
1443
1444 if (interval > 0 && time_is_before_jiffies(timeout)) {
1445 dev_dbg_f(zd_mac_dev(mac), "beacon interrupt stalled, "
1446 "restarting. "
1447 "(interval: %d, dtim: %d)\n",
1448 interval, period);
1449
1450 zd_chip_disable_hwint(&mac->chip);
1451
1452 beacon = ieee80211_beacon_get(mac->hw, mac->vif);
1453 if (beacon) {
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +03001454 zd_mac_free_cur_beacon(mac);
1455
Jussi Kivilinnadde46732011-06-20 14:42:49 +03001456 zd_mac_config_beacon(mac->hw, beacon, false);
Jussi Kivilinna9be23252011-01-31 20:48:55 +02001457 }
1458
1459 zd_set_beacon_interval(&mac->chip, interval, period, mac->type);
1460
1461 zd_chip_enable_hwint(&mac->chip);
1462
1463 spin_lock_irq(&mac->lock);
1464 mac->beacon.last_update = jiffies;
1465 spin_unlock_irq(&mac->lock);
1466 }
1467
1468rearm:
1469 queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
1470 BEACON_WATCHDOG_DELAY);
1471}
1472
1473static void beacon_init(struct zd_mac *mac)
1474{
1475 INIT_DELAYED_WORK(&mac->beacon.watchdog_work, beacon_watchdog_handler);
1476}
1477
1478static void beacon_enable(struct zd_mac *mac)
1479{
1480 dev_dbg_f(zd_mac_dev(mac), "\n");
1481
1482 mac->beacon.last_update = jiffies;
1483 queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
1484 BEACON_WATCHDOG_DELAY);
1485}
1486
1487static void beacon_disable(struct zd_mac *mac)
1488{
1489 dev_dbg_f(zd_mac_dev(mac), "\n");
1490 cancel_delayed_work_sync(&mac->beacon.watchdog_work);
Jussi Kivilinnaf762d8c2011-06-20 14:42:39 +03001491
1492 zd_mac_free_cur_beacon(mac);
Jussi Kivilinna9be23252011-01-31 20:48:55 +02001493}
1494
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001495#define LINK_LED_WORK_DELAY HZ
1496
David Howellsc4028952006-11-22 14:57:56 +00001497static void link_led_handler(struct work_struct *work)
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001498{
David Howellsc4028952006-11-22 14:57:56 +00001499 struct zd_mac *mac =
1500 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001501 struct zd_chip *chip = &mac->chip;
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001502 int is_associated;
1503 int r;
1504
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +02001505 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1506 goto requeue;
1507
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001508 spin_lock_irq(&mac->lock);
Daniel Drake459c51a2007-11-19 15:00:29 +00001509 is_associated = mac->associated;
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001510 spin_unlock_irq(&mac->lock);
1511
1512 r = zd_chip_control_leds(chip,
Luis R. Rodriguez14b46c82009-08-04 14:04:17 -07001513 is_associated ? ZD_LED_ASSOCIATED : ZD_LED_SCANNING);
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001514 if (r)
Daniel Drake459c51a2007-11-19 15:00:29 +00001515 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001516
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +02001517requeue:
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001518 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1519 LINK_LED_WORK_DELAY);
1520}
1521
1522static void housekeeping_init(struct zd_mac *mac)
1523{
David Howellsc4028952006-11-22 14:57:56 +00001524 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001525}
1526
1527static void housekeeping_enable(struct zd_mac *mac)
1528{
1529 dev_dbg_f(zd_mac_dev(mac), "\n");
1530 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1531 0);
1532}
1533
1534static void housekeeping_disable(struct zd_mac *mac)
1535{
1536 dev_dbg_f(zd_mac_dev(mac), "\n");
Tejun Heoafe2c512010-12-14 16:21:17 +01001537 cancel_delayed_work_sync(&mac->housekeeping.link_led_work);
Luis R. Rodriguez14b46c82009-08-04 14:04:17 -07001538 zd_chip_control_leds(&mac->chip, ZD_LED_OFF);
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001539}