blob: 99c508c09e5ba791c3dabc262fe6a9bb95e41b10 [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 Kunitz583afd12006-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 Kunitz583afd12006-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 +0000227/**
228 * clear_tx_skb_control_block - clears the control block of tx skbuffs
229 * @skb: a &struct sk_buff pointer
230 *
231 * This clears the control block of skbuff buffers, which were transmitted to
232 * the device. Notify that the function is not thread-safe, so prevent
233 * multiple calls.
234 */
235static void clear_tx_skb_control_block(struct sk_buff *skb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100236{
Daniel Drake459c51a2007-11-19 15:00:29 +0000237 struct zd_tx_skb_control_block *cb =
238 (struct zd_tx_skb_control_block *)skb->cb;
239
240 kfree(cb->control);
241 cb->control = NULL;
242}
243
244/**
245 * kfree_tx_skb - frees a tx skbuff
246 * @skb: a &struct sk_buff pointer
247 *
248 * Frees the tx skbuff. Frees also the allocated control structure in the
249 * control block if necessary.
250 */
251static void kfree_tx_skb(struct sk_buff *skb)
252{
253 clear_tx_skb_control_block(skb);
254 dev_kfree_skb_any(skb);
255}
256
257static void zd_op_stop(struct ieee80211_hw *hw)
258{
259 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100260 struct zd_chip *chip = &mac->chip;
Daniel Drake459c51a2007-11-19 15:00:29 +0000261 struct sk_buff *skb;
262 struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
Daniel Drakee85d0912006-06-02 17:11:32 +0100263
Daniel Drake459c51a2007-11-19 15:00:29 +0000264 /* The order here deliberately is a little different from the open()
Daniel Drakee85d0912006-06-02 17:11:32 +0100265 * method, since we need to make sure there is no opportunity for RX
Daniel Drake459c51a2007-11-19 15:00:29 +0000266 * frames to be processed by mac80211 after we have stopped it.
Daniel Drakee85d0912006-06-02 17:11:32 +0100267 */
268
Daniel Drake459c51a2007-11-19 15:00:29 +0000269 zd_chip_disable_rxtx(chip);
Ulrich Kunitz583afd12006-09-13 02:42:38 +0100270 housekeeping_disable(mac);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000271 flush_workqueue(zd_workqueue);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000272
Daniel Drakee85d0912006-06-02 17:11:32 +0100273 zd_chip_disable_hwint(chip);
274 zd_chip_switch_radio_off(chip);
275 zd_chip_disable_int(chip);
276
Daniel Drake459c51a2007-11-19 15:00:29 +0000277
278 while ((skb = skb_dequeue(ack_wait_queue)))
279 kfree_tx_skb(skb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100280}
281
Daniel Drake459c51a2007-11-19 15:00:29 +0000282/**
283 * init_tx_skb_control_block - initializes skb control block
284 * @skb: a &sk_buff pointer
285 * @dev: pointer to the mac80221 device
286 * @control: mac80211 tx control applying for the frame in @skb
287 *
288 * Initializes the control block of the skbuff to be transmitted.
289 */
290static int init_tx_skb_control_block(struct sk_buff *skb,
291 struct ieee80211_hw *hw,
292 struct ieee80211_tx_control *control)
Daniel Drakee85d0912006-06-02 17:11:32 +0100293{
Daniel Drake459c51a2007-11-19 15:00:29 +0000294 struct zd_tx_skb_control_block *cb =
295 (struct zd_tx_skb_control_block *)skb->cb;
Daniel Drakee85d0912006-06-02 17:11:32 +0100296
Daniel Drake459c51a2007-11-19 15:00:29 +0000297 ZD_ASSERT(sizeof(*cb) <= sizeof(skb->cb));
298 memset(cb, 0, sizeof(*cb));
299 cb->hw= hw;
300 cb->control = kmalloc(sizeof(*control), GFP_ATOMIC);
301 if (cb->control == NULL)
302 return -ENOMEM;
303 memcpy(cb->control, control, sizeof(*control));
Daniel Drakee85d0912006-06-02 17:11:32 +0100304
305 return 0;
306}
307
Daniel Drake459c51a2007-11-19 15:00:29 +0000308/**
309 * tx_status - reports tx status of a packet if required
310 * @hw - a &struct ieee80211_hw pointer
311 * @skb - a sk-buffer
312 * @status - the tx status of the packet without control information
313 * @success - True for successfull transmission of the frame
314 *
315 * This information calls ieee80211_tx_status_irqsafe() if required by the
316 * control information. It copies the control information into the status
317 * information.
318 *
319 * If no status information has been requested, the skb is freed.
320 */
321static void tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
322 struct ieee80211_tx_status *status,
323 bool success)
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000324{
Daniel Drake459c51a2007-11-19 15:00:29 +0000325 struct zd_tx_skb_control_block *cb = (struct zd_tx_skb_control_block *)
326 skb->cb;
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000327
Daniel Drake459c51a2007-11-19 15:00:29 +0000328 ZD_ASSERT(cb->control != NULL);
329 memcpy(&status->control, cb->control, sizeof(status->control));
330 if (!success)
331 status->excessive_retries = 1;
332 clear_tx_skb_control_block(skb);
333 ieee80211_tx_status_irqsafe(hw, skb, status);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000334}
335
Daniel Drake459c51a2007-11-19 15:00:29 +0000336/**
337 * zd_mac_tx_failed - callback for failed frames
338 * @dev: the mac80211 wireless device
339 *
340 * This function is called if a frame couldn't be succesfully be
341 * transferred. The first frame from the tx queue, will be selected and
342 * reported as error to the upper layers.
343 */
344void zd_mac_tx_failed(struct ieee80211_hw *hw)
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000345{
Daniel Drake459c51a2007-11-19 15:00:29 +0000346 struct sk_buff_head *q = &zd_hw_mac(hw)->ack_wait_queue;
347 struct sk_buff *skb;
Johannes Berg5078ed52008-02-20 12:09:25 +0100348 struct ieee80211_tx_status status;
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000349
Daniel Drake459c51a2007-11-19 15:00:29 +0000350 skb = skb_dequeue(q);
351 if (skb == NULL)
352 return;
Johannes Berg5078ed52008-02-20 12:09:25 +0100353
354 memset(&status, 0, sizeof(status));
355
Daniel Drake459c51a2007-11-19 15:00:29 +0000356 tx_status(hw, skb, &status, 0);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000357}
358
Daniel Drake459c51a2007-11-19 15:00:29 +0000359/**
360 * zd_mac_tx_to_dev - callback for USB layer
361 * @skb: a &sk_buff pointer
362 * @error: error value, 0 if transmission successful
363 *
364 * Informs the MAC layer that the frame has successfully transferred to the
365 * device. If an ACK is required and the transfer to the device has been
366 * successful, the packets are put on the @ack_wait_queue with
367 * the control set removed.
368 */
369void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
Daniel Drakee85d0912006-06-02 17:11:32 +0100370{
Daniel Drake459c51a2007-11-19 15:00:29 +0000371 struct zd_tx_skb_control_block *cb =
372 (struct zd_tx_skb_control_block *)skb->cb;
373 struct ieee80211_hw *hw = cb->hw;
Daniel Drakee85d0912006-06-02 17:11:32 +0100374
Daniel Drake459c51a2007-11-19 15:00:29 +0000375 if (likely(cb->control)) {
376 skb_pull(skb, sizeof(struct zd_ctrlset));
377 if (unlikely(error ||
378 (cb->control->flags & IEEE80211_TXCTL_NO_ACK)))
379 {
Johannes Berg5078ed52008-02-20 12:09:25 +0100380 struct ieee80211_tx_status status;
381 memset(&status, 0, sizeof(status));
Daniel Drake459c51a2007-11-19 15:00:29 +0000382 tx_status(hw, skb, &status, !error);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000383 } else {
Daniel Drake459c51a2007-11-19 15:00:29 +0000384 struct sk_buff_head *q =
385 &zd_hw_mac(hw)->ack_wait_queue;
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000386
Daniel Drake459c51a2007-11-19 15:00:29 +0000387 skb_queue_tail(q, skb);
388 while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS)
389 zd_mac_tx_failed(hw);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000390 }
Daniel Drake459c51a2007-11-19 15:00:29 +0000391 } else {
392 kfree_tx_skb(skb);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000393 }
Daniel Drakee85d0912006-06-02 17:11:32 +0100394}
395
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000396static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
Daniel Drakee85d0912006-06-02 17:11:32 +0100397{
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100398 /* ZD_PURE_RATE() must be used to remove the modulation type flag of
Daniel Drake459c51a2007-11-19 15:00:29 +0000399 * the zd-rate values.
400 */
Daniel Drakee85d0912006-06-02 17:11:32 +0100401 static const u8 rate_divisor[] = {
Daniel Drake459c51a2007-11-19 15:00:29 +0000402 [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
403 [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
404 /* Bits must be doubled. */
405 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
406 [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
407 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
408 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
409 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
410 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
411 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
412 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
413 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
414 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
Daniel Drakee85d0912006-06-02 17:11:32 +0100415 };
416
417 u32 bits = (u32)tx_length * 8;
418 u32 divisor;
419
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100420 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
Daniel Drakee85d0912006-06-02 17:11:32 +0100421 if (divisor == 0)
422 return -EINVAL;
423
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000424 switch (zd_rate) {
425 case ZD_CCK_RATE_5_5M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100426 bits = (2*bits) + 10; /* round up to the next integer */
427 break;
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000428 case ZD_CCK_RATE_11M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100429 if (service) {
430 u32 t = bits % 11;
431 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
432 if (0 < t && t <= 3) {
433 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
434 }
435 }
436 bits += 10; /* round up to the next integer */
437 break;
438 }
439
440 return bits/divisor;
441}
442
Daniel Drakee85d0912006-06-02 17:11:32 +0100443static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
Daniel Drake459c51a2007-11-19 15:00:29 +0000444 struct ieee80211_hdr *header, u32 flags)
Daniel Drakee85d0912006-06-02 17:11:32 +0100445{
Daniel Drake459c51a2007-11-19 15:00:29 +0000446 u16 fctl = le16_to_cpu(header->frame_control);
Daniel Drakee85d0912006-06-02 17:11:32 +0100447
448 /*
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000449 * CONTROL TODO:
Daniel Drakee85d0912006-06-02 17:11:32 +0100450 * - if backoff needed, enable bit 0
451 * - if burst (backoff not needed) disable bit 0
Daniel Drakee85d0912006-06-02 17:11:32 +0100452 */
453
454 cs->control = 0;
455
456 /* First fragment */
Daniel Drake459c51a2007-11-19 15:00:29 +0000457 if (flags & IEEE80211_TXCTL_FIRST_FRAGMENT)
Daniel Drakee85d0912006-06-02 17:11:32 +0100458 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
459
460 /* Multicast */
461 if (is_multicast_ether_addr(header->addr1))
462 cs->control |= ZD_CS_MULTICAST;
463
464 /* PS-POLL */
Daniel Drake459c51a2007-11-19 15:00:29 +0000465 if ((fctl & (IEEE80211_FCTL_FTYPE|IEEE80211_FCTL_STYPE)) ==
466 (IEEE80211_FTYPE_CTL|IEEE80211_STYPE_PSPOLL))
Daniel Drakee85d0912006-06-02 17:11:32 +0100467 cs->control |= ZD_CS_PS_POLL_FRAME;
468
Daniel Drake459c51a2007-11-19 15:00:29 +0000469 if (flags & IEEE80211_TXCTL_USE_RTS_CTS)
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000470 cs->control |= ZD_CS_RTS;
471
Daniel Drake459c51a2007-11-19 15:00:29 +0000472 if (flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000473 cs->control |= ZD_CS_SELF_CTS;
Daniel Drakee85d0912006-06-02 17:11:32 +0100474
475 /* FIXME: Management frame? */
476}
477
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800478void zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon)
479{
480 struct zd_mac *mac = zd_hw_mac(hw);
481 u32 tmp, j = 0;
482 /* 4 more bytes for tail CRC */
483 u32 full_len = beacon->len + 4;
484 zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 0);
485 zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
486 while (tmp & 0x2) {
487 zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
488 if ((++j % 100) == 0) {
489 printk(KERN_ERR "CR_BCN_FIFO_SEMAPHORE not ready\n");
490 if (j >= 500) {
491 printk(KERN_ERR "Giving up beacon config.\n");
492 return;
493 }
494 }
495 msleep(1);
496 }
497
498 zd_iowrite32(&mac->chip, CR_BCN_FIFO, full_len - 1);
499 if (zd_chip_is_zd1211b(&mac->chip))
500 zd_iowrite32(&mac->chip, CR_BCN_LENGTH, full_len - 1);
501
502 for (j = 0 ; j < beacon->len; j++)
503 zd_iowrite32(&mac->chip, CR_BCN_FIFO,
504 *((u8 *)(beacon->data + j)));
505
506 for (j = 0; j < 4; j++)
507 zd_iowrite32(&mac->chip, CR_BCN_FIFO, 0x0);
508
509 zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 1);
510 /* 802.11b/g 2.4G CCK 1Mb
511 * 802.11a, not yet implemented, uses different values (see GPL vendor
512 * driver)
513 */
514 zd_iowrite32(&mac->chip, CR_BCN_PLCP_CFG, 0x00000400 |
515 (full_len << 19));
516}
517
Daniel Drakee85d0912006-06-02 17:11:32 +0100518static int fill_ctrlset(struct zd_mac *mac,
Daniel Drake459c51a2007-11-19 15:00:29 +0000519 struct sk_buff *skb,
520 struct ieee80211_tx_control *control)
Daniel Drakee85d0912006-06-02 17:11:32 +0100521{
522 int r;
Daniel Drake459c51a2007-11-19 15:00:29 +0000523 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
524 unsigned int frag_len = skb->len + FCS_LEN;
Daniel Drakee85d0912006-06-02 17:11:32 +0100525 unsigned int packet_length;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200526 struct ieee80211_rate *txrate;
Daniel Drakee85d0912006-06-02 17:11:32 +0100527 struct zd_ctrlset *cs = (struct zd_ctrlset *)
528 skb_push(skb, sizeof(struct zd_ctrlset));
529
Daniel Drakee85d0912006-06-02 17:11:32 +0100530 ZD_ASSERT(frag_len <= 0xffff);
Daniel Drakee85d0912006-06-02 17:11:32 +0100531
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200532 txrate = ieee80211_get_tx_rate(mac->hw, control);
533
534 cs->modulation = txrate->hw_value;
Johannes Berg8318d782008-01-24 19:38:38 +0100535 if (control->flags & IEEE80211_TXCTL_SHORT_PREAMBLE)
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200536 cs->modulation = txrate->hw_value_short;
Daniel Drakee85d0912006-06-02 17:11:32 +0100537
538 cs->tx_length = cpu_to_le16(frag_len);
539
Daniel Drake459c51a2007-11-19 15:00:29 +0000540 cs_set_control(mac, cs, hdr, control->flags);
Daniel Drakee85d0912006-06-02 17:11:32 +0100541
542 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
543 ZD_ASSERT(packet_length <= 0xffff);
544 /* ZD1211B: Computing the length difference this way, gives us
545 * flexibility to compute the packet length.
546 */
Daniel Drake74553ae2007-07-01 18:22:32 +0100547 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
Daniel Drakee85d0912006-06-02 17:11:32 +0100548 packet_length - frag_len : packet_length);
549
550 /*
551 * CURRENT LENGTH:
552 * - transmit frame length in microseconds
553 * - seems to be derived from frame length
554 * - see Cal_Us_Service() in zdinlinef.h
555 * - if macp->bTxBurstEnable is enabled, then multiply by 4
556 * - bTxBurstEnable is never set in the vendor driver
557 *
558 * SERVICE:
559 * - "for PLCP configuration"
560 * - always 0 except in some situations at 802.11b 11M
561 * - see line 53 of zdinlinef.h
562 */
563 cs->service = 0;
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100564 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
Daniel Drakee85d0912006-06-02 17:11:32 +0100565 le16_to_cpu(cs->tx_length));
566 if (r < 0)
567 return r;
568 cs->current_length = cpu_to_le16(r);
Daniel Drake459c51a2007-11-19 15:00:29 +0000569 cs->next_frame_length = 0;
Daniel Drakee85d0912006-06-02 17:11:32 +0100570
571 return 0;
572}
573
Daniel Drake459c51a2007-11-19 15:00:29 +0000574/**
575 * zd_op_tx - transmits a network frame to the device
Ulrich Kunitz741fec52006-11-22 00:05:53 +0000576 *
Daniel Drake459c51a2007-11-19 15:00:29 +0000577 * @dev: mac80211 hardware device
578 * @skb: socket buffer
579 * @control: the control structure
Daniel Drakee85d0912006-06-02 17:11:32 +0100580 *
Daniel Drake459c51a2007-11-19 15:00:29 +0000581 * This function transmit an IEEE 802.11 network frame to the device. The
582 * control block of the skbuff will be initialized. If necessary the incoming
583 * mac80211 queues will be stopped.
Daniel Drakee85d0912006-06-02 17:11:32 +0100584 */
Daniel Drake459c51a2007-11-19 15:00:29 +0000585static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
586 struct ieee80211_tx_control *control)
Daniel Drakee85d0912006-06-02 17:11:32 +0100587{
Daniel Drake459c51a2007-11-19 15:00:29 +0000588 struct zd_mac *mac = zd_hw_mac(hw);
589 int r;
Daniel Drakee85d0912006-06-02 17:11:32 +0100590
Daniel Drake459c51a2007-11-19 15:00:29 +0000591 r = fill_ctrlset(mac, skb, control);
592 if (r)
593 return r;
Daniel Drakee85d0912006-06-02 17:11:32 +0100594
Daniel Drake459c51a2007-11-19 15:00:29 +0000595 r = init_tx_skb_control_block(skb, hw, control);
596 if (r)
597 return r;
598 r = zd_usb_tx(&mac->chip.usb, skb);
599 if (r) {
600 clear_tx_skb_control_block(skb);
601 return r;
602 }
603 return 0;
604}
605
606/**
607 * filter_ack - filters incoming packets for acknowledgements
608 * @dev: the mac80211 device
609 * @rx_hdr: received header
610 * @stats: the status for the received packet
611 *
612 * This functions looks for ACK packets and tries to match them with the
613 * frames in the tx queue. If a match is found the frame will be dequeued and
614 * the upper layers is informed about the successful transmission. If
615 * mac80211 queues have been stopped and the number of frames still to be
616 * transmitted is low the queues will be opened again.
617 *
618 * Returns 1 if the frame was an ACK, 0 if it was ignored.
619 */
620static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
621 struct ieee80211_rx_status *stats)
622{
623 u16 fc = le16_to_cpu(rx_hdr->frame_control);
624 struct sk_buff *skb;
625 struct sk_buff_head *q;
626 unsigned long flags;
627
628 if ((fc & (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) !=
629 (IEEE80211_FTYPE_CTL | IEEE80211_STYPE_ACK))
630 return 0;
631
632 q = &zd_hw_mac(hw)->ack_wait_queue;
633 spin_lock_irqsave(&q->lock, flags);
634 for (skb = q->next; skb != (struct sk_buff *)q; skb = skb->next) {
635 struct ieee80211_hdr *tx_hdr;
636
637 tx_hdr = (struct ieee80211_hdr *)skb->data;
638 if (likely(!compare_ether_addr(tx_hdr->addr2, rx_hdr->addr1)))
639 {
Johannes Berg5078ed52008-02-20 12:09:25 +0100640 struct ieee80211_tx_status status;
641
642 memset(&status, 0, sizeof(status));
Daniel Drake459c51a2007-11-19 15:00:29 +0000643 status.flags = IEEE80211_TX_STATUS_ACK;
Bruno Randolf566bfe52008-05-08 19:15:40 +0200644 status.ack_signal = stats->signal;
Daniel Drake459c51a2007-11-19 15:00:29 +0000645 __skb_unlink(skb, q);
646 tx_status(hw, skb, &status, 1);
647 goto out;
648 }
649 }
650out:
651 spin_unlock_irqrestore(&q->lock, flags);
652 return 1;
653}
654
655int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
656{
657 struct zd_mac *mac = zd_hw_mac(hw);
658 struct ieee80211_rx_status stats;
659 const struct rx_status *status;
660 struct sk_buff *skb;
661 int bad_frame = 0;
Michael Buesch90817282007-12-29 17:24:23 +0100662 u16 fc;
663 bool is_qos, is_4addr, need_padding;
Johannes Berg8318d782008-01-24 19:38:38 +0100664 int i;
665 u8 rate;
Daniel Drake459c51a2007-11-19 15:00:29 +0000666
667 if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
668 FCS_LEN + sizeof(struct rx_status))
Daniel Drakee85d0912006-06-02 17:11:32 +0100669 return -EINVAL;
670
Daniel Drake459c51a2007-11-19 15:00:29 +0000671 memset(&stats, 0, sizeof(stats));
Daniel Drakee85d0912006-06-02 17:11:32 +0100672
Daniel Drake459c51a2007-11-19 15:00:29 +0000673 /* Note about pass_failed_fcs and pass_ctrl access below:
674 * mac locking intentionally omitted here, as this is the only unlocked
675 * reader and the only writer is configure_filter. Plus, if there were
676 * any races accessing these variables, it wouldn't really matter.
677 * If mac80211 ever provides a way for us to access filter flags
678 * from outside configure_filter, we could improve on this. Also, this
679 * situation may change once we implement some kind of DMA-into-skb
680 * RX path. */
Daniel Drakee85d0912006-06-02 17:11:32 +0100681
Daniel Drake459c51a2007-11-19 15:00:29 +0000682 /* Caller has to ensure that length >= sizeof(struct rx_status). */
683 status = (struct rx_status *)
Ulrich Kunitz937a0492007-10-02 18:36:53 +0100684 (buffer + (length - sizeof(struct rx_status)));
Daniel Drakee85d0912006-06-02 17:11:32 +0100685 if (status->frame_status & ZD_RX_ERROR) {
Daniel Drake459c51a2007-11-19 15:00:29 +0000686 if (mac->pass_failed_fcs &&
687 (status->frame_status & ZD_RX_CRC32_ERROR)) {
688 stats.flag |= RX_FLAG_FAILED_FCS_CRC;
689 bad_frame = 1;
690 } else {
691 return -EINVAL;
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000692 }
Daniel Drakee85d0912006-06-02 17:11:32 +0100693 }
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000694
Johannes Berg8318d782008-01-24 19:38:38 +0100695 stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
696 stats.band = IEEE80211_BAND_2GHZ;
Bruno Randolf566bfe52008-05-08 19:15:40 +0200697 stats.signal = status->signal_strength;
698 stats.qual = zd_rx_qual_percent(buffer,
Daniel Drakee85d0912006-06-02 17:11:32 +0100699 length - sizeof(struct rx_status),
700 status);
Johannes Berg8318d782008-01-24 19:38:38 +0100701
702 rate = zd_rx_rate(buffer, status);
703
704 /* todo: return index in the big switches in zd_rx_rate instead */
705 for (i = 0; i < mac->band.n_bitrates; i++)
706 if (rate == mac->band.bitrates[i].hw_value)
707 stats.rate_idx = i;
Daniel Drakee85d0912006-06-02 17:11:32 +0100708
Daniel Drake459c51a2007-11-19 15:00:29 +0000709 length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
710 buffer += ZD_PLCP_HEADER_SIZE;
Daniel Drakee85d0912006-06-02 17:11:32 +0100711
Daniel Drake459c51a2007-11-19 15:00:29 +0000712 /* Except for bad frames, filter each frame to see if it is an ACK, in
713 * which case our internal TX tracking is updated. Normally we then
714 * bail here as there's no need to pass ACKs on up to the stack, but
715 * there is also the case where the stack has requested us to pass
716 * control frames on up (pass_ctrl) which we must consider. */
717 if (!bad_frame &&
718 filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
719 && !mac->pass_ctrl)
720 return 0;
Daniel Drakee85d0912006-06-02 17:11:32 +0100721
Michael Buesch90817282007-12-29 17:24:23 +0100722 fc = le16_to_cpu(*((__le16 *) buffer));
723
724 is_qos = ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
725 ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_QOS_DATA);
726 is_4addr = (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
727 (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS);
728 need_padding = is_qos ^ is_4addr;
729
730 skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
Daniel Drake459c51a2007-11-19 15:00:29 +0000731 if (skb == NULL)
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800732 return -ENOMEM;
Michael Buesch90817282007-12-29 17:24:23 +0100733 if (need_padding) {
734 /* Make sure the the payload data is 4 byte aligned. */
735 skb_reserve(skb, 2);
736 }
737
Daniel Drake459c51a2007-11-19 15:00:29 +0000738 memcpy(skb_put(skb, length), buffer, length);
739
740 ieee80211_rx_irqsafe(hw, skb, &stats);
Daniel Drakee85d0912006-06-02 17:11:32 +0100741 return 0;
742}
743
Daniel Drake459c51a2007-11-19 15:00:29 +0000744static int zd_op_add_interface(struct ieee80211_hw *hw,
745 struct ieee80211_if_init_conf *conf)
Daniel Drakee85d0912006-06-02 17:11:32 +0100746{
Daniel Drake459c51a2007-11-19 15:00:29 +0000747 struct zd_mac *mac = zd_hw_mac(hw);
748
749 /* using IEEE80211_IF_TYPE_INVALID to indicate no mode selected */
750 if (mac->type != IEEE80211_IF_TYPE_INVALID)
751 return -EOPNOTSUPP;
752
753 switch (conf->type) {
754 case IEEE80211_IF_TYPE_MNTR:
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800755 case IEEE80211_IF_TYPE_MESH_POINT:
Daniel Drake459c51a2007-11-19 15:00:29 +0000756 case IEEE80211_IF_TYPE_STA:
Bruno Randolf84e6dc92008-05-07 11:46:02 +0200757 case IEEE80211_IF_TYPE_IBSS:
Daniel Drake459c51a2007-11-19 15:00:29 +0000758 mac->type = conf->type;
759 break;
760 default:
761 return -EOPNOTSUPP;
762 }
763
764 return zd_write_mac_addr(&mac->chip, conf->mac_addr);
Daniel Drakee85d0912006-06-02 17:11:32 +0100765}
766
Daniel Drake459c51a2007-11-19 15:00:29 +0000767static void zd_op_remove_interface(struct ieee80211_hw *hw,
768 struct ieee80211_if_init_conf *conf)
Daniel Drakee85d0912006-06-02 17:11:32 +0100769{
Daniel Drake459c51a2007-11-19 15:00:29 +0000770 struct zd_mac *mac = zd_hw_mac(hw);
771 mac->type = IEEE80211_IF_TYPE_INVALID;
772 zd_write_mac_addr(&mac->chip, NULL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100773}
774
Daniel Drake459c51a2007-11-19 15:00:29 +0000775static int zd_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Daniel Drakee85d0912006-06-02 17:11:32 +0100776{
Daniel Drake459c51a2007-11-19 15:00:29 +0000777 struct zd_mac *mac = zd_hw_mac(hw);
Johannes Berg8318d782008-01-24 19:38:38 +0100778 return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
Daniel Drakee85d0912006-06-02 17:11:32 +0100779}
780
Johannes Berg32bfd352007-12-19 01:31:26 +0100781static int zd_op_config_interface(struct ieee80211_hw *hw,
782 struct ieee80211_vif *vif,
Daniel Drake459c51a2007-11-19 15:00:29 +0000783 struct ieee80211_if_conf *conf)
Daniel Drakee85d0912006-06-02 17:11:32 +0100784{
Daniel Drake459c51a2007-11-19 15:00:29 +0000785 struct zd_mac *mac = zd_hw_mac(hw);
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800786 int associated;
787
Bruno Randolf84e6dc92008-05-07 11:46:02 +0200788 if (mac->type == IEEE80211_IF_TYPE_MESH_POINT ||
789 mac->type == IEEE80211_IF_TYPE_IBSS) {
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800790 associated = true;
791 if (conf->beacon) {
792 zd_mac_config_beacon(hw, conf->beacon);
793 kfree_skb(conf->beacon);
794 zd_set_beacon_interval(&mac->chip, BCN_MODE_IBSS |
795 hw->conf.beacon_int);
796 }
797 } else
798 associated = is_valid_ether_addr(conf->bssid);
Daniel Drakee85d0912006-06-02 17:11:32 +0100799
Daniel Drakee85d0912006-06-02 17:11:32 +0100800 spin_lock_irq(&mac->lock);
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800801 mac->associated = associated;
Daniel Drakee85d0912006-06-02 17:11:32 +0100802 spin_unlock_irq(&mac->lock);
Daniel Drake459c51a2007-11-19 15:00:29 +0000803
804 /* TODO: do hardware bssid filtering */
805 return 0;
806}
807
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -0800808void zd_process_intr(struct work_struct *work)
809{
810 u16 int_status;
811 struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
812
813 int_status = le16_to_cpu(*(u16 *)(mac->intr_buffer+4));
814 if (int_status & INT_CFG_NEXT_BCN) {
815 if (net_ratelimit())
816 dev_dbg_f(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");
817 } else
818 dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
819
820 zd_chip_enable_hwint(&mac->chip);
821}
822
823
Daniel Drake459c51a2007-11-19 15:00:29 +0000824static void set_multicast_hash_handler(struct work_struct *work)
825{
826 struct zd_mac *mac =
827 container_of(work, struct zd_mac, set_multicast_hash_work);
828 struct zd_mc_hash hash;
829
830 spin_lock_irq(&mac->lock);
831 hash = mac->multicast_hash;
832 spin_unlock_irq(&mac->lock);
833
834 zd_chip_set_multicast_hash(&mac->chip, &hash);
835}
836
837static void set_rx_filter_handler(struct work_struct *work)
838{
839 struct zd_mac *mac =
840 container_of(work, struct zd_mac, set_rx_filter_work);
841 int r;
842
843 dev_dbg_f(zd_mac_dev(mac), "\n");
844 r = set_rx_filter(mac);
845 if (r)
846 dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r);
847}
848
849#define SUPPORTED_FIF_FLAGS \
850 (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
Michael Buesch2c1a1b12008-02-10 16:03:55 +0100851 FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
Daniel Drake459c51a2007-11-19 15:00:29 +0000852static void zd_op_configure_filter(struct ieee80211_hw *hw,
853 unsigned int changed_flags,
854 unsigned int *new_flags,
855 int mc_count, struct dev_mc_list *mclist)
856{
857 struct zd_mc_hash hash;
858 struct zd_mac *mac = zd_hw_mac(hw);
859 unsigned long flags;
860 int i;
861
862 /* Only deal with supported flags */
863 changed_flags &= SUPPORTED_FIF_FLAGS;
864 *new_flags &= SUPPORTED_FIF_FLAGS;
865
866 /* changed_flags is always populated but this driver
867 * doesn't support all FIF flags so its possible we don't
868 * need to do anything */
869 if (!changed_flags)
870 return;
871
872 if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI)) {
873 zd_mc_add_all(&hash);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +0100874 } else {
Daniel Drake459c51a2007-11-19 15:00:29 +0000875 DECLARE_MAC_BUF(macbuf);
876
877 zd_mc_clear(&hash);
878 for (i = 0; i < mc_count; i++) {
879 if (!mclist)
880 break;
881 dev_dbg_f(zd_mac_dev(mac), "mc addr %s\n",
882 print_mac(macbuf, mclist->dmi_addr));
883 zd_mc_add_addr(&hash, mclist->dmi_addr);
884 mclist = mclist->next;
885 }
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +0100886 }
Daniel Drake459c51a2007-11-19 15:00:29 +0000887
888 spin_lock_irqsave(&mac->lock, flags);
889 mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
890 mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
891 mac->multicast_hash = hash;
892 spin_unlock_irqrestore(&mac->lock, flags);
893 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
894
895 if (changed_flags & FIF_CONTROL)
896 queue_work(zd_workqueue, &mac->set_rx_filter_work);
897
898 /* no handling required for FIF_OTHER_BSS as we don't currently
899 * do BSSID filtering */
900 /* FIXME: in future it would be nice to enable the probe response
901 * filter (so that the driver doesn't see them) until
902 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
903 * have to schedule work to enable prbresp reception, which might
904 * happen too late. For now we'll just listen and forward them all the
905 * time. */
906}
907
908static void set_rts_cts_work(struct work_struct *work)
909{
910 struct zd_mac *mac =
911 container_of(work, struct zd_mac, set_rts_cts_work);
912 unsigned long flags;
913 unsigned int short_preamble;
914
915 mutex_lock(&mac->chip.mutex);
916
917 spin_lock_irqsave(&mac->lock, flags);
918 mac->updating_rts_rate = 0;
919 short_preamble = mac->short_preamble;
920 spin_unlock_irqrestore(&mac->lock, flags);
921
922 zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
923 mutex_unlock(&mac->chip.mutex);
924}
925
Johannes Berg471b3ef2007-12-28 14:32:58 +0100926static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
927 struct ieee80211_vif *vif,
928 struct ieee80211_bss_conf *bss_conf,
929 u32 changes)
Daniel Drake459c51a2007-11-19 15:00:29 +0000930{
931 struct zd_mac *mac = zd_hw_mac(hw);
932 unsigned long flags;
933
934 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
935
Johannes Berg471b3ef2007-12-28 14:32:58 +0100936 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Daniel Drake459c51a2007-11-19 15:00:29 +0000937 spin_lock_irqsave(&mac->lock, flags);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100938 mac->short_preamble = bss_conf->use_short_preamble;
Daniel Drake459c51a2007-11-19 15:00:29 +0000939 if (!mac->updating_rts_rate) {
940 mac->updating_rts_rate = 1;
941 /* FIXME: should disable TX here, until work has
942 * completed and RTS_CTS reg is updated */
943 queue_work(zd_workqueue, &mac->set_rts_cts_work);
944 }
945 spin_unlock_irqrestore(&mac->lock, flags);
946 }
947}
948
Bruno Randolf84e6dc92008-05-07 11:46:02 +0200949static int zd_op_beacon_update(struct ieee80211_hw *hw,
950 struct sk_buff *skb,
951 struct ieee80211_tx_control *ctl)
952{
953 struct zd_mac *mac = zd_hw_mac(hw);
954 zd_mac_config_beacon(hw, skb);
955 kfree_skb(skb);
956 zd_set_beacon_interval(&mac->chip, BCN_MODE_IBSS |
957 hw->conf.beacon_int);
958 return 0;
959}
960
Daniel Drake459c51a2007-11-19 15:00:29 +0000961static const struct ieee80211_ops zd_ops = {
962 .tx = zd_op_tx,
963 .start = zd_op_start,
964 .stop = zd_op_stop,
965 .add_interface = zd_op_add_interface,
966 .remove_interface = zd_op_remove_interface,
967 .config = zd_op_config,
968 .config_interface = zd_op_config_interface,
969 .configure_filter = zd_op_configure_filter,
Johannes Berg471b3ef2007-12-28 14:32:58 +0100970 .bss_info_changed = zd_op_bss_info_changed,
Bruno Randolf84e6dc92008-05-07 11:46:02 +0200971 .beacon_update = zd_op_beacon_update,
Daniel Drake459c51a2007-11-19 15:00:29 +0000972};
973
974struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
975{
976 struct zd_mac *mac;
977 struct ieee80211_hw *hw;
Daniel Drake459c51a2007-11-19 15:00:29 +0000978
979 hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
980 if (!hw) {
981 dev_dbg_f(&intf->dev, "out of memory\n");
982 return NULL;
983 }
984
985 mac = zd_hw_mac(hw);
986
987 memset(mac, 0, sizeof(*mac));
988 spin_lock_init(&mac->lock);
989 mac->hw = hw;
990
991 mac->type = IEEE80211_IF_TYPE_INVALID;
992
993 memcpy(mac->channels, zd_channels, sizeof(zd_channels));
994 memcpy(mac->rates, zd_rates, sizeof(zd_rates));
Johannes Berg8318d782008-01-24 19:38:38 +0100995 mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
996 mac->band.bitrates = mac->rates;
997 mac->band.n_channels = ARRAY_SIZE(zd_channels);
998 mac->band.channels = mac->channels;
Daniel Drake459c51a2007-11-19 15:00:29 +0000999
Johannes Berg8318d782008-01-24 19:38:38 +01001000 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
1001
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -08001002 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
Bruno Randolf566bfe52008-05-08 19:15:40 +02001003 IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE |
1004 IEEE80211_HW_SIGNAL_DB;
Daniel Drake459c51a2007-11-19 15:00:29 +00001005
Bruno Randolf566bfe52008-05-08 19:15:40 +02001006 hw->max_signal = 100;
Daniel Drake459c51a2007-11-19 15:00:29 +00001007 hw->queues = 1;
1008 hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
1009
1010 skb_queue_head_init(&mac->ack_wait_queue);
1011
Daniel Drake459c51a2007-11-19 15:00:29 +00001012 zd_chip_init(&mac->chip, hw, intf);
1013 housekeeping_init(mac);
1014 INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
1015 INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
1016 INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler);
Luis Carlos Cobo72e77a82008-03-03 12:32:15 -08001017 INIT_WORK(&mac->process_intr, zd_process_intr);
Daniel Drake459c51a2007-11-19 15:00:29 +00001018
1019 SET_IEEE80211_DEV(hw, &intf->dev);
1020 return hw;
Daniel Drakee85d0912006-06-02 17:11:32 +01001021}
1022
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001023#define LINK_LED_WORK_DELAY HZ
1024
David Howellsc4028952006-11-22 14:57:56 +00001025static void link_led_handler(struct work_struct *work)
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001026{
David Howellsc4028952006-11-22 14:57:56 +00001027 struct zd_mac *mac =
1028 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001029 struct zd_chip *chip = &mac->chip;
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001030 int is_associated;
1031 int r;
1032
1033 spin_lock_irq(&mac->lock);
Daniel Drake459c51a2007-11-19 15:00:29 +00001034 is_associated = mac->associated;
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001035 spin_unlock_irq(&mac->lock);
1036
1037 r = zd_chip_control_leds(chip,
1038 is_associated ? LED_ASSOCIATED : LED_SCANNING);
1039 if (r)
Daniel Drake459c51a2007-11-19 15:00:29 +00001040 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001041
1042 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1043 LINK_LED_WORK_DELAY);
1044}
1045
1046static void housekeeping_init(struct zd_mac *mac)
1047{
David Howellsc4028952006-11-22 14:57:56 +00001048 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001049}
1050
1051static void housekeeping_enable(struct zd_mac *mac)
1052{
1053 dev_dbg_f(zd_mac_dev(mac), "\n");
1054 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1055 0);
1056}
1057
1058static void housekeeping_disable(struct zd_mac *mac)
1059{
1060 dev_dbg_f(zd_mac_dev(mac), "\n");
1061 cancel_rearming_delayed_workqueue(zd_workqueue,
1062 &mac->housekeeping.link_led_work);
1063 zd_chip_control_leds(&mac->chip, LED_OFF);
1064}