blob: 6d666359a42f7c8252180c1895a9b26f69f12228 [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>
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" },
44 { ZD_REGDOMAIN_JAPAN_ADD, "JP" },
45 { ZD_REGDOMAIN_SPAIN, "ES" },
46 { ZD_REGDOMAIN_FRANCE, "FR" },
47};
48
Daniel Drake459c51a2007-11-19 15:00:29 +000049/* This table contains the hardware specific values for the modulation rates. */
50static const struct ieee80211_rate zd_rates[] = {
Johannes Berg8318d782008-01-24 19:38:38 +010051 { .bitrate = 10,
52 .hw_value = ZD_CCK_RATE_1M, },
53 { .bitrate = 20,
54 .hw_value = ZD_CCK_RATE_2M,
55 .hw_value_short = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
56 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
57 { .bitrate = 55,
58 .hw_value = ZD_CCK_RATE_5_5M,
59 .hw_value_short = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
60 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
61 { .bitrate = 110,
62 .hw_value = ZD_CCK_RATE_11M,
63 .hw_value_short = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
64 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
65 { .bitrate = 60,
66 .hw_value = ZD_OFDM_RATE_6M,
67 .flags = 0 },
68 { .bitrate = 90,
69 .hw_value = ZD_OFDM_RATE_9M,
70 .flags = 0 },
71 { .bitrate = 120,
72 .hw_value = ZD_OFDM_RATE_12M,
73 .flags = 0 },
74 { .bitrate = 180,
75 .hw_value = ZD_OFDM_RATE_18M,
76 .flags = 0 },
77 { .bitrate = 240,
78 .hw_value = ZD_OFDM_RATE_24M,
79 .flags = 0 },
80 { .bitrate = 360,
81 .hw_value = ZD_OFDM_RATE_36M,
82 .flags = 0 },
83 { .bitrate = 480,
84 .hw_value = ZD_OFDM_RATE_48M,
85 .flags = 0 },
86 { .bitrate = 540,
87 .hw_value = ZD_OFDM_RATE_54M,
88 .flags = 0 },
Daniel Drake459c51a2007-11-19 15:00:29 +000089};
90
91static const struct ieee80211_channel zd_channels[] = {
Johannes Berg8318d782008-01-24 19:38:38 +010092 { .center_freq = 2412, .hw_value = 1 },
93 { .center_freq = 2417, .hw_value = 2 },
94 { .center_freq = 2422, .hw_value = 3 },
95 { .center_freq = 2427, .hw_value = 4 },
96 { .center_freq = 2432, .hw_value = 5 },
97 { .center_freq = 2437, .hw_value = 6 },
98 { .center_freq = 2442, .hw_value = 7 },
99 { .center_freq = 2447, .hw_value = 8 },
100 { .center_freq = 2452, .hw_value = 9 },
101 { .center_freq = 2457, .hw_value = 10 },
102 { .center_freq = 2462, .hw_value = 11 },
103 { .center_freq = 2467, .hw_value = 12 },
104 { .center_freq = 2472, .hw_value = 13 },
105 { .center_freq = 2484, .hw_value = 14 },
Daniel Drake459c51a2007-11-19 15:00:29 +0000106};
Daniel Drakee85d0912006-06-02 17:11:32 +0100107
Ulrich Kunitz583afd12006-09-13 02:42:38 +0100108static void housekeeping_init(struct zd_mac *mac);
109static void housekeeping_enable(struct zd_mac *mac);
110static void housekeeping_disable(struct zd_mac *mac);
111
Luis R. Rodrigueze83a1072008-09-09 23:19:49 -0700112static int zd_reg2alpha2(u8 regdomain, char *alpha2)
113{
114 unsigned int i;
115 struct zd_reg_alpha2_map *reg_map;
116 for (i = 0; i < ARRAY_SIZE(reg_alpha2_map); i++) {
117 reg_map = &reg_alpha2_map[i];
118 if (regdomain == reg_map->reg) {
119 alpha2[0] = reg_map->alpha2[0];
120 alpha2[1] = reg_map->alpha2[1];
121 return 0;
122 }
123 }
124 return 1;
125}
126
Daniel Drake459c51a2007-11-19 15:00:29 +0000127int zd_mac_preinit_hw(struct ieee80211_hw *hw)
Daniel Drake74553ae2007-07-01 18:22:32 +0100128{
129 int r;
130 u8 addr[ETH_ALEN];
Daniel Drake459c51a2007-11-19 15:00:29 +0000131 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drake74553ae2007-07-01 18:22:32 +0100132
133 r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
134 if (r)
135 return r;
136
Daniel Drake459c51a2007-11-19 15:00:29 +0000137 SET_IEEE80211_PERM_ADDR(hw, addr);
138
Daniel Drake74553ae2007-07-01 18:22:32 +0100139 return 0;
140}
141
Daniel Drake459c51a2007-11-19 15:00:29 +0000142int zd_mac_init_hw(struct ieee80211_hw *hw)
Daniel Drakee85d0912006-06-02 17:11:32 +0100143{
144 int r;
Daniel Drake459c51a2007-11-19 15:00:29 +0000145 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100146 struct zd_chip *chip = &mac->chip;
Luis R. Rodrigueze83a1072008-09-09 23:19:49 -0700147 char alpha2[2];
Daniel Drakee85d0912006-06-02 17:11:32 +0100148 u8 default_regdomain;
149
150 r = zd_chip_enable_int(chip);
151 if (r)
152 goto out;
Daniel Drake74553ae2007-07-01 18:22:32 +0100153 r = zd_chip_init_hw(chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100154 if (r)
155 goto disable_int;
156
Daniel Drakee85d0912006-06-02 17:11:32 +0100157 ZD_ASSERT(!irqs_disabled());
Daniel Drakee85d0912006-06-02 17:11:32 +0100158
159 r = zd_read_regdomain(chip, &default_regdomain);
160 if (r)
161 goto disable_int;
Daniel Drakee85d0912006-06-02 17:11:32 +0100162 spin_lock_irq(&mac->lock);
163 mac->regdomain = mac->default_regdomain = default_regdomain;
164 spin_unlock_irq(&mac->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +0100165
Daniel Drake40da08b2006-08-01 23:43:32 +0200166 /* We must inform the device that we are doing encryption/decryption in
167 * software at the moment. */
168 r = zd_set_encryption_type(chip, ENC_SNIFFER);
Daniel Drakee85d0912006-06-02 17:11:32 +0100169 if (r)
170 goto disable_int;
171
Luis R. Rodrigueze83a1072008-09-09 23:19:49 -0700172 r = zd_reg2alpha2(mac->regdomain, alpha2);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500173 if (r)
174 goto disable_int;
Daniel Drakee85d0912006-06-02 17:11:32 +0100175
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500176 r = regulatory_hint(hw->wiphy, alpha2);
Daniel Drakee85d0912006-06-02 17:11:32 +0100177disable_int:
178 zd_chip_disable_int(chip);
179out:
180 return r;
181}
182
183void zd_mac_clear(struct zd_mac *mac)
184{
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000185 flush_workqueue(zd_workqueue);
Daniel Drakee85d0912006-06-02 17:11:32 +0100186 zd_chip_clear(&mac->chip);
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100187 ZD_ASSERT(!spin_is_locked(&mac->lock));
188 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
Daniel Drakee85d0912006-06-02 17:11:32 +0100189}
190
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100191static int set_rx_filter(struct zd_mac *mac)
Daniel Drakee85d0912006-06-02 17:11:32 +0100192{
Daniel Drake459c51a2007-11-19 15:00:29 +0000193 unsigned long flags;
194 u32 filter = STA_RX_FILTER;
Daniel Drakee85d0912006-06-02 17:11:32 +0100195
Daniel Drake459c51a2007-11-19 15:00:29 +0000196 spin_lock_irqsave(&mac->lock, flags);
197 if (mac->pass_ctrl)
198 filter |= RX_FILTER_CTRL;
199 spin_unlock_irqrestore(&mac->lock, flags);
200
201 return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100202}
203
204static int set_mc_hash(struct zd_mac *mac)
205{
206 struct zd_mc_hash hash;
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100207 zd_mc_clear(&hash);
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100208 return zd_chip_set_multicast_hash(&mac->chip, &hash);
209}
210
Daniel Drake459c51a2007-11-19 15:00:29 +0000211static int zd_op_start(struct ieee80211_hw *hw)
Daniel Drakee85d0912006-06-02 17:11:32 +0100212{
Daniel Drake459c51a2007-11-19 15:00:29 +0000213 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100214 struct zd_chip *chip = &mac->chip;
Daniel Drake74553ae2007-07-01 18:22:32 +0100215 struct zd_usb *usb = &chip->usb;
Daniel Drakee85d0912006-06-02 17:11:32 +0100216 int r;
217
Daniel Drake74553ae2007-07-01 18:22:32 +0100218 if (!usb->initialized) {
219 r = zd_usb_init_hw(usb);
220 if (r)
221 goto out;
222 }
223
Daniel Drakee85d0912006-06-02 17:11:32 +0100224 r = zd_chip_enable_int(chip);
225 if (r < 0)
226 goto out;
227
228 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
229 if (r < 0)
230 goto disable_int;
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100231 r = set_rx_filter(mac);
232 if (r)
233 goto disable_int;
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100234 r = set_mc_hash(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100235 if (r)
236 goto disable_int;
237 r = zd_chip_switch_radio_on(chip);
238 if (r < 0)
239 goto disable_int;
Daniel Drake459c51a2007-11-19 15:00:29 +0000240 r = zd_chip_enable_rxtx(chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100241 if (r < 0)
242 goto disable_radio;
243 r = zd_chip_enable_hwint(chip);
244 if (r < 0)
Daniel Drake459c51a2007-11-19 15:00:29 +0000245 goto disable_rxtx;
Daniel Drakee85d0912006-06-02 17:11:32 +0100246
Ulrich Kunitz583afd12006-09-13 02:42:38 +0100247 housekeeping_enable(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100248 return 0;
Daniel Drake459c51a2007-11-19 15:00:29 +0000249disable_rxtx:
250 zd_chip_disable_rxtx(chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100251disable_radio:
252 zd_chip_switch_radio_off(chip);
253disable_int:
254 zd_chip_disable_int(chip);
255out:
256 return r;
257}
258
Daniel Drake459c51a2007-11-19 15:00:29 +0000259static void zd_op_stop(struct ieee80211_hw *hw)
260{
261 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100262 struct zd_chip *chip = &mac->chip;
Daniel Drake459c51a2007-11-19 15:00:29 +0000263 struct sk_buff *skb;
264 struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
Daniel Drakee85d0912006-06-02 17:11:32 +0100265
Daniel Drake459c51a2007-11-19 15:00:29 +0000266 /* The order here deliberately is a little different from the open()
Daniel Drakee85d0912006-06-02 17:11:32 +0100267 * method, since we need to make sure there is no opportunity for RX
Daniel Drake459c51a2007-11-19 15:00:29 +0000268 * frames to be processed by mac80211 after we have stopped it.
Daniel Drakee85d0912006-06-02 17:11:32 +0100269 */
270
Daniel Drake459c51a2007-11-19 15:00:29 +0000271 zd_chip_disable_rxtx(chip);
Ulrich Kunitz583afd12006-09-13 02:42:38 +0100272 housekeeping_disable(mac);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000273 flush_workqueue(zd_workqueue);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000274
Daniel Drakee85d0912006-06-02 17:11:32 +0100275 zd_chip_disable_hwint(chip);
276 zd_chip_switch_radio_off(chip);
277 zd_chip_disable_int(chip);
278
Daniel Drake459c51a2007-11-19 15:00:29 +0000279
280 while ((skb = skb_dequeue(ack_wait_queue)))
Johannes Berge039fa42008-05-15 12:55:29 +0200281 dev_kfree_skb_any(skb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100282}
283
Daniel Drake459c51a2007-11-19 15:00:29 +0000284/**
285 * tx_status - reports tx status of a packet if required
286 * @hw - a &struct ieee80211_hw pointer
287 * @skb - a sk-buffer
Johannes Berge039fa42008-05-15 12:55:29 +0200288 * @flags: extra flags to set in the TX status info
289 * @ackssi: ACK signal strength
Coly Li73ac36e2009-01-07 18:09:16 -0800290 * @success - True for successful transmission of the frame
Daniel Drake459c51a2007-11-19 15:00:29 +0000291 *
292 * This information calls ieee80211_tx_status_irqsafe() if required by the
293 * control information. It copies the control information into the status
294 * information.
295 *
296 * If no status information has been requested, the skb is freed.
297 */
298static void tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
Johannes Berge6a98542008-10-21 12:40:02 +0200299 int ackssi, bool success)
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000300{
Johannes Berge039fa42008-05-15 12:55:29 +0200301 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000302
Johannes Berge6a98542008-10-21 12:40:02 +0200303 ieee80211_tx_info_clear_status(info);
Johannes Berge039fa42008-05-15 12:55:29 +0200304
Johannes Berge6a98542008-10-21 12:40:02 +0200305 if (success)
306 info->flags |= IEEE80211_TX_STAT_ACK;
Johannes Berge039fa42008-05-15 12:55:29 +0200307 info->status.ack_signal = ackssi;
308 ieee80211_tx_status_irqsafe(hw, skb);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000309}
310
Daniel Drake459c51a2007-11-19 15:00:29 +0000311/**
312 * zd_mac_tx_failed - callback for failed frames
313 * @dev: the mac80211 wireless device
314 *
315 * This function is called if a frame couldn't be succesfully be
316 * transferred. The first frame from the tx queue, will be selected and
317 * reported as error to the upper layers.
318 */
319void zd_mac_tx_failed(struct ieee80211_hw *hw)
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000320{
Daniel Drake459c51a2007-11-19 15:00:29 +0000321 struct sk_buff_head *q = &zd_hw_mac(hw)->ack_wait_queue;
322 struct sk_buff *skb;
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000323
Daniel Drake459c51a2007-11-19 15:00:29 +0000324 skb = skb_dequeue(q);
325 if (skb == NULL)
326 return;
Johannes Berg5078ed52008-02-20 12:09:25 +0100327
Johannes Berge6a98542008-10-21 12:40:02 +0200328 tx_status(hw, skb, 0, 0);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000329}
330
Daniel Drake459c51a2007-11-19 15:00:29 +0000331/**
332 * zd_mac_tx_to_dev - callback for USB layer
333 * @skb: a &sk_buff pointer
334 * @error: error value, 0 if transmission successful
335 *
336 * Informs the MAC layer that the frame has successfully transferred to the
337 * device. If an ACK is required and the transfer to the device has been
338 * successful, the packets are put on the @ack_wait_queue with
339 * the control set removed.
340 */
341void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
Daniel Drakee85d0912006-06-02 17:11:32 +0100342{
Johannes Berge039fa42008-05-15 12:55:29 +0200343 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200344 struct ieee80211_hw *hw = info->rate_driver_data[0];
Daniel Drakee85d0912006-06-02 17:11:32 +0100345
Johannes Berge039fa42008-05-15 12:55:29 +0200346 skb_pull(skb, sizeof(struct zd_ctrlset));
347 if (unlikely(error ||
348 (info->flags & IEEE80211_TX_CTL_NO_ACK))) {
Johannes Berge6a98542008-10-21 12:40:02 +0200349 tx_status(hw, skb, 0, !error);
Daniel Drake459c51a2007-11-19 15:00:29 +0000350 } else {
Johannes Berge039fa42008-05-15 12:55:29 +0200351 struct sk_buff_head *q =
352 &zd_hw_mac(hw)->ack_wait_queue;
353
354 skb_queue_tail(q, skb);
355 while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS)
356 zd_mac_tx_failed(hw);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000357 }
Daniel Drakee85d0912006-06-02 17:11:32 +0100358}
359
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000360static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
Daniel Drakee85d0912006-06-02 17:11:32 +0100361{
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100362 /* ZD_PURE_RATE() must be used to remove the modulation type flag of
Daniel Drake459c51a2007-11-19 15:00:29 +0000363 * the zd-rate values.
364 */
Daniel Drakee85d0912006-06-02 17:11:32 +0100365 static const u8 rate_divisor[] = {
Daniel Drake459c51a2007-11-19 15:00:29 +0000366 [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
367 [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
368 /* Bits must be doubled. */
369 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
370 [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
371 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
372 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
373 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
374 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
375 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
376 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
377 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
378 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
Daniel Drakee85d0912006-06-02 17:11:32 +0100379 };
380
381 u32 bits = (u32)tx_length * 8;
382 u32 divisor;
383
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100384 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
Daniel Drakee85d0912006-06-02 17:11:32 +0100385 if (divisor == 0)
386 return -EINVAL;
387
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000388 switch (zd_rate) {
389 case ZD_CCK_RATE_5_5M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100390 bits = (2*bits) + 10; /* round up to the next integer */
391 break;
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000392 case ZD_CCK_RATE_11M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100393 if (service) {
394 u32 t = bits % 11;
395 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
396 if (0 < t && t <= 3) {
397 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
398 }
399 }
400 bits += 10; /* round up to the next integer */
401 break;
402 }
403
404 return bits/divisor;
405}
406
Daniel Drakee85d0912006-06-02 17:11:32 +0100407static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
Johannes Berge6a98542008-10-21 12:40:02 +0200408 struct ieee80211_hdr *header,
409 struct ieee80211_tx_info *info)
Daniel Drakee85d0912006-06-02 17:11:32 +0100410{
Daniel Drakee85d0912006-06-02 17:11:32 +0100411 /*
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000412 * CONTROL TODO:
Daniel Drakee85d0912006-06-02 17:11:32 +0100413 * - if backoff needed, enable bit 0
414 * - if burst (backoff not needed) disable bit 0
Daniel Drakee85d0912006-06-02 17:11:32 +0100415 */
416
417 cs->control = 0;
418
419 /* First fragment */
Johannes Berge6a98542008-10-21 12:40:02 +0200420 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
Daniel Drakee85d0912006-06-02 17:11:32 +0100421 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
422
Gábor Stefanik13bdcd92009-05-14 17:34:59 +0000423 /* No ACK expected (multicast, etc.) */
424 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
425 cs->control |= ZD_CS_NO_ACK;
Daniel Drakee85d0912006-06-02 17:11:32 +0100426
427 /* PS-POLL */
Harvey Harrison85365822008-06-14 23:33:39 -0700428 if (ieee80211_is_pspoll(header->frame_control))
Daniel Drakee85d0912006-06-02 17:11:32 +0100429 cs->control |= ZD_CS_PS_POLL_FRAME;
430
Johannes Berge6a98542008-10-21 12:40:02 +0200431 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000432 cs->control |= ZD_CS_RTS;
433
Johannes Berge6a98542008-10-21 12:40:02 +0200434 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000435 cs->control |= ZD_CS_SELF_CTS;
Daniel Drakee85d0912006-06-02 17:11:32 +0100436
437 /* FIXME: Management frame? */
438}
439
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100440static int zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon)
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800441{
442 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100443 int r;
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800444 u32 tmp, j = 0;
445 /* 4 more bytes for tail CRC */
446 u32 full_len = beacon->len + 4;
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100447
448 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 0);
449 if (r < 0)
450 return r;
451 r = zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
452 if (r < 0)
453 return r;
454
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800455 while (tmp & 0x2) {
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100456 r = zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
457 if (r < 0)
458 return r;
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800459 if ((++j % 100) == 0) {
460 printk(KERN_ERR "CR_BCN_FIFO_SEMAPHORE not ready\n");
461 if (j >= 500) {
462 printk(KERN_ERR "Giving up beacon config.\n");
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100463 return -ETIMEDOUT;
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800464 }
465 }
466 msleep(1);
467 }
468
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100469 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO, full_len - 1);
470 if (r < 0)
471 return r;
472 if (zd_chip_is_zd1211b(&mac->chip)) {
473 r = zd_iowrite32(&mac->chip, CR_BCN_LENGTH, full_len - 1);
474 if (r < 0)
475 return r;
476 }
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800477
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100478 for (j = 0 ; j < beacon->len; j++) {
479 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO,
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800480 *((u8 *)(beacon->data + j)));
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100481 if (r < 0)
482 return r;
483 }
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800484
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100485 for (j = 0; j < 4; j++) {
486 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO, 0x0);
487 if (r < 0)
488 return r;
489 }
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800490
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100491 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 1);
492 if (r < 0)
493 return r;
494
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800495 /* 802.11b/g 2.4G CCK 1Mb
496 * 802.11a, not yet implemented, uses different values (see GPL vendor
497 * driver)
498 */
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100499 return zd_iowrite32(&mac->chip, CR_BCN_PLCP_CFG, 0x00000400 |
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800500 (full_len << 19));
501}
502
Daniel Drakee85d0912006-06-02 17:11:32 +0100503static int fill_ctrlset(struct zd_mac *mac,
Johannes Berge039fa42008-05-15 12:55:29 +0200504 struct sk_buff *skb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100505{
506 int r;
Daniel Drake459c51a2007-11-19 15:00:29 +0000507 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
508 unsigned int frag_len = skb->len + FCS_LEN;
Daniel Drakee85d0912006-06-02 17:11:32 +0100509 unsigned int packet_length;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200510 struct ieee80211_rate *txrate;
Daniel Drakee85d0912006-06-02 17:11:32 +0100511 struct zd_ctrlset *cs = (struct zd_ctrlset *)
512 skb_push(skb, sizeof(struct zd_ctrlset));
Johannes Berge039fa42008-05-15 12:55:29 +0200513 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100514
Daniel Drakee85d0912006-06-02 17:11:32 +0100515 ZD_ASSERT(frag_len <= 0xffff);
Daniel Drakee85d0912006-06-02 17:11:32 +0100516
Johannes Berge039fa42008-05-15 12:55:29 +0200517 txrate = ieee80211_get_tx_rate(mac->hw, info);
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200518
519 cs->modulation = txrate->hw_value;
Johannes Berge6a98542008-10-21 12:40:02 +0200520 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200521 cs->modulation = txrate->hw_value_short;
Daniel Drakee85d0912006-06-02 17:11:32 +0100522
523 cs->tx_length = cpu_to_le16(frag_len);
524
Johannes Berge6a98542008-10-21 12:40:02 +0200525 cs_set_control(mac, cs, hdr, info);
Daniel Drakee85d0912006-06-02 17:11:32 +0100526
527 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
528 ZD_ASSERT(packet_length <= 0xffff);
529 /* ZD1211B: Computing the length difference this way, gives us
530 * flexibility to compute the packet length.
531 */
Daniel Drake74553ae2007-07-01 18:22:32 +0100532 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
Daniel Drakee85d0912006-06-02 17:11:32 +0100533 packet_length - frag_len : packet_length);
534
535 /*
536 * CURRENT LENGTH:
537 * - transmit frame length in microseconds
538 * - seems to be derived from frame length
539 * - see Cal_Us_Service() in zdinlinef.h
540 * - if macp->bTxBurstEnable is enabled, then multiply by 4
541 * - bTxBurstEnable is never set in the vendor driver
542 *
543 * SERVICE:
544 * - "for PLCP configuration"
545 * - always 0 except in some situations at 802.11b 11M
546 * - see line 53 of zdinlinef.h
547 */
548 cs->service = 0;
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100549 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
Daniel Drakee85d0912006-06-02 17:11:32 +0100550 le16_to_cpu(cs->tx_length));
551 if (r < 0)
552 return r;
553 cs->current_length = cpu_to_le16(r);
Daniel Drake459c51a2007-11-19 15:00:29 +0000554 cs->next_frame_length = 0;
Daniel Drakee85d0912006-06-02 17:11:32 +0100555
556 return 0;
557}
558
Daniel Drake459c51a2007-11-19 15:00:29 +0000559/**
560 * zd_op_tx - transmits a network frame to the device
Ulrich Kunitz741fec52006-11-22 00:05:53 +0000561 *
Daniel Drake459c51a2007-11-19 15:00:29 +0000562 * @dev: mac80211 hardware device
563 * @skb: socket buffer
564 * @control: the control structure
Daniel Drakee85d0912006-06-02 17:11:32 +0100565 *
Daniel Drake459c51a2007-11-19 15:00:29 +0000566 * This function transmit an IEEE 802.11 network frame to the device. The
567 * control block of the skbuff will be initialized. If necessary the incoming
568 * mac80211 queues will be stopped.
Daniel Drakee85d0912006-06-02 17:11:32 +0100569 */
Johannes Berge039fa42008-05-15 12:55:29 +0200570static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100571{
Daniel Drake459c51a2007-11-19 15:00:29 +0000572 struct zd_mac *mac = zd_hw_mac(hw);
Johannes Berge039fa42008-05-15 12:55:29 +0200573 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Daniel Drake459c51a2007-11-19 15:00:29 +0000574 int r;
Daniel Drakee85d0912006-06-02 17:11:32 +0100575
Johannes Berge039fa42008-05-15 12:55:29 +0200576 r = fill_ctrlset(mac, skb);
Daniel Drake459c51a2007-11-19 15:00:29 +0000577 if (r)
Jouni Malinen640c65e2009-03-16 21:47:33 +0200578 goto fail;
Daniel Drakee85d0912006-06-02 17:11:32 +0100579
Johannes Berge6a98542008-10-21 12:40:02 +0200580 info->rate_driver_data[0] = hw;
Johannes Berge039fa42008-05-15 12:55:29 +0200581
582 r = zd_usb_tx(&mac->chip.usb, skb);
Daniel Drake459c51a2007-11-19 15:00:29 +0000583 if (r)
Jouni Malinen640c65e2009-03-16 21:47:33 +0200584 goto fail;
585 return 0;
586
587fail:
588 dev_kfree_skb(skb);
Daniel Drake459c51a2007-11-19 15:00:29 +0000589 return 0;
590}
591
592/**
593 * filter_ack - filters incoming packets for acknowledgements
594 * @dev: the mac80211 device
595 * @rx_hdr: received header
596 * @stats: the status for the received packet
597 *
598 * This functions looks for ACK packets and tries to match them with the
599 * frames in the tx queue. If a match is found the frame will be dequeued and
600 * the upper layers is informed about the successful transmission. If
601 * mac80211 queues have been stopped and the number of frames still to be
602 * transmitted is low the queues will be opened again.
603 *
604 * Returns 1 if the frame was an ACK, 0 if it was ignored.
605 */
606static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
607 struct ieee80211_rx_status *stats)
608{
Daniel Drake459c51a2007-11-19 15:00:29 +0000609 struct sk_buff *skb;
610 struct sk_buff_head *q;
611 unsigned long flags;
612
Harvey Harrison85365822008-06-14 23:33:39 -0700613 if (!ieee80211_is_ack(rx_hdr->frame_control))
Daniel Drake459c51a2007-11-19 15:00:29 +0000614 return 0;
615
616 q = &zd_hw_mac(hw)->ack_wait_queue;
617 spin_lock_irqsave(&q->lock, flags);
David S. Miller47a227d2008-09-23 00:23:30 -0700618 skb_queue_walk(q, skb) {
Daniel Drake459c51a2007-11-19 15:00:29 +0000619 struct ieee80211_hdr *tx_hdr;
620
621 tx_hdr = (struct ieee80211_hdr *)skb->data;
Shaddy Baddahcde69012008-11-28 17:10:45 +1100622 if (likely(!memcmp(tx_hdr->addr2, rx_hdr->addr1, ETH_ALEN)))
Daniel Drake459c51a2007-11-19 15:00:29 +0000623 {
Daniel Drake459c51a2007-11-19 15:00:29 +0000624 __skb_unlink(skb, q);
Johannes Berge6a98542008-10-21 12:40:02 +0200625 tx_status(hw, skb, stats->signal, 1);
Daniel Drake459c51a2007-11-19 15:00:29 +0000626 goto out;
627 }
628 }
629out:
630 spin_unlock_irqrestore(&q->lock, flags);
631 return 1;
632}
633
634int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
635{
636 struct zd_mac *mac = zd_hw_mac(hw);
637 struct ieee80211_rx_status stats;
638 const struct rx_status *status;
639 struct sk_buff *skb;
640 int bad_frame = 0;
Harvey Harrison85365822008-06-14 23:33:39 -0700641 __le16 fc;
642 int need_padding;
Johannes Berg8318d782008-01-24 19:38:38 +0100643 int i;
644 u8 rate;
Daniel Drake459c51a2007-11-19 15:00:29 +0000645
646 if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
647 FCS_LEN + sizeof(struct rx_status))
Daniel Drakee85d0912006-06-02 17:11:32 +0100648 return -EINVAL;
649
Daniel Drake459c51a2007-11-19 15:00:29 +0000650 memset(&stats, 0, sizeof(stats));
Daniel Drakee85d0912006-06-02 17:11:32 +0100651
Daniel Drake459c51a2007-11-19 15:00:29 +0000652 /* Note about pass_failed_fcs and pass_ctrl access below:
653 * mac locking intentionally omitted here, as this is the only unlocked
654 * reader and the only writer is configure_filter. Plus, if there were
655 * any races accessing these variables, it wouldn't really matter.
656 * If mac80211 ever provides a way for us to access filter flags
657 * from outside configure_filter, we could improve on this. Also, this
658 * situation may change once we implement some kind of DMA-into-skb
659 * RX path. */
Daniel Drakee85d0912006-06-02 17:11:32 +0100660
Daniel Drake459c51a2007-11-19 15:00:29 +0000661 /* Caller has to ensure that length >= sizeof(struct rx_status). */
662 status = (struct rx_status *)
Ulrich Kunitz937a0492007-10-02 18:36:53 +0100663 (buffer + (length - sizeof(struct rx_status)));
Daniel Drakee85d0912006-06-02 17:11:32 +0100664 if (status->frame_status & ZD_RX_ERROR) {
Daniel Drake459c51a2007-11-19 15:00:29 +0000665 if (mac->pass_failed_fcs &&
666 (status->frame_status & ZD_RX_CRC32_ERROR)) {
667 stats.flag |= RX_FLAG_FAILED_FCS_CRC;
668 bad_frame = 1;
669 } else {
670 return -EINVAL;
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000671 }
Daniel Drakee85d0912006-06-02 17:11:32 +0100672 }
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000673
Johannes Berg8318d782008-01-24 19:38:38 +0100674 stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
675 stats.band = IEEE80211_BAND_2GHZ;
Bruno Randolf566bfe52008-05-08 19:15:40 +0200676 stats.signal = status->signal_strength;
677 stats.qual = zd_rx_qual_percent(buffer,
Daniel Drakee85d0912006-06-02 17:11:32 +0100678 length - sizeof(struct rx_status),
679 status);
Johannes Berg8318d782008-01-24 19:38:38 +0100680
681 rate = zd_rx_rate(buffer, status);
682
683 /* todo: return index in the big switches in zd_rx_rate instead */
684 for (i = 0; i < mac->band.n_bitrates; i++)
685 if (rate == mac->band.bitrates[i].hw_value)
686 stats.rate_idx = i;
Daniel Drakee85d0912006-06-02 17:11:32 +0100687
Daniel Drake459c51a2007-11-19 15:00:29 +0000688 length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
689 buffer += ZD_PLCP_HEADER_SIZE;
Daniel Drakee85d0912006-06-02 17:11:32 +0100690
Daniel Drake459c51a2007-11-19 15:00:29 +0000691 /* Except for bad frames, filter each frame to see if it is an ACK, in
692 * which case our internal TX tracking is updated. Normally we then
693 * bail here as there's no need to pass ACKs on up to the stack, but
694 * there is also the case where the stack has requested us to pass
695 * control frames on up (pass_ctrl) which we must consider. */
696 if (!bad_frame &&
697 filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
698 && !mac->pass_ctrl)
699 return 0;
Daniel Drakee85d0912006-06-02 17:11:32 +0100700
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400701 fc = get_unaligned((__le16*)buffer);
Harvey Harrison85365822008-06-14 23:33:39 -0700702 need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
Michael Buesch90817282007-12-29 17:24:23 +0100703
704 skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
Daniel Drake459c51a2007-11-19 15:00:29 +0000705 if (skb == NULL)
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800706 return -ENOMEM;
Michael Buesch90817282007-12-29 17:24:23 +0100707 if (need_padding) {
708 /* Make sure the the payload data is 4 byte aligned. */
709 skb_reserve(skb, 2);
710 }
711
Daniel Drake459c51a2007-11-19 15:00:29 +0000712 memcpy(skb_put(skb, length), buffer, length);
713
Johannes Bergf1d58c22009-06-17 13:13:00 +0200714 memcpy(IEEE80211_SKB_RXCB(skb), &stats, sizeof(stats));
715 ieee80211_rx_irqsafe(hw, skb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100716 return 0;
717}
718
Daniel Drake459c51a2007-11-19 15:00:29 +0000719static int zd_op_add_interface(struct ieee80211_hw *hw,
720 struct ieee80211_if_init_conf *conf)
Daniel Drakee85d0912006-06-02 17:11:32 +0100721{
Daniel Drake459c51a2007-11-19 15:00:29 +0000722 struct zd_mac *mac = zd_hw_mac(hw);
723
Johannes Berg05c914f2008-09-11 00:01:58 +0200724 /* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */
725 if (mac->type != NL80211_IFTYPE_UNSPECIFIED)
Daniel Drake459c51a2007-11-19 15:00:29 +0000726 return -EOPNOTSUPP;
727
728 switch (conf->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200729 case NL80211_IFTYPE_MONITOR:
730 case NL80211_IFTYPE_MESH_POINT:
731 case NL80211_IFTYPE_STATION:
732 case NL80211_IFTYPE_ADHOC:
Daniel Drake459c51a2007-11-19 15:00:29 +0000733 mac->type = conf->type;
734 break;
735 default:
736 return -EOPNOTSUPP;
737 }
738
739 return zd_write_mac_addr(&mac->chip, conf->mac_addr);
Daniel Drakee85d0912006-06-02 17:11:32 +0100740}
741
Daniel Drake459c51a2007-11-19 15:00:29 +0000742static void zd_op_remove_interface(struct ieee80211_hw *hw,
743 struct ieee80211_if_init_conf *conf)
Daniel Drakee85d0912006-06-02 17:11:32 +0100744{
Daniel Drake459c51a2007-11-19 15:00:29 +0000745 struct zd_mac *mac = zd_hw_mac(hw);
Johannes Berg05c914f2008-09-11 00:01:58 +0200746 mac->type = NL80211_IFTYPE_UNSPECIFIED;
Luis Carlos Cobo86229f02008-07-08 16:19:21 +0200747 zd_set_beacon_interval(&mac->chip, 0);
Daniel Drake459c51a2007-11-19 15:00:29 +0000748 zd_write_mac_addr(&mac->chip, NULL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100749}
750
Johannes Berge8975582008-10-09 12:18:51 +0200751static int zd_op_config(struct ieee80211_hw *hw, u32 changed)
Daniel Drakee85d0912006-06-02 17:11:32 +0100752{
Daniel Drake459c51a2007-11-19 15:00:29 +0000753 struct zd_mac *mac = zd_hw_mac(hw);
Johannes Berge8975582008-10-09 12:18:51 +0200754 struct ieee80211_conf *conf = &hw->conf;
755
Johannes Berg8318d782008-01-24 19:38:38 +0100756 return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
Daniel Drakee85d0912006-06-02 17:11:32 +0100757}
758
Luis R. Rodrigueze83a1072008-09-09 23:19:49 -0700759static void zd_process_intr(struct work_struct *work)
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800760{
761 u16 int_status;
762 struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
763
Al Virod63ddce2008-05-21 01:34:30 +0100764 int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer+4));
Johannes Berg570a0a72009-02-10 21:25:37 +0100765 if (int_status & INT_CFG_NEXT_BCN)
766 dev_dbg_f_limit(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");
767 else
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800768 dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
769
770 zd_chip_enable_hwint(&mac->chip);
771}
772
773
Daniel Drake459c51a2007-11-19 15:00:29 +0000774static void set_multicast_hash_handler(struct work_struct *work)
775{
776 struct zd_mac *mac =
777 container_of(work, struct zd_mac, set_multicast_hash_work);
778 struct zd_mc_hash hash;
779
780 spin_lock_irq(&mac->lock);
781 hash = mac->multicast_hash;
782 spin_unlock_irq(&mac->lock);
783
784 zd_chip_set_multicast_hash(&mac->chip, &hash);
785}
786
787static void set_rx_filter_handler(struct work_struct *work)
788{
789 struct zd_mac *mac =
790 container_of(work, struct zd_mac, set_rx_filter_work);
791 int r;
792
793 dev_dbg_f(zd_mac_dev(mac), "\n");
794 r = set_rx_filter(mac);
795 if (r)
796 dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r);
797}
798
Johannes Berg3ac64be2009-08-17 16:16:53 +0200799static u64 zd_op_prepare_multicast(struct ieee80211_hw *hw,
800 int mc_count, struct dev_addr_list *mclist)
801{
802 struct zd_mac *mac = zd_hw_mac(hw);
803 struct zd_mc_hash hash;
804 int i;
805
806 zd_mc_clear(&hash);
807
808 for (i = 0; i < mc_count; i++) {
809 if (!mclist)
810 break;
811 dev_dbg_f(zd_mac_dev(mac), "mc addr %pM\n", mclist->dmi_addr);
812 zd_mc_add_addr(&hash, mclist->dmi_addr);
813 mclist = mclist->next;
814 }
815
816 return hash.low | ((u64)hash.high << 32);
817}
818
Daniel Drake459c51a2007-11-19 15:00:29 +0000819#define SUPPORTED_FIF_FLAGS \
820 (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
Michael Buesch2c1a1b12008-02-10 16:03:55 +0100821 FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
Daniel Drake459c51a2007-11-19 15:00:29 +0000822static void zd_op_configure_filter(struct ieee80211_hw *hw,
823 unsigned int changed_flags,
824 unsigned int *new_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +0200825 u64 multicast)
Daniel Drake459c51a2007-11-19 15:00:29 +0000826{
Johannes Berg3ac64be2009-08-17 16:16:53 +0200827 struct zd_mc_hash hash = {
828 .low = multicast,
829 .high = multicast >> 32,
830 };
Daniel Drake459c51a2007-11-19 15:00:29 +0000831 struct zd_mac *mac = zd_hw_mac(hw);
832 unsigned long flags;
Daniel Drake459c51a2007-11-19 15:00:29 +0000833
834 /* Only deal with supported flags */
835 changed_flags &= SUPPORTED_FIF_FLAGS;
836 *new_flags &= SUPPORTED_FIF_FLAGS;
837
838 /* changed_flags is always populated but this driver
839 * doesn't support all FIF flags so its possible we don't
840 * need to do anything */
841 if (!changed_flags)
842 return;
843
Johannes Berg3ac64be2009-08-17 16:16:53 +0200844 if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI))
Daniel Drake459c51a2007-11-19 15:00:29 +0000845 zd_mc_add_all(&hash);
Daniel Drake459c51a2007-11-19 15:00:29 +0000846
847 spin_lock_irqsave(&mac->lock, flags);
848 mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
849 mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
850 mac->multicast_hash = hash;
851 spin_unlock_irqrestore(&mac->lock, flags);
Johannes Berg3ac64be2009-08-17 16:16:53 +0200852
853 /* XXX: these can be called here now, can sleep now! */
Daniel Drake459c51a2007-11-19 15:00:29 +0000854 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
855
856 if (changed_flags & FIF_CONTROL)
857 queue_work(zd_workqueue, &mac->set_rx_filter_work);
858
859 /* no handling required for FIF_OTHER_BSS as we don't currently
860 * do BSSID filtering */
861 /* FIXME: in future it would be nice to enable the probe response
862 * filter (so that the driver doesn't see them) until
863 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
864 * have to schedule work to enable prbresp reception, which might
865 * happen too late. For now we'll just listen and forward them all the
866 * time. */
867}
868
869static void set_rts_cts_work(struct work_struct *work)
870{
871 struct zd_mac *mac =
872 container_of(work, struct zd_mac, set_rts_cts_work);
873 unsigned long flags;
874 unsigned int short_preamble;
875
876 mutex_lock(&mac->chip.mutex);
877
878 spin_lock_irqsave(&mac->lock, flags);
879 mac->updating_rts_rate = 0;
880 short_preamble = mac->short_preamble;
881 spin_unlock_irqrestore(&mac->lock, flags);
882
883 zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
884 mutex_unlock(&mac->chip.mutex);
885}
886
Johannes Berg471b3ef2007-12-28 14:32:58 +0100887static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
888 struct ieee80211_vif *vif,
889 struct ieee80211_bss_conf *bss_conf,
890 u32 changes)
Daniel Drake459c51a2007-11-19 15:00:29 +0000891{
892 struct zd_mac *mac = zd_hw_mac(hw);
893 unsigned long flags;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200894 int associated;
Daniel Drake459c51a2007-11-19 15:00:29 +0000895
896 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
897
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200898 if (mac->type == NL80211_IFTYPE_MESH_POINT ||
899 mac->type == NL80211_IFTYPE_ADHOC) {
900 associated = true;
901 if (changes & BSS_CHANGED_BEACON) {
902 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
903
904 if (beacon) {
905 zd_mac_config_beacon(hw, beacon);
906 kfree_skb(beacon);
907 }
908 }
909
910 if (changes & BSS_CHANGED_BEACON_ENABLED) {
911 u32 interval;
912
913 if (bss_conf->enable_beacon)
914 interval = BCN_MODE_IBSS |
915 bss_conf->beacon_int;
916 else
917 interval = 0;
918
919 zd_set_beacon_interval(&mac->chip, interval);
920 }
921 } else
922 associated = is_valid_ether_addr(bss_conf->bssid);
923
924 spin_lock_irq(&mac->lock);
925 mac->associated = associated;
926 spin_unlock_irq(&mac->lock);
927
928 /* TODO: do hardware bssid filtering */
929
Johannes Berg471b3ef2007-12-28 14:32:58 +0100930 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Daniel Drake459c51a2007-11-19 15:00:29 +0000931 spin_lock_irqsave(&mac->lock, flags);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100932 mac->short_preamble = bss_conf->use_short_preamble;
Daniel Drake459c51a2007-11-19 15:00:29 +0000933 if (!mac->updating_rts_rate) {
934 mac->updating_rts_rate = 1;
935 /* FIXME: should disable TX here, until work has
936 * completed and RTS_CTS reg is updated */
937 queue_work(zd_workqueue, &mac->set_rts_cts_work);
938 }
939 spin_unlock_irqrestore(&mac->lock, flags);
940 }
941}
942
Alina Friedrichsen5fe73192009-02-25 00:49:18 +0100943static u64 zd_op_get_tsf(struct ieee80211_hw *hw)
944{
945 struct zd_mac *mac = zd_hw_mac(hw);
946 return zd_chip_get_tsf(&mac->chip);
947}
948
Daniel Drake459c51a2007-11-19 15:00:29 +0000949static const struct ieee80211_ops zd_ops = {
950 .tx = zd_op_tx,
951 .start = zd_op_start,
952 .stop = zd_op_stop,
953 .add_interface = zd_op_add_interface,
954 .remove_interface = zd_op_remove_interface,
955 .config = zd_op_config,
Johannes Berg3ac64be2009-08-17 16:16:53 +0200956 .prepare_multicast = zd_op_prepare_multicast,
Daniel Drake459c51a2007-11-19 15:00:29 +0000957 .configure_filter = zd_op_configure_filter,
Johannes Berg471b3ef2007-12-28 14:32:58 +0100958 .bss_info_changed = zd_op_bss_info_changed,
Alina Friedrichsen5fe73192009-02-25 00:49:18 +0100959 .get_tsf = zd_op_get_tsf,
Daniel Drake459c51a2007-11-19 15:00:29 +0000960};
961
962struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
963{
964 struct zd_mac *mac;
965 struct ieee80211_hw *hw;
Daniel Drake459c51a2007-11-19 15:00:29 +0000966
967 hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
968 if (!hw) {
969 dev_dbg_f(&intf->dev, "out of memory\n");
970 return NULL;
971 }
972
973 mac = zd_hw_mac(hw);
974
975 memset(mac, 0, sizeof(*mac));
976 spin_lock_init(&mac->lock);
977 mac->hw = hw;
978
Johannes Berg05c914f2008-09-11 00:01:58 +0200979 mac->type = NL80211_IFTYPE_UNSPECIFIED;
Daniel Drake459c51a2007-11-19 15:00:29 +0000980
981 memcpy(mac->channels, zd_channels, sizeof(zd_channels));
982 memcpy(mac->rates, zd_rates, sizeof(zd_rates));
Johannes Berg8318d782008-01-24 19:38:38 +0100983 mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
984 mac->band.bitrates = mac->rates;
985 mac->band.n_channels = ARRAY_SIZE(zd_channels);
986 mac->band.channels = mac->channels;
Daniel Drake459c51a2007-11-19 15:00:29 +0000987
Johannes Berg8318d782008-01-24 19:38:38 +0100988 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
989
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800990 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
Johannes Berg7fee5372009-01-30 11:13:06 +0100991 IEEE80211_HW_SIGNAL_UNSPEC;
Daniel Drake459c51a2007-11-19 15:00:29 +0000992
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700993 hw->wiphy->interface_modes =
994 BIT(NL80211_IFTYPE_MESH_POINT) |
995 BIT(NL80211_IFTYPE_STATION) |
996 BIT(NL80211_IFTYPE_ADHOC);
997
Bruno Randolf566bfe52008-05-08 19:15:40 +0200998 hw->max_signal = 100;
Daniel Drake459c51a2007-11-19 15:00:29 +0000999 hw->queues = 1;
1000 hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
1001
1002 skb_queue_head_init(&mac->ack_wait_queue);
1003
Daniel Drake459c51a2007-11-19 15:00:29 +00001004 zd_chip_init(&mac->chip, hw, intf);
1005 housekeeping_init(mac);
1006 INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
1007 INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
1008 INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler);
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -08001009 INIT_WORK(&mac->process_intr, zd_process_intr);
Daniel Drake459c51a2007-11-19 15:00:29 +00001010
1011 SET_IEEE80211_DEV(hw, &intf->dev);
1012 return hw;
Daniel Drakee85d0912006-06-02 17:11:32 +01001013}
1014
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001015#define LINK_LED_WORK_DELAY HZ
1016
David Howellsc4028952006-11-22 14:57:56 +00001017static void link_led_handler(struct work_struct *work)
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001018{
David Howellsc4028952006-11-22 14:57:56 +00001019 struct zd_mac *mac =
1020 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001021 struct zd_chip *chip = &mac->chip;
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001022 int is_associated;
1023 int r;
1024
1025 spin_lock_irq(&mac->lock);
Daniel Drake459c51a2007-11-19 15:00:29 +00001026 is_associated = mac->associated;
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001027 spin_unlock_irq(&mac->lock);
1028
1029 r = zd_chip_control_leds(chip,
Luis R. Rodriguez14b46c82009-08-04 14:04:17 -07001030 is_associated ? ZD_LED_ASSOCIATED : ZD_LED_SCANNING);
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001031 if (r)
Daniel Drake459c51a2007-11-19 15:00:29 +00001032 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001033
1034 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1035 LINK_LED_WORK_DELAY);
1036}
1037
1038static void housekeeping_init(struct zd_mac *mac)
1039{
David Howellsc4028952006-11-22 14:57:56 +00001040 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001041}
1042
1043static void housekeeping_enable(struct zd_mac *mac)
1044{
1045 dev_dbg_f(zd_mac_dev(mac), "\n");
1046 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1047 0);
1048}
1049
1050static void housekeeping_disable(struct zd_mac *mac)
1051{
1052 dev_dbg_f(zd_mac_dev(mac), "\n");
1053 cancel_rearming_delayed_workqueue(zd_workqueue,
1054 &mac->housekeeping.link_led_work);
Luis R. Rodriguez14b46c82009-08-04 14:04:17 -07001055 zd_chip_control_leds(&mac->chip, ZD_LED_OFF);
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001056}