blob: 665f76af2fec134fcb53bad523dfee7593362088 [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>
Daniel Drake459c51a2007-11-19 15:00:29 +00006 * Copyright (c) 2007 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
7 *
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"
32#include "zd_ieee80211.h"
Daniel Drakee85d0912006-06-02 17:11:32 +010033#include "zd_rf.h"
Daniel Drakee85d0912006-06-02 17:11:32 +010034
Daniel Drake459c51a2007-11-19 15:00:29 +000035/* This table contains the hardware specific values for the modulation rates. */
36static const struct ieee80211_rate zd_rates[] = {
Johannes Berg8318d782008-01-24 19:38:38 +010037 { .bitrate = 10,
38 .hw_value = ZD_CCK_RATE_1M, },
39 { .bitrate = 20,
40 .hw_value = ZD_CCK_RATE_2M,
41 .hw_value_short = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
42 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
43 { .bitrate = 55,
44 .hw_value = ZD_CCK_RATE_5_5M,
45 .hw_value_short = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
46 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
47 { .bitrate = 110,
48 .hw_value = ZD_CCK_RATE_11M,
49 .hw_value_short = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
50 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
51 { .bitrate = 60,
52 .hw_value = ZD_OFDM_RATE_6M,
53 .flags = 0 },
54 { .bitrate = 90,
55 .hw_value = ZD_OFDM_RATE_9M,
56 .flags = 0 },
57 { .bitrate = 120,
58 .hw_value = ZD_OFDM_RATE_12M,
59 .flags = 0 },
60 { .bitrate = 180,
61 .hw_value = ZD_OFDM_RATE_18M,
62 .flags = 0 },
63 { .bitrate = 240,
64 .hw_value = ZD_OFDM_RATE_24M,
65 .flags = 0 },
66 { .bitrate = 360,
67 .hw_value = ZD_OFDM_RATE_36M,
68 .flags = 0 },
69 { .bitrate = 480,
70 .hw_value = ZD_OFDM_RATE_48M,
71 .flags = 0 },
72 { .bitrate = 540,
73 .hw_value = ZD_OFDM_RATE_54M,
74 .flags = 0 },
Daniel Drake459c51a2007-11-19 15:00:29 +000075};
76
77static const struct ieee80211_channel zd_channels[] = {
Johannes Berg8318d782008-01-24 19:38:38 +010078 { .center_freq = 2412, .hw_value = 1 },
79 { .center_freq = 2417, .hw_value = 2 },
80 { .center_freq = 2422, .hw_value = 3 },
81 { .center_freq = 2427, .hw_value = 4 },
82 { .center_freq = 2432, .hw_value = 5 },
83 { .center_freq = 2437, .hw_value = 6 },
84 { .center_freq = 2442, .hw_value = 7 },
85 { .center_freq = 2447, .hw_value = 8 },
86 { .center_freq = 2452, .hw_value = 9 },
87 { .center_freq = 2457, .hw_value = 10 },
88 { .center_freq = 2462, .hw_value = 11 },
89 { .center_freq = 2467, .hw_value = 12 },
90 { .center_freq = 2472, .hw_value = 13 },
91 { .center_freq = 2484, .hw_value = 14 },
Daniel Drake459c51a2007-11-19 15:00:29 +000092};
Daniel Drakee85d0912006-06-02 17:11:32 +010093
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +010094static void housekeeping_init(struct zd_mac *mac);
95static void housekeeping_enable(struct zd_mac *mac);
96static void housekeeping_disable(struct zd_mac *mac);
97
Daniel Drake459c51a2007-11-19 15:00:29 +000098int zd_mac_preinit_hw(struct ieee80211_hw *hw)
Daniel Drake74553ae2007-07-01 18:22:32 +010099{
100 int r;
101 u8 addr[ETH_ALEN];
Daniel Drake459c51a2007-11-19 15:00:29 +0000102 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drake74553ae2007-07-01 18:22:32 +0100103
104 r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
105 if (r)
106 return r;
107
Daniel Drake459c51a2007-11-19 15:00:29 +0000108 SET_IEEE80211_PERM_ADDR(hw, addr);
109
Daniel Drake74553ae2007-07-01 18:22:32 +0100110 return 0;
111}
112
Daniel Drake459c51a2007-11-19 15:00:29 +0000113int zd_mac_init_hw(struct ieee80211_hw *hw)
Daniel Drakee85d0912006-06-02 17:11:32 +0100114{
115 int r;
Daniel Drake459c51a2007-11-19 15:00:29 +0000116 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100117 struct zd_chip *chip = &mac->chip;
Daniel Drakee85d0912006-06-02 17:11:32 +0100118 u8 default_regdomain;
119
120 r = zd_chip_enable_int(chip);
121 if (r)
122 goto out;
Daniel Drake74553ae2007-07-01 18:22:32 +0100123 r = zd_chip_init_hw(chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100124 if (r)
125 goto disable_int;
126
Daniel Drakee85d0912006-06-02 17:11:32 +0100127 ZD_ASSERT(!irqs_disabled());
Daniel Drakee85d0912006-06-02 17:11:32 +0100128
129 r = zd_read_regdomain(chip, &default_regdomain);
130 if (r)
131 goto disable_int;
Daniel Drakee85d0912006-06-02 17:11:32 +0100132 spin_lock_irq(&mac->lock);
133 mac->regdomain = mac->default_regdomain = default_regdomain;
134 spin_unlock_irq(&mac->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +0100135
Daniel Drake40da08b2006-08-01 23:43:32 +0200136 /* We must inform the device that we are doing encryption/decryption in
137 * software at the moment. */
138 r = zd_set_encryption_type(chip, ENC_SNIFFER);
Daniel Drakee85d0912006-06-02 17:11:32 +0100139 if (r)
140 goto disable_int;
141
Daniel Drake459c51a2007-11-19 15:00:29 +0000142 zd_geo_init(hw, mac->regdomain);
Daniel Drakee85d0912006-06-02 17:11:32 +0100143
144 r = 0;
145disable_int:
146 zd_chip_disable_int(chip);
147out:
148 return r;
149}
150
151void zd_mac_clear(struct zd_mac *mac)
152{
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000153 flush_workqueue(zd_workqueue);
Daniel Drakee85d0912006-06-02 17:11:32 +0100154 zd_chip_clear(&mac->chip);
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100155 ZD_ASSERT(!spin_is_locked(&mac->lock));
156 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
Daniel Drakee85d0912006-06-02 17:11:32 +0100157}
158
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100159static int set_rx_filter(struct zd_mac *mac)
Daniel Drakee85d0912006-06-02 17:11:32 +0100160{
Daniel Drake459c51a2007-11-19 15:00:29 +0000161 unsigned long flags;
162 u32 filter = STA_RX_FILTER;
Daniel Drakee85d0912006-06-02 17:11:32 +0100163
Daniel Drake459c51a2007-11-19 15:00:29 +0000164 spin_lock_irqsave(&mac->lock, flags);
165 if (mac->pass_ctrl)
166 filter |= RX_FILTER_CTRL;
167 spin_unlock_irqrestore(&mac->lock, flags);
168
169 return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100170}
171
172static int set_mc_hash(struct zd_mac *mac)
173{
174 struct zd_mc_hash hash;
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100175 zd_mc_clear(&hash);
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100176 return zd_chip_set_multicast_hash(&mac->chip, &hash);
177}
178
Daniel Drake459c51a2007-11-19 15:00:29 +0000179static int zd_op_start(struct ieee80211_hw *hw)
Daniel Drakee85d0912006-06-02 17:11:32 +0100180{
Daniel Drake459c51a2007-11-19 15:00:29 +0000181 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100182 struct zd_chip *chip = &mac->chip;
Daniel Drake74553ae2007-07-01 18:22:32 +0100183 struct zd_usb *usb = &chip->usb;
Daniel Drakee85d0912006-06-02 17:11:32 +0100184 int r;
185
Daniel Drake74553ae2007-07-01 18:22:32 +0100186 if (!usb->initialized) {
187 r = zd_usb_init_hw(usb);
188 if (r)
189 goto out;
190 }
191
Daniel Drakee85d0912006-06-02 17:11:32 +0100192 r = zd_chip_enable_int(chip);
193 if (r < 0)
194 goto out;
195
196 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
197 if (r < 0)
198 goto disable_int;
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100199 r = set_rx_filter(mac);
200 if (r)
201 goto disable_int;
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100202 r = set_mc_hash(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100203 if (r)
204 goto disable_int;
205 r = zd_chip_switch_radio_on(chip);
206 if (r < 0)
207 goto disable_int;
Daniel Drake459c51a2007-11-19 15:00:29 +0000208 r = zd_chip_enable_rxtx(chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100209 if (r < 0)
210 goto disable_radio;
211 r = zd_chip_enable_hwint(chip);
212 if (r < 0)
Daniel Drake459c51a2007-11-19 15:00:29 +0000213 goto disable_rxtx;
Daniel Drakee85d0912006-06-02 17:11:32 +0100214
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100215 housekeeping_enable(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100216 return 0;
Daniel Drake459c51a2007-11-19 15:00:29 +0000217disable_rxtx:
218 zd_chip_disable_rxtx(chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100219disable_radio:
220 zd_chip_switch_radio_off(chip);
221disable_int:
222 zd_chip_disable_int(chip);
223out:
224 return r;
225}
226
Daniel Drake459c51a2007-11-19 15:00:29 +0000227static void zd_op_stop(struct ieee80211_hw *hw)
228{
229 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100230 struct zd_chip *chip = &mac->chip;
Daniel Drake459c51a2007-11-19 15:00:29 +0000231 struct sk_buff *skb;
232 struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
Daniel Drakee85d0912006-06-02 17:11:32 +0100233
Daniel Drake459c51a2007-11-19 15:00:29 +0000234 /* The order here deliberately is a little different from the open()
Daniel Drakee85d0912006-06-02 17:11:32 +0100235 * method, since we need to make sure there is no opportunity for RX
Daniel Drake459c51a2007-11-19 15:00:29 +0000236 * frames to be processed by mac80211 after we have stopped it.
Daniel Drakee85d0912006-06-02 17:11:32 +0100237 */
238
Daniel Drake459c51a2007-11-19 15:00:29 +0000239 zd_chip_disable_rxtx(chip);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100240 housekeeping_disable(mac);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000241 flush_workqueue(zd_workqueue);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000242
Daniel Drakee85d0912006-06-02 17:11:32 +0100243 zd_chip_disable_hwint(chip);
244 zd_chip_switch_radio_off(chip);
245 zd_chip_disable_int(chip);
246
Daniel Drake459c51a2007-11-19 15:00:29 +0000247
248 while ((skb = skb_dequeue(ack_wait_queue)))
Johannes Berge039fa42008-05-15 12:55:29 +0200249 dev_kfree_skb_any(skb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100250}
251
Daniel Drake459c51a2007-11-19 15:00:29 +0000252/**
253 * tx_status - reports tx status of a packet if required
254 * @hw - a &struct ieee80211_hw pointer
255 * @skb - a sk-buffer
Johannes Berge039fa42008-05-15 12:55:29 +0200256 * @flags: extra flags to set in the TX status info
257 * @ackssi: ACK signal strength
Daniel Drake459c51a2007-11-19 15:00:29 +0000258 * @success - True for successfull transmission of the frame
259 *
260 * This information calls ieee80211_tx_status_irqsafe() if required by the
261 * control information. It copies the control information into the status
262 * information.
263 *
264 * If no status information has been requested, the skb is freed.
265 */
266static void tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
Johannes Berge039fa42008-05-15 12:55:29 +0200267 u32 flags, int ackssi, bool success)
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000268{
Johannes Berge039fa42008-05-15 12:55:29 +0200269 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000270
Johannes Berge039fa42008-05-15 12:55:29 +0200271 memset(&info->status, 0, sizeof(info->status));
272
Daniel Drake459c51a2007-11-19 15:00:29 +0000273 if (!success)
Johannes Berge039fa42008-05-15 12:55:29 +0200274 info->status.excessive_retries = 1;
275 info->flags |= flags;
276 info->status.ack_signal = ackssi;
277 ieee80211_tx_status_irqsafe(hw, skb);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000278}
279
Daniel Drake459c51a2007-11-19 15:00:29 +0000280/**
281 * zd_mac_tx_failed - callback for failed frames
282 * @dev: the mac80211 wireless device
283 *
284 * This function is called if a frame couldn't be succesfully be
285 * transferred. The first frame from the tx queue, will be selected and
286 * reported as error to the upper layers.
287 */
288void zd_mac_tx_failed(struct ieee80211_hw *hw)
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000289{
Daniel Drake459c51a2007-11-19 15:00:29 +0000290 struct sk_buff_head *q = &zd_hw_mac(hw)->ack_wait_queue;
291 struct sk_buff *skb;
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000292
Daniel Drake459c51a2007-11-19 15:00:29 +0000293 skb = skb_dequeue(q);
294 if (skb == NULL)
295 return;
Johannes Berg5078ed52008-02-20 12:09:25 +0100296
Johannes Berge039fa42008-05-15 12:55:29 +0200297 tx_status(hw, skb, 0, 0, 0);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000298}
299
Daniel Drake459c51a2007-11-19 15:00:29 +0000300/**
301 * zd_mac_tx_to_dev - callback for USB layer
302 * @skb: a &sk_buff pointer
303 * @error: error value, 0 if transmission successful
304 *
305 * Informs the MAC layer that the frame has successfully transferred to the
306 * device. If an ACK is required and the transfer to the device has been
307 * successful, the packets are put on the @ack_wait_queue with
308 * the control set removed.
309 */
310void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
Daniel Drakee85d0912006-06-02 17:11:32 +0100311{
Johannes Berge039fa42008-05-15 12:55:29 +0200312 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
313 struct ieee80211_hw *hw = info->driver_data[0];
Daniel Drakee85d0912006-06-02 17:11:32 +0100314
Johannes Berge039fa42008-05-15 12:55:29 +0200315 skb_pull(skb, sizeof(struct zd_ctrlset));
316 if (unlikely(error ||
317 (info->flags & IEEE80211_TX_CTL_NO_ACK))) {
318 tx_status(hw, skb, 0, 0, !error);
Daniel Drake459c51a2007-11-19 15:00:29 +0000319 } else {
Johannes Berge039fa42008-05-15 12:55:29 +0200320 struct sk_buff_head *q =
321 &zd_hw_mac(hw)->ack_wait_queue;
322
323 skb_queue_tail(q, skb);
324 while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS)
325 zd_mac_tx_failed(hw);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000326 }
Daniel Drakee85d0912006-06-02 17:11:32 +0100327}
328
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000329static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
Daniel Drakee85d0912006-06-02 17:11:32 +0100330{
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100331 /* ZD_PURE_RATE() must be used to remove the modulation type flag of
Daniel Drake459c51a2007-11-19 15:00:29 +0000332 * the zd-rate values.
333 */
Daniel Drakee85d0912006-06-02 17:11:32 +0100334 static const u8 rate_divisor[] = {
Daniel Drake459c51a2007-11-19 15:00:29 +0000335 [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
336 [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
337 /* Bits must be doubled. */
338 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
339 [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
340 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
341 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
342 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
343 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
344 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
345 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
346 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
347 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
Daniel Drakee85d0912006-06-02 17:11:32 +0100348 };
349
350 u32 bits = (u32)tx_length * 8;
351 u32 divisor;
352
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100353 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
Daniel Drakee85d0912006-06-02 17:11:32 +0100354 if (divisor == 0)
355 return -EINVAL;
356
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000357 switch (zd_rate) {
358 case ZD_CCK_RATE_5_5M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100359 bits = (2*bits) + 10; /* round up to the next integer */
360 break;
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000361 case ZD_CCK_RATE_11M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100362 if (service) {
363 u32 t = bits % 11;
364 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
365 if (0 < t && t <= 3) {
366 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
367 }
368 }
369 bits += 10; /* round up to the next integer */
370 break;
371 }
372
373 return bits/divisor;
374}
375
Daniel Drakee85d0912006-06-02 17:11:32 +0100376static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
Daniel Drake459c51a2007-11-19 15:00:29 +0000377 struct ieee80211_hdr *header, u32 flags)
Daniel Drakee85d0912006-06-02 17:11:32 +0100378{
Daniel Drakee85d0912006-06-02 17:11:32 +0100379 /*
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000380 * CONTROL TODO:
Daniel Drakee85d0912006-06-02 17:11:32 +0100381 * - if backoff needed, enable bit 0
382 * - if burst (backoff not needed) disable bit 0
Daniel Drakee85d0912006-06-02 17:11:32 +0100383 */
384
385 cs->control = 0;
386
387 /* First fragment */
Johannes Berge039fa42008-05-15 12:55:29 +0200388 if (flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
Daniel Drakee85d0912006-06-02 17:11:32 +0100389 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
390
391 /* Multicast */
392 if (is_multicast_ether_addr(header->addr1))
393 cs->control |= ZD_CS_MULTICAST;
394
395 /* PS-POLL */
Harvey Harrison85365822008-06-14 23:33:39 -0700396 if (ieee80211_is_pspoll(header->frame_control))
Daniel Drakee85d0912006-06-02 17:11:32 +0100397 cs->control |= ZD_CS_PS_POLL_FRAME;
398
Johannes Berge039fa42008-05-15 12:55:29 +0200399 if (flags & IEEE80211_TX_CTL_USE_RTS_CTS)
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000400 cs->control |= ZD_CS_RTS;
401
Johannes Berge039fa42008-05-15 12:55:29 +0200402 if (flags & IEEE80211_TX_CTL_USE_CTS_PROTECT)
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000403 cs->control |= ZD_CS_SELF_CTS;
Daniel Drakee85d0912006-06-02 17:11:32 +0100404
405 /* FIXME: Management frame? */
406}
407
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100408static int zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon)
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800409{
410 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100411 int r;
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800412 u32 tmp, j = 0;
413 /* 4 more bytes for tail CRC */
414 u32 full_len = beacon->len + 4;
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100415
416 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 0);
417 if (r < 0)
418 return r;
419 r = zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
420 if (r < 0)
421 return r;
422
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800423 while (tmp & 0x2) {
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100424 r = zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
425 if (r < 0)
426 return r;
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800427 if ((++j % 100) == 0) {
428 printk(KERN_ERR "CR_BCN_FIFO_SEMAPHORE not ready\n");
429 if (j >= 500) {
430 printk(KERN_ERR "Giving up beacon config.\n");
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100431 return -ETIMEDOUT;
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800432 }
433 }
434 msleep(1);
435 }
436
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100437 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO, full_len - 1);
438 if (r < 0)
439 return r;
440 if (zd_chip_is_zd1211b(&mac->chip)) {
441 r = zd_iowrite32(&mac->chip, CR_BCN_LENGTH, full_len - 1);
442 if (r < 0)
443 return r;
444 }
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800445
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100446 for (j = 0 ; j < beacon->len; j++) {
447 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO,
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800448 *((u8 *)(beacon->data + j)));
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100449 if (r < 0)
450 return r;
451 }
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800452
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100453 for (j = 0; j < 4; j++) {
454 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO, 0x0);
455 if (r < 0)
456 return r;
457 }
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800458
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100459 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 1);
460 if (r < 0)
461 return r;
462
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800463 /* 802.11b/g 2.4G CCK 1Mb
464 * 802.11a, not yet implemented, uses different values (see GPL vendor
465 * driver)
466 */
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100467 return zd_iowrite32(&mac->chip, CR_BCN_PLCP_CFG, 0x00000400 |
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800468 (full_len << 19));
469}
470
Daniel Drakee85d0912006-06-02 17:11:32 +0100471static int fill_ctrlset(struct zd_mac *mac,
Johannes Berge039fa42008-05-15 12:55:29 +0200472 struct sk_buff *skb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100473{
474 int r;
Daniel Drake459c51a2007-11-19 15:00:29 +0000475 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
476 unsigned int frag_len = skb->len + FCS_LEN;
Daniel Drakee85d0912006-06-02 17:11:32 +0100477 unsigned int packet_length;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200478 struct ieee80211_rate *txrate;
Daniel Drakee85d0912006-06-02 17:11:32 +0100479 struct zd_ctrlset *cs = (struct zd_ctrlset *)
480 skb_push(skb, sizeof(struct zd_ctrlset));
Johannes Berge039fa42008-05-15 12:55:29 +0200481 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100482
Daniel Drakee85d0912006-06-02 17:11:32 +0100483 ZD_ASSERT(frag_len <= 0xffff);
Daniel Drakee85d0912006-06-02 17:11:32 +0100484
Johannes Berge039fa42008-05-15 12:55:29 +0200485 txrate = ieee80211_get_tx_rate(mac->hw, info);
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200486
487 cs->modulation = txrate->hw_value;
Johannes Berge039fa42008-05-15 12:55:29 +0200488 if (info->flags & IEEE80211_TX_CTL_SHORT_PREAMBLE)
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200489 cs->modulation = txrate->hw_value_short;
Daniel Drakee85d0912006-06-02 17:11:32 +0100490
491 cs->tx_length = cpu_to_le16(frag_len);
492
Johannes Berge039fa42008-05-15 12:55:29 +0200493 cs_set_control(mac, cs, hdr, info->flags);
Daniel Drakee85d0912006-06-02 17:11:32 +0100494
495 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
496 ZD_ASSERT(packet_length <= 0xffff);
497 /* ZD1211B: Computing the length difference this way, gives us
498 * flexibility to compute the packet length.
499 */
Daniel Drake74553ae2007-07-01 18:22:32 +0100500 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
Daniel Drakee85d0912006-06-02 17:11:32 +0100501 packet_length - frag_len : packet_length);
502
503 /*
504 * CURRENT LENGTH:
505 * - transmit frame length in microseconds
506 * - seems to be derived from frame length
507 * - see Cal_Us_Service() in zdinlinef.h
508 * - if macp->bTxBurstEnable is enabled, then multiply by 4
509 * - bTxBurstEnable is never set in the vendor driver
510 *
511 * SERVICE:
512 * - "for PLCP configuration"
513 * - always 0 except in some situations at 802.11b 11M
514 * - see line 53 of zdinlinef.h
515 */
516 cs->service = 0;
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100517 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
Daniel Drakee85d0912006-06-02 17:11:32 +0100518 le16_to_cpu(cs->tx_length));
519 if (r < 0)
520 return r;
521 cs->current_length = cpu_to_le16(r);
Daniel Drake459c51a2007-11-19 15:00:29 +0000522 cs->next_frame_length = 0;
Daniel Drakee85d0912006-06-02 17:11:32 +0100523
524 return 0;
525}
526
Daniel Drake459c51a2007-11-19 15:00:29 +0000527/**
528 * zd_op_tx - transmits a network frame to the device
Ulrich Kunitz741fec52006-11-22 00:05:53 +0000529 *
Daniel Drake459c51a2007-11-19 15:00:29 +0000530 * @dev: mac80211 hardware device
531 * @skb: socket buffer
532 * @control: the control structure
Daniel Drakee85d0912006-06-02 17:11:32 +0100533 *
Daniel Drake459c51a2007-11-19 15:00:29 +0000534 * This function transmit an IEEE 802.11 network frame to the device. The
535 * control block of the skbuff will be initialized. If necessary the incoming
536 * mac80211 queues will be stopped.
Daniel Drakee85d0912006-06-02 17:11:32 +0100537 */
Johannes Berge039fa42008-05-15 12:55:29 +0200538static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100539{
Daniel Drake459c51a2007-11-19 15:00:29 +0000540 struct zd_mac *mac = zd_hw_mac(hw);
Johannes Berge039fa42008-05-15 12:55:29 +0200541 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Daniel Drake459c51a2007-11-19 15:00:29 +0000542 int r;
Daniel Drakee85d0912006-06-02 17:11:32 +0100543
Johannes Berge039fa42008-05-15 12:55:29 +0200544 r = fill_ctrlset(mac, skb);
Daniel Drake459c51a2007-11-19 15:00:29 +0000545 if (r)
546 return r;
Daniel Drakee85d0912006-06-02 17:11:32 +0100547
Johannes Berge039fa42008-05-15 12:55:29 +0200548 info->driver_data[0] = hw;
549
550 r = zd_usb_tx(&mac->chip.usb, skb);
Daniel Drake459c51a2007-11-19 15:00:29 +0000551 if (r)
552 return r;
Daniel Drake459c51a2007-11-19 15:00:29 +0000553 return 0;
554}
555
556/**
557 * filter_ack - filters incoming packets for acknowledgements
558 * @dev: the mac80211 device
559 * @rx_hdr: received header
560 * @stats: the status for the received packet
561 *
562 * This functions looks for ACK packets and tries to match them with the
563 * frames in the tx queue. If a match is found the frame will be dequeued and
564 * the upper layers is informed about the successful transmission. If
565 * mac80211 queues have been stopped and the number of frames still to be
566 * transmitted is low the queues will be opened again.
567 *
568 * Returns 1 if the frame was an ACK, 0 if it was ignored.
569 */
570static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
571 struct ieee80211_rx_status *stats)
572{
Daniel Drake459c51a2007-11-19 15:00:29 +0000573 struct sk_buff *skb;
574 struct sk_buff_head *q;
575 unsigned long flags;
576
Harvey Harrison85365822008-06-14 23:33:39 -0700577 if (!ieee80211_is_ack(rx_hdr->frame_control))
Daniel Drake459c51a2007-11-19 15:00:29 +0000578 return 0;
579
580 q = &zd_hw_mac(hw)->ack_wait_queue;
581 spin_lock_irqsave(&q->lock, flags);
582 for (skb = q->next; skb != (struct sk_buff *)q; skb = skb->next) {
583 struct ieee80211_hdr *tx_hdr;
584
585 tx_hdr = (struct ieee80211_hdr *)skb->data;
586 if (likely(!compare_ether_addr(tx_hdr->addr2, rx_hdr->addr1)))
587 {
Daniel Drake459c51a2007-11-19 15:00:29 +0000588 __skb_unlink(skb, q);
Johannes Berge039fa42008-05-15 12:55:29 +0200589 tx_status(hw, skb, IEEE80211_TX_STAT_ACK, stats->signal, 1);
Daniel Drake459c51a2007-11-19 15:00:29 +0000590 goto out;
591 }
592 }
593out:
594 spin_unlock_irqrestore(&q->lock, flags);
595 return 1;
596}
597
598int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
599{
600 struct zd_mac *mac = zd_hw_mac(hw);
601 struct ieee80211_rx_status stats;
602 const struct rx_status *status;
603 struct sk_buff *skb;
604 int bad_frame = 0;
Harvey Harrison85365822008-06-14 23:33:39 -0700605 __le16 fc;
606 int need_padding;
Johannes Berg8318d782008-01-24 19:38:38 +0100607 int i;
608 u8 rate;
Daniel Drake459c51a2007-11-19 15:00:29 +0000609
610 if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
611 FCS_LEN + sizeof(struct rx_status))
Daniel Drakee85d0912006-06-02 17:11:32 +0100612 return -EINVAL;
613
Daniel Drake459c51a2007-11-19 15:00:29 +0000614 memset(&stats, 0, sizeof(stats));
Daniel Drakee85d0912006-06-02 17:11:32 +0100615
Daniel Drake459c51a2007-11-19 15:00:29 +0000616 /* Note about pass_failed_fcs and pass_ctrl access below:
617 * mac locking intentionally omitted here, as this is the only unlocked
618 * reader and the only writer is configure_filter. Plus, if there were
619 * any races accessing these variables, it wouldn't really matter.
620 * If mac80211 ever provides a way for us to access filter flags
621 * from outside configure_filter, we could improve on this. Also, this
622 * situation may change once we implement some kind of DMA-into-skb
623 * RX path. */
Daniel Drakee85d0912006-06-02 17:11:32 +0100624
Daniel Drake459c51a2007-11-19 15:00:29 +0000625 /* Caller has to ensure that length >= sizeof(struct rx_status). */
626 status = (struct rx_status *)
Ulrich Kunitz937a0492007-10-02 18:36:53 +0100627 (buffer + (length - sizeof(struct rx_status)));
Daniel Drakee85d0912006-06-02 17:11:32 +0100628 if (status->frame_status & ZD_RX_ERROR) {
Daniel Drake459c51a2007-11-19 15:00:29 +0000629 if (mac->pass_failed_fcs &&
630 (status->frame_status & ZD_RX_CRC32_ERROR)) {
631 stats.flag |= RX_FLAG_FAILED_FCS_CRC;
632 bad_frame = 1;
633 } else {
634 return -EINVAL;
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000635 }
Daniel Drakee85d0912006-06-02 17:11:32 +0100636 }
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000637
Johannes Berg8318d782008-01-24 19:38:38 +0100638 stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
639 stats.band = IEEE80211_BAND_2GHZ;
Bruno Randolf566bfe52008-05-08 19:15:40 +0200640 stats.signal = status->signal_strength;
641 stats.qual = zd_rx_qual_percent(buffer,
Daniel Drakee85d0912006-06-02 17:11:32 +0100642 length - sizeof(struct rx_status),
643 status);
Johannes Berg8318d782008-01-24 19:38:38 +0100644
645 rate = zd_rx_rate(buffer, status);
646
647 /* todo: return index in the big switches in zd_rx_rate instead */
648 for (i = 0; i < mac->band.n_bitrates; i++)
649 if (rate == mac->band.bitrates[i].hw_value)
650 stats.rate_idx = i;
Daniel Drakee85d0912006-06-02 17:11:32 +0100651
Daniel Drake459c51a2007-11-19 15:00:29 +0000652 length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
653 buffer += ZD_PLCP_HEADER_SIZE;
Daniel Drakee85d0912006-06-02 17:11:32 +0100654
Daniel Drake459c51a2007-11-19 15:00:29 +0000655 /* Except for bad frames, filter each frame to see if it is an ACK, in
656 * which case our internal TX tracking is updated. Normally we then
657 * bail here as there's no need to pass ACKs on up to the stack, but
658 * there is also the case where the stack has requested us to pass
659 * control frames on up (pass_ctrl) which we must consider. */
660 if (!bad_frame &&
661 filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
662 && !mac->pass_ctrl)
663 return 0;
Daniel Drakee85d0912006-06-02 17:11:32 +0100664
Harvey Harrison85365822008-06-14 23:33:39 -0700665 fc = *(__le16 *)buffer;
666 need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
Michael Buesch90817282007-12-29 17:24:23 +0100667
668 skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
Daniel Drake459c51a2007-11-19 15:00:29 +0000669 if (skb == NULL)
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800670 return -ENOMEM;
Michael Buesch90817282007-12-29 17:24:23 +0100671 if (need_padding) {
672 /* Make sure the the payload data is 4 byte aligned. */
673 skb_reserve(skb, 2);
674 }
675
Daniel Drake459c51a2007-11-19 15:00:29 +0000676 memcpy(skb_put(skb, length), buffer, length);
677
678 ieee80211_rx_irqsafe(hw, skb, &stats);
Daniel Drakee85d0912006-06-02 17:11:32 +0100679 return 0;
680}
681
Daniel Drake459c51a2007-11-19 15:00:29 +0000682static int zd_op_add_interface(struct ieee80211_hw *hw,
683 struct ieee80211_if_init_conf *conf)
Daniel Drakee85d0912006-06-02 17:11:32 +0100684{
Daniel Drake459c51a2007-11-19 15:00:29 +0000685 struct zd_mac *mac = zd_hw_mac(hw);
686
687 /* using IEEE80211_IF_TYPE_INVALID to indicate no mode selected */
688 if (mac->type != IEEE80211_IF_TYPE_INVALID)
689 return -EOPNOTSUPP;
690
691 switch (conf->type) {
692 case IEEE80211_IF_TYPE_MNTR:
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800693 case IEEE80211_IF_TYPE_MESH_POINT:
Daniel Drake459c51a2007-11-19 15:00:29 +0000694 case IEEE80211_IF_TYPE_STA:
Bruno Randolf84e6dc92008-05-07 11:46:02 +0200695 case IEEE80211_IF_TYPE_IBSS:
Daniel Drake459c51a2007-11-19 15:00:29 +0000696 mac->type = conf->type;
697 break;
698 default:
699 return -EOPNOTSUPP;
700 }
701
702 return zd_write_mac_addr(&mac->chip, conf->mac_addr);
Daniel Drakee85d0912006-06-02 17:11:32 +0100703}
704
Daniel Drake459c51a2007-11-19 15:00:29 +0000705static void zd_op_remove_interface(struct ieee80211_hw *hw,
706 struct ieee80211_if_init_conf *conf)
Daniel Drakee85d0912006-06-02 17:11:32 +0100707{
Daniel Drake459c51a2007-11-19 15:00:29 +0000708 struct zd_mac *mac = zd_hw_mac(hw);
709 mac->type = IEEE80211_IF_TYPE_INVALID;
710 zd_write_mac_addr(&mac->chip, NULL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100711}
712
Daniel Drake459c51a2007-11-19 15:00:29 +0000713static int zd_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Daniel Drakee85d0912006-06-02 17:11:32 +0100714{
Daniel Drake459c51a2007-11-19 15:00:29 +0000715 struct zd_mac *mac = zd_hw_mac(hw);
Johannes Berg8318d782008-01-24 19:38:38 +0100716 return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
Daniel Drakee85d0912006-06-02 17:11:32 +0100717}
718
Johannes Berg32bfd352007-12-19 01:31:26 +0100719static int zd_op_config_interface(struct ieee80211_hw *hw,
720 struct ieee80211_vif *vif,
Daniel Drake459c51a2007-11-19 15:00:29 +0000721 struct ieee80211_if_conf *conf)
Daniel Drakee85d0912006-06-02 17:11:32 +0100722{
Daniel Drake459c51a2007-11-19 15:00:29 +0000723 struct zd_mac *mac = zd_hw_mac(hw);
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800724 int associated;
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100725 int r;
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800726
Bruno Randolf84e6dc92008-05-07 11:46:02 +0200727 if (mac->type == IEEE80211_IF_TYPE_MESH_POINT ||
728 mac->type == IEEE80211_IF_TYPE_IBSS) {
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800729 associated = true;
730 if (conf->beacon) {
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100731 r = zd_mac_config_beacon(hw, conf->beacon);
732 if (r < 0)
733 return r;
734 r = zd_set_beacon_interval(&mac->chip, BCN_MODE_IBSS |
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800735 hw->conf.beacon_int);
Daniel Drakef2cae6c2008-07-04 04:30:49 +0100736 if (r < 0)
737 return r;
738 kfree_skb(conf->beacon);
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800739 }
740 } else
741 associated = is_valid_ether_addr(conf->bssid);
Daniel Drakee85d0912006-06-02 17:11:32 +0100742
Daniel Drakee85d0912006-06-02 17:11:32 +0100743 spin_lock_irq(&mac->lock);
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800744 mac->associated = associated;
Daniel Drakee85d0912006-06-02 17:11:32 +0100745 spin_unlock_irq(&mac->lock);
Daniel Drake459c51a2007-11-19 15:00:29 +0000746
747 /* TODO: do hardware bssid filtering */
748 return 0;
749}
750
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800751void zd_process_intr(struct work_struct *work)
752{
753 u16 int_status;
754 struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
755
Al Virod63ddce2008-05-21 01:34:30 +0100756 int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer+4));
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800757 if (int_status & INT_CFG_NEXT_BCN) {
758 if (net_ratelimit())
759 dev_dbg_f(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");
760 } else
761 dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
762
763 zd_chip_enable_hwint(&mac->chip);
764}
765
766
Daniel Drake459c51a2007-11-19 15:00:29 +0000767static void set_multicast_hash_handler(struct work_struct *work)
768{
769 struct zd_mac *mac =
770 container_of(work, struct zd_mac, set_multicast_hash_work);
771 struct zd_mc_hash hash;
772
773 spin_lock_irq(&mac->lock);
774 hash = mac->multicast_hash;
775 spin_unlock_irq(&mac->lock);
776
777 zd_chip_set_multicast_hash(&mac->chip, &hash);
778}
779
780static void set_rx_filter_handler(struct work_struct *work)
781{
782 struct zd_mac *mac =
783 container_of(work, struct zd_mac, set_rx_filter_work);
784 int r;
785
786 dev_dbg_f(zd_mac_dev(mac), "\n");
787 r = set_rx_filter(mac);
788 if (r)
789 dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r);
790}
791
792#define SUPPORTED_FIF_FLAGS \
793 (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
Michael Buesch2c1a1b12008-02-10 16:03:55 +0100794 FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
Daniel Drake459c51a2007-11-19 15:00:29 +0000795static void zd_op_configure_filter(struct ieee80211_hw *hw,
796 unsigned int changed_flags,
797 unsigned int *new_flags,
798 int mc_count, struct dev_mc_list *mclist)
799{
800 struct zd_mc_hash hash;
801 struct zd_mac *mac = zd_hw_mac(hw);
802 unsigned long flags;
803 int i;
804
805 /* Only deal with supported flags */
806 changed_flags &= SUPPORTED_FIF_FLAGS;
807 *new_flags &= SUPPORTED_FIF_FLAGS;
808
809 /* changed_flags is always populated but this driver
810 * doesn't support all FIF flags so its possible we don't
811 * need to do anything */
812 if (!changed_flags)
813 return;
814
815 if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI)) {
816 zd_mc_add_all(&hash);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +0100817 } else {
Daniel Drake459c51a2007-11-19 15:00:29 +0000818 DECLARE_MAC_BUF(macbuf);
819
820 zd_mc_clear(&hash);
821 for (i = 0; i < mc_count; i++) {
822 if (!mclist)
823 break;
824 dev_dbg_f(zd_mac_dev(mac), "mc addr %s\n",
825 print_mac(macbuf, mclist->dmi_addr));
826 zd_mc_add_addr(&hash, mclist->dmi_addr);
827 mclist = mclist->next;
828 }
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +0100829 }
Daniel Drake459c51a2007-11-19 15:00:29 +0000830
831 spin_lock_irqsave(&mac->lock, flags);
832 mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
833 mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
834 mac->multicast_hash = hash;
835 spin_unlock_irqrestore(&mac->lock, flags);
836 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
837
838 if (changed_flags & FIF_CONTROL)
839 queue_work(zd_workqueue, &mac->set_rx_filter_work);
840
841 /* no handling required for FIF_OTHER_BSS as we don't currently
842 * do BSSID filtering */
843 /* FIXME: in future it would be nice to enable the probe response
844 * filter (so that the driver doesn't see them) until
845 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
846 * have to schedule work to enable prbresp reception, which might
847 * happen too late. For now we'll just listen and forward them all the
848 * time. */
849}
850
851static void set_rts_cts_work(struct work_struct *work)
852{
853 struct zd_mac *mac =
854 container_of(work, struct zd_mac, set_rts_cts_work);
855 unsigned long flags;
856 unsigned int short_preamble;
857
858 mutex_lock(&mac->chip.mutex);
859
860 spin_lock_irqsave(&mac->lock, flags);
861 mac->updating_rts_rate = 0;
862 short_preamble = mac->short_preamble;
863 spin_unlock_irqrestore(&mac->lock, flags);
864
865 zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
866 mutex_unlock(&mac->chip.mutex);
867}
868
Johannes Berg471b3ef2007-12-28 14:32:58 +0100869static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
870 struct ieee80211_vif *vif,
871 struct ieee80211_bss_conf *bss_conf,
872 u32 changes)
Daniel Drake459c51a2007-11-19 15:00:29 +0000873{
874 struct zd_mac *mac = zd_hw_mac(hw);
875 unsigned long flags;
876
877 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
878
Johannes Berg471b3ef2007-12-28 14:32:58 +0100879 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Daniel Drake459c51a2007-11-19 15:00:29 +0000880 spin_lock_irqsave(&mac->lock, flags);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100881 mac->short_preamble = bss_conf->use_short_preamble;
Daniel Drake459c51a2007-11-19 15:00:29 +0000882 if (!mac->updating_rts_rate) {
883 mac->updating_rts_rate = 1;
884 /* FIXME: should disable TX here, until work has
885 * completed and RTS_CTS reg is updated */
886 queue_work(zd_workqueue, &mac->set_rts_cts_work);
887 }
888 spin_unlock_irqrestore(&mac->lock, flags);
889 }
890}
891
Bruno Randolf84e6dc92008-05-07 11:46:02 +0200892static int zd_op_beacon_update(struct ieee80211_hw *hw,
Johannes Berge039fa42008-05-15 12:55:29 +0200893 struct sk_buff *skb)
Bruno Randolf84e6dc92008-05-07 11:46:02 +0200894{
895 struct zd_mac *mac = zd_hw_mac(hw);
896 zd_mac_config_beacon(hw, skb);
897 kfree_skb(skb);
898 zd_set_beacon_interval(&mac->chip, BCN_MODE_IBSS |
899 hw->conf.beacon_int);
900 return 0;
901}
902
Daniel Drake459c51a2007-11-19 15:00:29 +0000903static const struct ieee80211_ops zd_ops = {
904 .tx = zd_op_tx,
905 .start = zd_op_start,
906 .stop = zd_op_stop,
907 .add_interface = zd_op_add_interface,
908 .remove_interface = zd_op_remove_interface,
909 .config = zd_op_config,
910 .config_interface = zd_op_config_interface,
911 .configure_filter = zd_op_configure_filter,
Johannes Berg471b3ef2007-12-28 14:32:58 +0100912 .bss_info_changed = zd_op_bss_info_changed,
Bruno Randolf84e6dc92008-05-07 11:46:02 +0200913 .beacon_update = zd_op_beacon_update,
Daniel Drake459c51a2007-11-19 15:00:29 +0000914};
915
916struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
917{
918 struct zd_mac *mac;
919 struct ieee80211_hw *hw;
Daniel Drake459c51a2007-11-19 15:00:29 +0000920
921 hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
922 if (!hw) {
923 dev_dbg_f(&intf->dev, "out of memory\n");
924 return NULL;
925 }
926
927 mac = zd_hw_mac(hw);
928
929 memset(mac, 0, sizeof(*mac));
930 spin_lock_init(&mac->lock);
931 mac->hw = hw;
932
933 mac->type = IEEE80211_IF_TYPE_INVALID;
934
935 memcpy(mac->channels, zd_channels, sizeof(zd_channels));
936 memcpy(mac->rates, zd_rates, sizeof(zd_rates));
Johannes Berg8318d782008-01-24 19:38:38 +0100937 mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
938 mac->band.bitrates = mac->rates;
939 mac->band.n_channels = ARRAY_SIZE(zd_channels);
940 mac->band.channels = mac->channels;
Daniel Drake459c51a2007-11-19 15:00:29 +0000941
Johannes Berg8318d782008-01-24 19:38:38 +0100942 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
943
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800944 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
Bruno Randolf566bfe52008-05-08 19:15:40 +0200945 IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE |
946 IEEE80211_HW_SIGNAL_DB;
Daniel Drake459c51a2007-11-19 15:00:29 +0000947
Bruno Randolf566bfe52008-05-08 19:15:40 +0200948 hw->max_signal = 100;
Daniel Drake459c51a2007-11-19 15:00:29 +0000949 hw->queues = 1;
950 hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
951
952 skb_queue_head_init(&mac->ack_wait_queue);
953
Daniel Drake459c51a2007-11-19 15:00:29 +0000954 zd_chip_init(&mac->chip, hw, intf);
955 housekeeping_init(mac);
956 INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
957 INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
958 INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler);
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800959 INIT_WORK(&mac->process_intr, zd_process_intr);
Daniel Drake459c51a2007-11-19 15:00:29 +0000960
961 SET_IEEE80211_DEV(hw, &intf->dev);
962 return hw;
Daniel Drakee85d0912006-06-02 17:11:32 +0100963}
964
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100965#define LINK_LED_WORK_DELAY HZ
966
David Howellsc4028952006-11-22 14:57:56 +0000967static void link_led_handler(struct work_struct *work)
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100968{
David Howellsc4028952006-11-22 14:57:56 +0000969 struct zd_mac *mac =
970 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100971 struct zd_chip *chip = &mac->chip;
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100972 int is_associated;
973 int r;
974
975 spin_lock_irq(&mac->lock);
Daniel Drake459c51a2007-11-19 15:00:29 +0000976 is_associated = mac->associated;
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100977 spin_unlock_irq(&mac->lock);
978
979 r = zd_chip_control_leds(chip,
980 is_associated ? LED_ASSOCIATED : LED_SCANNING);
981 if (r)
Daniel Drake459c51a2007-11-19 15:00:29 +0000982 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100983
984 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
985 LINK_LED_WORK_DELAY);
986}
987
988static void housekeeping_init(struct zd_mac *mac)
989{
David Howellsc4028952006-11-22 14:57:56 +0000990 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100991}
992
993static void housekeeping_enable(struct zd_mac *mac)
994{
995 dev_dbg_f(zd_mac_dev(mac), "\n");
996 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
997 0);
998}
999
1000static void housekeeping_disable(struct zd_mac *mac)
1001{
1002 dev_dbg_f(zd_mac_dev(mac), "\n");
1003 cancel_rearming_delayed_workqueue(zd_workqueue,
1004 &mac->housekeeping.link_led_work);
1005 zd_chip_control_leds(&mac->chip, LED_OFF);
1006}