blob: aaffd081b1bf884dacf523c29a6e7589711cdb52 [file] [log] [blame]
Daniel Drakee85d0912006-06-02 17:11:32 +01001/* zd_mac.c
2 *
Daniel Drake459c51a2007-11-19 15:00:29 +00003 * Copyright (c) 2007 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
4 *
Daniel Drakee85d0912006-06-02 17:11:32 +01005 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/netdevice.h>
21#include <linux/etherdevice.h>
Daniel Drakee85d0912006-06-02 17:11:32 +010022#include <linux/usb.h>
23#include <linux/jiffies.h>
24#include <net/ieee80211_radiotap.h>
25
26#include "zd_def.h"
27#include "zd_chip.h"
28#include "zd_mac.h"
29#include "zd_ieee80211.h"
Daniel Drakee85d0912006-06-02 17:11:32 +010030#include "zd_rf.h"
Daniel Drakee85d0912006-06-02 17:11:32 +010031
Daniel Drake459c51a2007-11-19 15:00:29 +000032/* This table contains the hardware specific values for the modulation rates. */
33static const struct ieee80211_rate zd_rates[] = {
34 { .rate = 10,
35 .val = ZD_CCK_RATE_1M,
36 .flags = IEEE80211_RATE_CCK },
37 { .rate = 20,
38 .val = ZD_CCK_RATE_2M,
39 .val2 = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
40 .flags = IEEE80211_RATE_CCK_2 },
41 { .rate = 55,
42 .val = ZD_CCK_RATE_5_5M,
43 .val2 = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
44 .flags = IEEE80211_RATE_CCK_2 },
45 { .rate = 110,
46 .val = ZD_CCK_RATE_11M,
47 .val2 = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
48 .flags = IEEE80211_RATE_CCK_2 },
49 { .rate = 60,
50 .val = ZD_OFDM_RATE_6M,
51 .flags = IEEE80211_RATE_OFDM },
52 { .rate = 90,
53 .val = ZD_OFDM_RATE_9M,
54 .flags = IEEE80211_RATE_OFDM },
55 { .rate = 120,
56 .val = ZD_OFDM_RATE_12M,
57 .flags = IEEE80211_RATE_OFDM },
58 { .rate = 180,
59 .val = ZD_OFDM_RATE_18M,
60 .flags = IEEE80211_RATE_OFDM },
61 { .rate = 240,
62 .val = ZD_OFDM_RATE_24M,
63 .flags = IEEE80211_RATE_OFDM },
64 { .rate = 360,
65 .val = ZD_OFDM_RATE_36M,
66 .flags = IEEE80211_RATE_OFDM },
67 { .rate = 480,
68 .val = ZD_OFDM_RATE_48M,
69 .flags = IEEE80211_RATE_OFDM },
70 { .rate = 540,
71 .val = ZD_OFDM_RATE_54M,
72 .flags = IEEE80211_RATE_OFDM },
73};
74
75static const struct ieee80211_channel zd_channels[] = {
76 { .chan = 1,
77 .freq = 2412},
78 { .chan = 2,
79 .freq = 2417},
80 { .chan = 3,
81 .freq = 2422},
82 { .chan = 4,
83 .freq = 2427},
84 { .chan = 5,
85 .freq = 2432},
86 { .chan = 6,
87 .freq = 2437},
88 { .chan = 7,
89 .freq = 2442},
90 { .chan = 8,
91 .freq = 2447},
92 { .chan = 9,
93 .freq = 2452},
94 { .chan = 10,
95 .freq = 2457},
96 { .chan = 11,
97 .freq = 2462},
98 { .chan = 12,
99 .freq = 2467},
100 { .chan = 13,
101 .freq = 2472},
102 { .chan = 14,
103 .freq = 2484}
104};
Daniel Drakee85d0912006-06-02 17:11:32 +0100105
Ulrich Kunitz583afd12006-09-13 02:42:38 +0100106static void housekeeping_init(struct zd_mac *mac);
107static void housekeeping_enable(struct zd_mac *mac);
108static void housekeeping_disable(struct zd_mac *mac);
109
Daniel Drake459c51a2007-11-19 15:00:29 +0000110int zd_mac_preinit_hw(struct ieee80211_hw *hw)
Daniel Drake74553ae2007-07-01 18:22:32 +0100111{
112 int r;
113 u8 addr[ETH_ALEN];
Daniel Drake459c51a2007-11-19 15:00:29 +0000114 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drake74553ae2007-07-01 18:22:32 +0100115
116 r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
117 if (r)
118 return r;
119
Daniel Drake459c51a2007-11-19 15:00:29 +0000120 SET_IEEE80211_PERM_ADDR(hw, addr);
121
Daniel Drake74553ae2007-07-01 18:22:32 +0100122 return 0;
123}
124
Daniel Drake459c51a2007-11-19 15:00:29 +0000125int zd_mac_init_hw(struct ieee80211_hw *hw)
Daniel Drakee85d0912006-06-02 17:11:32 +0100126{
127 int r;
Daniel Drake459c51a2007-11-19 15:00:29 +0000128 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100129 struct zd_chip *chip = &mac->chip;
Daniel Drakee85d0912006-06-02 17:11:32 +0100130 u8 default_regdomain;
131
132 r = zd_chip_enable_int(chip);
133 if (r)
134 goto out;
Daniel Drake74553ae2007-07-01 18:22:32 +0100135 r = zd_chip_init_hw(chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100136 if (r)
137 goto disable_int;
138
Daniel Drakee85d0912006-06-02 17:11:32 +0100139 ZD_ASSERT(!irqs_disabled());
Daniel Drakee85d0912006-06-02 17:11:32 +0100140
141 r = zd_read_regdomain(chip, &default_regdomain);
142 if (r)
143 goto disable_int;
Daniel Drakee85d0912006-06-02 17:11:32 +0100144 spin_lock_irq(&mac->lock);
145 mac->regdomain = mac->default_regdomain = default_regdomain;
146 spin_unlock_irq(&mac->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +0100147
Daniel Drake40da08b2006-08-01 23:43:32 +0200148 /* We must inform the device that we are doing encryption/decryption in
149 * software at the moment. */
150 r = zd_set_encryption_type(chip, ENC_SNIFFER);
Daniel Drakee85d0912006-06-02 17:11:32 +0100151 if (r)
152 goto disable_int;
153
Daniel Drake459c51a2007-11-19 15:00:29 +0000154 zd_geo_init(hw, mac->regdomain);
Daniel Drakee85d0912006-06-02 17:11:32 +0100155
156 r = 0;
157disable_int:
158 zd_chip_disable_int(chip);
159out:
160 return r;
161}
162
163void zd_mac_clear(struct zd_mac *mac)
164{
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000165 flush_workqueue(zd_workqueue);
Daniel Drakee85d0912006-06-02 17:11:32 +0100166 zd_chip_clear(&mac->chip);
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100167 ZD_ASSERT(!spin_is_locked(&mac->lock));
168 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
Daniel Drakee85d0912006-06-02 17:11:32 +0100169}
170
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100171static int set_rx_filter(struct zd_mac *mac)
Daniel Drakee85d0912006-06-02 17:11:32 +0100172{
Daniel Drake459c51a2007-11-19 15:00:29 +0000173 unsigned long flags;
174 u32 filter = STA_RX_FILTER;
Daniel Drakee85d0912006-06-02 17:11:32 +0100175
Daniel Drake459c51a2007-11-19 15:00:29 +0000176 spin_lock_irqsave(&mac->lock, flags);
177 if (mac->pass_ctrl)
178 filter |= RX_FILTER_CTRL;
179 spin_unlock_irqrestore(&mac->lock, flags);
180
181 return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100182}
183
184static int set_mc_hash(struct zd_mac *mac)
185{
186 struct zd_mc_hash hash;
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100187 zd_mc_clear(&hash);
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100188 return zd_chip_set_multicast_hash(&mac->chip, &hash);
189}
190
Daniel Drake459c51a2007-11-19 15:00:29 +0000191static int zd_op_start(struct ieee80211_hw *hw)
Daniel Drakee85d0912006-06-02 17:11:32 +0100192{
Daniel Drake459c51a2007-11-19 15:00:29 +0000193 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100194 struct zd_chip *chip = &mac->chip;
Daniel Drake74553ae2007-07-01 18:22:32 +0100195 struct zd_usb *usb = &chip->usb;
Daniel Drakee85d0912006-06-02 17:11:32 +0100196 int r;
197
Daniel Drake74553ae2007-07-01 18:22:32 +0100198 if (!usb->initialized) {
199 r = zd_usb_init_hw(usb);
200 if (r)
201 goto out;
202 }
203
Daniel Drakee85d0912006-06-02 17:11:32 +0100204 r = zd_chip_enable_int(chip);
205 if (r < 0)
206 goto out;
207
208 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
209 if (r < 0)
210 goto disable_int;
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100211 r = set_rx_filter(mac);
212 if (r)
213 goto disable_int;
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100214 r = set_mc_hash(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100215 if (r)
216 goto disable_int;
217 r = zd_chip_switch_radio_on(chip);
218 if (r < 0)
219 goto disable_int;
Daniel Drake459c51a2007-11-19 15:00:29 +0000220 r = zd_chip_enable_rxtx(chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100221 if (r < 0)
222 goto disable_radio;
223 r = zd_chip_enable_hwint(chip);
224 if (r < 0)
Daniel Drake459c51a2007-11-19 15:00:29 +0000225 goto disable_rxtx;
Daniel Drakee85d0912006-06-02 17:11:32 +0100226
Ulrich Kunitz583afd12006-09-13 02:42:38 +0100227 housekeeping_enable(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100228 return 0;
Daniel Drake459c51a2007-11-19 15:00:29 +0000229disable_rxtx:
230 zd_chip_disable_rxtx(chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100231disable_radio:
232 zd_chip_switch_radio_off(chip);
233disable_int:
234 zd_chip_disable_int(chip);
235out:
236 return r;
237}
238
Daniel Drake459c51a2007-11-19 15:00:29 +0000239/**
240 * clear_tx_skb_control_block - clears the control block of tx skbuffs
241 * @skb: a &struct sk_buff pointer
242 *
243 * This clears the control block of skbuff buffers, which were transmitted to
244 * the device. Notify that the function is not thread-safe, so prevent
245 * multiple calls.
246 */
247static void clear_tx_skb_control_block(struct sk_buff *skb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100248{
Daniel Drake459c51a2007-11-19 15:00:29 +0000249 struct zd_tx_skb_control_block *cb =
250 (struct zd_tx_skb_control_block *)skb->cb;
251
252 kfree(cb->control);
253 cb->control = NULL;
254}
255
256/**
257 * kfree_tx_skb - frees a tx skbuff
258 * @skb: a &struct sk_buff pointer
259 *
260 * Frees the tx skbuff. Frees also the allocated control structure in the
261 * control block if necessary.
262 */
263static void kfree_tx_skb(struct sk_buff *skb)
264{
265 clear_tx_skb_control_block(skb);
266 dev_kfree_skb_any(skb);
267}
268
269static void zd_op_stop(struct ieee80211_hw *hw)
270{
271 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100272 struct zd_chip *chip = &mac->chip;
Daniel Drake459c51a2007-11-19 15:00:29 +0000273 struct sk_buff *skb;
274 struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
Daniel Drakee85d0912006-06-02 17:11:32 +0100275
Daniel Drake459c51a2007-11-19 15:00:29 +0000276 /* The order here deliberately is a little different from the open()
Daniel Drakee85d0912006-06-02 17:11:32 +0100277 * method, since we need to make sure there is no opportunity for RX
Daniel Drake459c51a2007-11-19 15:00:29 +0000278 * frames to be processed by mac80211 after we have stopped it.
Daniel Drakee85d0912006-06-02 17:11:32 +0100279 */
280
Daniel Drake459c51a2007-11-19 15:00:29 +0000281 zd_chip_disable_rxtx(chip);
Ulrich Kunitz583afd12006-09-13 02:42:38 +0100282 housekeeping_disable(mac);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000283 flush_workqueue(zd_workqueue);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000284
Daniel Drakee85d0912006-06-02 17:11:32 +0100285 zd_chip_disable_hwint(chip);
286 zd_chip_switch_radio_off(chip);
287 zd_chip_disable_int(chip);
288
Daniel Drake459c51a2007-11-19 15:00:29 +0000289
290 while ((skb = skb_dequeue(ack_wait_queue)))
291 kfree_tx_skb(skb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100292}
293
Daniel Drake459c51a2007-11-19 15:00:29 +0000294/**
295 * init_tx_skb_control_block - initializes skb control block
296 * @skb: a &sk_buff pointer
297 * @dev: pointer to the mac80221 device
298 * @control: mac80211 tx control applying for the frame in @skb
299 *
300 * Initializes the control block of the skbuff to be transmitted.
301 */
302static int init_tx_skb_control_block(struct sk_buff *skb,
303 struct ieee80211_hw *hw,
304 struct ieee80211_tx_control *control)
Daniel Drakee85d0912006-06-02 17:11:32 +0100305{
Daniel Drake459c51a2007-11-19 15:00:29 +0000306 struct zd_tx_skb_control_block *cb =
307 (struct zd_tx_skb_control_block *)skb->cb;
Daniel Drakee85d0912006-06-02 17:11:32 +0100308
Daniel Drake459c51a2007-11-19 15:00:29 +0000309 ZD_ASSERT(sizeof(*cb) <= sizeof(skb->cb));
310 memset(cb, 0, sizeof(*cb));
311 cb->hw= hw;
312 cb->control = kmalloc(sizeof(*control), GFP_ATOMIC);
313 if (cb->control == NULL)
314 return -ENOMEM;
315 memcpy(cb->control, control, sizeof(*control));
Daniel Drakee85d0912006-06-02 17:11:32 +0100316
317 return 0;
318}
319
Daniel Drake459c51a2007-11-19 15:00:29 +0000320/**
321 * tx_status - reports tx status of a packet if required
322 * @hw - a &struct ieee80211_hw pointer
323 * @skb - a sk-buffer
324 * @status - the tx status of the packet without control information
325 * @success - True for successfull transmission of the frame
326 *
327 * This information calls ieee80211_tx_status_irqsafe() if required by the
328 * control information. It copies the control information into the status
329 * information.
330 *
331 * If no status information has been requested, the skb is freed.
332 */
333static void tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
334 struct ieee80211_tx_status *status,
335 bool success)
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000336{
Daniel Drake459c51a2007-11-19 15:00:29 +0000337 struct zd_tx_skb_control_block *cb = (struct zd_tx_skb_control_block *)
338 skb->cb;
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000339
Daniel Drake459c51a2007-11-19 15:00:29 +0000340 ZD_ASSERT(cb->control != NULL);
341 memcpy(&status->control, cb->control, sizeof(status->control));
342 if (!success)
343 status->excessive_retries = 1;
344 clear_tx_skb_control_block(skb);
345 ieee80211_tx_status_irqsafe(hw, skb, status);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000346}
347
Daniel Drake459c51a2007-11-19 15:00:29 +0000348/**
349 * zd_mac_tx_failed - callback for failed frames
350 * @dev: the mac80211 wireless device
351 *
352 * This function is called if a frame couldn't be succesfully be
353 * transferred. The first frame from the tx queue, will be selected and
354 * reported as error to the upper layers.
355 */
356void zd_mac_tx_failed(struct ieee80211_hw *hw)
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000357{
Daniel Drake459c51a2007-11-19 15:00:29 +0000358 struct sk_buff_head *q = &zd_hw_mac(hw)->ack_wait_queue;
359 struct sk_buff *skb;
360 struct ieee80211_tx_status status = {{0}};
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000361
Daniel Drake459c51a2007-11-19 15:00:29 +0000362 skb = skb_dequeue(q);
363 if (skb == NULL)
364 return;
365 tx_status(hw, skb, &status, 0);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000366}
367
Daniel Drake459c51a2007-11-19 15:00:29 +0000368/**
369 * zd_mac_tx_to_dev - callback for USB layer
370 * @skb: a &sk_buff pointer
371 * @error: error value, 0 if transmission successful
372 *
373 * Informs the MAC layer that the frame has successfully transferred to the
374 * device. If an ACK is required and the transfer to the device has been
375 * successful, the packets are put on the @ack_wait_queue with
376 * the control set removed.
377 */
378void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
Daniel Drakee85d0912006-06-02 17:11:32 +0100379{
Daniel Drake459c51a2007-11-19 15:00:29 +0000380 struct zd_tx_skb_control_block *cb =
381 (struct zd_tx_skb_control_block *)skb->cb;
382 struct ieee80211_hw *hw = cb->hw;
Daniel Drakee85d0912006-06-02 17:11:32 +0100383
Daniel Drake459c51a2007-11-19 15:00:29 +0000384 if (likely(cb->control)) {
385 skb_pull(skb, sizeof(struct zd_ctrlset));
386 if (unlikely(error ||
387 (cb->control->flags & IEEE80211_TXCTL_NO_ACK)))
388 {
389 struct ieee80211_tx_status status = {{0}};
390 tx_status(hw, skb, &status, !error);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000391 } else {
Daniel Drake459c51a2007-11-19 15:00:29 +0000392 struct sk_buff_head *q =
393 &zd_hw_mac(hw)->ack_wait_queue;
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000394
Daniel Drake459c51a2007-11-19 15:00:29 +0000395 skb_queue_tail(q, skb);
396 while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS)
397 zd_mac_tx_failed(hw);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000398 }
Daniel Drake459c51a2007-11-19 15:00:29 +0000399 } else {
400 kfree_tx_skb(skb);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000401 }
Daniel Drakee85d0912006-06-02 17:11:32 +0100402}
403
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000404static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
Daniel Drakee85d0912006-06-02 17:11:32 +0100405{
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100406 /* ZD_PURE_RATE() must be used to remove the modulation type flag of
Daniel Drake459c51a2007-11-19 15:00:29 +0000407 * the zd-rate values.
408 */
Daniel Drakee85d0912006-06-02 17:11:32 +0100409 static const u8 rate_divisor[] = {
Daniel Drake459c51a2007-11-19 15:00:29 +0000410 [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
411 [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
412 /* Bits must be doubled. */
413 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
414 [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
415 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
416 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
417 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
418 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
419 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
420 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
421 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
422 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
Daniel Drakee85d0912006-06-02 17:11:32 +0100423 };
424
425 u32 bits = (u32)tx_length * 8;
426 u32 divisor;
427
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100428 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
Daniel Drakee85d0912006-06-02 17:11:32 +0100429 if (divisor == 0)
430 return -EINVAL;
431
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000432 switch (zd_rate) {
433 case ZD_CCK_RATE_5_5M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100434 bits = (2*bits) + 10; /* round up to the next integer */
435 break;
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000436 case ZD_CCK_RATE_11M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100437 if (service) {
438 u32 t = bits % 11;
439 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
440 if (0 < t && t <= 3) {
441 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
442 }
443 }
444 bits += 10; /* round up to the next integer */
445 break;
446 }
447
448 return bits/divisor;
449}
450
Daniel Drakee85d0912006-06-02 17:11:32 +0100451static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
Daniel Drake459c51a2007-11-19 15:00:29 +0000452 struct ieee80211_hdr *header, u32 flags)
Daniel Drakee85d0912006-06-02 17:11:32 +0100453{
Daniel Drake459c51a2007-11-19 15:00:29 +0000454 u16 fctl = le16_to_cpu(header->frame_control);
Daniel Drakee85d0912006-06-02 17:11:32 +0100455
456 /*
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000457 * CONTROL TODO:
Daniel Drakee85d0912006-06-02 17:11:32 +0100458 * - if backoff needed, enable bit 0
459 * - if burst (backoff not needed) disable bit 0
Daniel Drakee85d0912006-06-02 17:11:32 +0100460 */
461
462 cs->control = 0;
463
464 /* First fragment */
Daniel Drake459c51a2007-11-19 15:00:29 +0000465 if (flags & IEEE80211_TXCTL_FIRST_FRAGMENT)
Daniel Drakee85d0912006-06-02 17:11:32 +0100466 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
467
468 /* Multicast */
469 if (is_multicast_ether_addr(header->addr1))
470 cs->control |= ZD_CS_MULTICAST;
471
472 /* PS-POLL */
Daniel Drake459c51a2007-11-19 15:00:29 +0000473 if ((fctl & (IEEE80211_FCTL_FTYPE|IEEE80211_FCTL_STYPE)) ==
474 (IEEE80211_FTYPE_CTL|IEEE80211_STYPE_PSPOLL))
Daniel Drakee85d0912006-06-02 17:11:32 +0100475 cs->control |= ZD_CS_PS_POLL_FRAME;
476
Daniel Drake459c51a2007-11-19 15:00:29 +0000477 if (flags & IEEE80211_TXCTL_USE_RTS_CTS)
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000478 cs->control |= ZD_CS_RTS;
479
Daniel Drake459c51a2007-11-19 15:00:29 +0000480 if (flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000481 cs->control |= ZD_CS_SELF_CTS;
Daniel Drakee85d0912006-06-02 17:11:32 +0100482
483 /* FIXME: Management frame? */
484}
485
486static int fill_ctrlset(struct zd_mac *mac,
Daniel Drake459c51a2007-11-19 15:00:29 +0000487 struct sk_buff *skb,
488 struct ieee80211_tx_control *control)
Daniel Drakee85d0912006-06-02 17:11:32 +0100489{
490 int r;
Daniel Drake459c51a2007-11-19 15:00:29 +0000491 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
492 unsigned int frag_len = skb->len + FCS_LEN;
Daniel Drakee85d0912006-06-02 17:11:32 +0100493 unsigned int packet_length;
494 struct zd_ctrlset *cs = (struct zd_ctrlset *)
495 skb_push(skb, sizeof(struct zd_ctrlset));
496
Daniel Drakee85d0912006-06-02 17:11:32 +0100497 ZD_ASSERT(frag_len <= 0xffff);
Daniel Drakee85d0912006-06-02 17:11:32 +0100498
Daniel Drake459c51a2007-11-19 15:00:29 +0000499 cs->modulation = control->tx_rate;
Daniel Drakee85d0912006-06-02 17:11:32 +0100500
501 cs->tx_length = cpu_to_le16(frag_len);
502
Daniel Drake459c51a2007-11-19 15:00:29 +0000503 cs_set_control(mac, cs, hdr, control->flags);
Daniel Drakee85d0912006-06-02 17:11:32 +0100504
505 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
506 ZD_ASSERT(packet_length <= 0xffff);
507 /* ZD1211B: Computing the length difference this way, gives us
508 * flexibility to compute the packet length.
509 */
Daniel Drake74553ae2007-07-01 18:22:32 +0100510 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
Daniel Drakee85d0912006-06-02 17:11:32 +0100511 packet_length - frag_len : packet_length);
512
513 /*
514 * CURRENT LENGTH:
515 * - transmit frame length in microseconds
516 * - seems to be derived from frame length
517 * - see Cal_Us_Service() in zdinlinef.h
518 * - if macp->bTxBurstEnable is enabled, then multiply by 4
519 * - bTxBurstEnable is never set in the vendor driver
520 *
521 * SERVICE:
522 * - "for PLCP configuration"
523 * - always 0 except in some situations at 802.11b 11M
524 * - see line 53 of zdinlinef.h
525 */
526 cs->service = 0;
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100527 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
Daniel Drakee85d0912006-06-02 17:11:32 +0100528 le16_to_cpu(cs->tx_length));
529 if (r < 0)
530 return r;
531 cs->current_length = cpu_to_le16(r);
Daniel Drake459c51a2007-11-19 15:00:29 +0000532 cs->next_frame_length = 0;
Daniel Drakee85d0912006-06-02 17:11:32 +0100533
534 return 0;
535}
536
Daniel Drake459c51a2007-11-19 15:00:29 +0000537/**
538 * zd_op_tx - transmits a network frame to the device
Ulrich Kunitz741fec52006-11-22 00:05:53 +0000539 *
Daniel Drake459c51a2007-11-19 15:00:29 +0000540 * @dev: mac80211 hardware device
541 * @skb: socket buffer
542 * @control: the control structure
Daniel Drakee85d0912006-06-02 17:11:32 +0100543 *
Daniel Drake459c51a2007-11-19 15:00:29 +0000544 * This function transmit an IEEE 802.11 network frame to the device. The
545 * control block of the skbuff will be initialized. If necessary the incoming
546 * mac80211 queues will be stopped.
Daniel Drakee85d0912006-06-02 17:11:32 +0100547 */
Daniel Drake459c51a2007-11-19 15:00:29 +0000548static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
549 struct ieee80211_tx_control *control)
Daniel Drakee85d0912006-06-02 17:11:32 +0100550{
Daniel Drake459c51a2007-11-19 15:00:29 +0000551 struct zd_mac *mac = zd_hw_mac(hw);
552 int r;
Daniel Drakee85d0912006-06-02 17:11:32 +0100553
Daniel Drake459c51a2007-11-19 15:00:29 +0000554 r = fill_ctrlset(mac, skb, control);
555 if (r)
556 return r;
Daniel Drakee85d0912006-06-02 17:11:32 +0100557
Daniel Drake459c51a2007-11-19 15:00:29 +0000558 r = init_tx_skb_control_block(skb, hw, control);
559 if (r)
560 return r;
561 r = zd_usb_tx(&mac->chip.usb, skb);
562 if (r) {
563 clear_tx_skb_control_block(skb);
564 return r;
565 }
566 return 0;
567}
568
569/**
570 * filter_ack - filters incoming packets for acknowledgements
571 * @dev: the mac80211 device
572 * @rx_hdr: received header
573 * @stats: the status for the received packet
574 *
575 * This functions looks for ACK packets and tries to match them with the
576 * frames in the tx queue. If a match is found the frame will be dequeued and
577 * the upper layers is informed about the successful transmission. If
578 * mac80211 queues have been stopped and the number of frames still to be
579 * transmitted is low the queues will be opened again.
580 *
581 * Returns 1 if the frame was an ACK, 0 if it was ignored.
582 */
583static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
584 struct ieee80211_rx_status *stats)
585{
586 u16 fc = le16_to_cpu(rx_hdr->frame_control);
587 struct sk_buff *skb;
588 struct sk_buff_head *q;
589 unsigned long flags;
590
591 if ((fc & (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) !=
592 (IEEE80211_FTYPE_CTL | IEEE80211_STYPE_ACK))
593 return 0;
594
595 q = &zd_hw_mac(hw)->ack_wait_queue;
596 spin_lock_irqsave(&q->lock, flags);
597 for (skb = q->next; skb != (struct sk_buff *)q; skb = skb->next) {
598 struct ieee80211_hdr *tx_hdr;
599
600 tx_hdr = (struct ieee80211_hdr *)skb->data;
601 if (likely(!compare_ether_addr(tx_hdr->addr2, rx_hdr->addr1)))
602 {
603 struct ieee80211_tx_status status = {{0}};
604 status.flags = IEEE80211_TX_STATUS_ACK;
605 status.ack_signal = stats->ssi;
606 __skb_unlink(skb, q);
607 tx_status(hw, skb, &status, 1);
608 goto out;
609 }
610 }
611out:
612 spin_unlock_irqrestore(&q->lock, flags);
613 return 1;
614}
615
616int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
617{
618 struct zd_mac *mac = zd_hw_mac(hw);
619 struct ieee80211_rx_status stats;
620 const struct rx_status *status;
621 struct sk_buff *skb;
622 int bad_frame = 0;
623
624 if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
625 FCS_LEN + sizeof(struct rx_status))
Daniel Drakee85d0912006-06-02 17:11:32 +0100626 return -EINVAL;
627
Daniel Drake459c51a2007-11-19 15:00:29 +0000628 memset(&stats, 0, sizeof(stats));
Daniel Drakee85d0912006-06-02 17:11:32 +0100629
Daniel Drake459c51a2007-11-19 15:00:29 +0000630 /* Note about pass_failed_fcs and pass_ctrl access below:
631 * mac locking intentionally omitted here, as this is the only unlocked
632 * reader and the only writer is configure_filter. Plus, if there were
633 * any races accessing these variables, it wouldn't really matter.
634 * If mac80211 ever provides a way for us to access filter flags
635 * from outside configure_filter, we could improve on this. Also, this
636 * situation may change once we implement some kind of DMA-into-skb
637 * RX path. */
Daniel Drakee85d0912006-06-02 17:11:32 +0100638
Daniel Drake459c51a2007-11-19 15:00:29 +0000639 /* Caller has to ensure that length >= sizeof(struct rx_status). */
640 status = (struct rx_status *)
Ulrich Kunitz937a0492007-10-02 18:36:53 +0100641 (buffer + (length - sizeof(struct rx_status)));
Daniel Drakee85d0912006-06-02 17:11:32 +0100642 if (status->frame_status & ZD_RX_ERROR) {
Daniel Drake459c51a2007-11-19 15:00:29 +0000643 if (mac->pass_failed_fcs &&
644 (status->frame_status & ZD_RX_CRC32_ERROR)) {
645 stats.flag |= RX_FLAG_FAILED_FCS_CRC;
646 bad_frame = 1;
647 } else {
648 return -EINVAL;
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000649 }
Daniel Drakee85d0912006-06-02 17:11:32 +0100650 }
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000651
Daniel Drake459c51a2007-11-19 15:00:29 +0000652 stats.channel = _zd_chip_get_channel(&mac->chip);
653 stats.freq = zd_channels[stats.channel - 1].freq;
654 stats.phymode = MODE_IEEE80211G;
655 stats.ssi = status->signal_strength;
656 stats.signal = zd_rx_qual_percent(buffer,
Daniel Drakee85d0912006-06-02 17:11:32 +0100657 length - sizeof(struct rx_status),
658 status);
Daniel Drake459c51a2007-11-19 15:00:29 +0000659 stats.rate = zd_rx_rate(buffer, status);
Daniel Drakee85d0912006-06-02 17:11:32 +0100660
Daniel Drake459c51a2007-11-19 15:00:29 +0000661 length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
662 buffer += ZD_PLCP_HEADER_SIZE;
Daniel Drakee85d0912006-06-02 17:11:32 +0100663
Daniel Drake459c51a2007-11-19 15:00:29 +0000664 /* Except for bad frames, filter each frame to see if it is an ACK, in
665 * which case our internal TX tracking is updated. Normally we then
666 * bail here as there's no need to pass ACKs on up to the stack, but
667 * there is also the case where the stack has requested us to pass
668 * control frames on up (pass_ctrl) which we must consider. */
669 if (!bad_frame &&
670 filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
671 && !mac->pass_ctrl)
672 return 0;
Daniel Drakee85d0912006-06-02 17:11:32 +0100673
Daniel Drake459c51a2007-11-19 15:00:29 +0000674 skb = dev_alloc_skb(length);
675 if (skb == NULL)
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800676 return -ENOMEM;
Daniel Drake459c51a2007-11-19 15:00:29 +0000677 memcpy(skb_put(skb, length), buffer, length);
678
679 ieee80211_rx_irqsafe(hw, skb, &stats);
Daniel Drakee85d0912006-06-02 17:11:32 +0100680 return 0;
681}
682
Daniel Drake459c51a2007-11-19 15:00:29 +0000683static int zd_op_add_interface(struct ieee80211_hw *hw,
684 struct ieee80211_if_init_conf *conf)
Daniel Drakee85d0912006-06-02 17:11:32 +0100685{
Daniel Drake459c51a2007-11-19 15:00:29 +0000686 struct zd_mac *mac = zd_hw_mac(hw);
687
688 /* using IEEE80211_IF_TYPE_INVALID to indicate no mode selected */
689 if (mac->type != IEEE80211_IF_TYPE_INVALID)
690 return -EOPNOTSUPP;
691
692 switch (conf->type) {
693 case IEEE80211_IF_TYPE_MNTR:
694 case IEEE80211_IF_TYPE_STA:
695 mac->type = conf->type;
696 break;
697 default:
698 return -EOPNOTSUPP;
699 }
700
701 return zd_write_mac_addr(&mac->chip, conf->mac_addr);
Daniel Drakee85d0912006-06-02 17:11:32 +0100702}
703
Daniel Drake459c51a2007-11-19 15:00:29 +0000704static void zd_op_remove_interface(struct ieee80211_hw *hw,
705 struct ieee80211_if_init_conf *conf)
Daniel Drakee85d0912006-06-02 17:11:32 +0100706{
Daniel Drake459c51a2007-11-19 15:00:29 +0000707 struct zd_mac *mac = zd_hw_mac(hw);
708 mac->type = IEEE80211_IF_TYPE_INVALID;
709 zd_write_mac_addr(&mac->chip, NULL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100710}
711
Daniel Drake459c51a2007-11-19 15:00:29 +0000712static int zd_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Daniel Drakee85d0912006-06-02 17:11:32 +0100713{
Daniel Drake459c51a2007-11-19 15:00:29 +0000714 struct zd_mac *mac = zd_hw_mac(hw);
715 return zd_chip_set_channel(&mac->chip, conf->channel);
Daniel Drakee85d0912006-06-02 17:11:32 +0100716}
717
Daniel Drake459c51a2007-11-19 15:00:29 +0000718static int zd_op_config_interface(struct ieee80211_hw *hw, int if_id,
719 struct ieee80211_if_conf *conf)
Daniel Drakee85d0912006-06-02 17:11:32 +0100720{
Daniel Drake459c51a2007-11-19 15:00:29 +0000721 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100722
Daniel Drakee85d0912006-06-02 17:11:32 +0100723 spin_lock_irq(&mac->lock);
Daniel Drake459c51a2007-11-19 15:00:29 +0000724 mac->associated = is_valid_ether_addr(conf->bssid);
Daniel Drakee85d0912006-06-02 17:11:32 +0100725 spin_unlock_irq(&mac->lock);
Daniel Drake459c51a2007-11-19 15:00:29 +0000726
727 /* TODO: do hardware bssid filtering */
728 return 0;
729}
730
731static void set_multicast_hash_handler(struct work_struct *work)
732{
733 struct zd_mac *mac =
734 container_of(work, struct zd_mac, set_multicast_hash_work);
735 struct zd_mc_hash hash;
736
737 spin_lock_irq(&mac->lock);
738 hash = mac->multicast_hash;
739 spin_unlock_irq(&mac->lock);
740
741 zd_chip_set_multicast_hash(&mac->chip, &hash);
742}
743
744static void set_rx_filter_handler(struct work_struct *work)
745{
746 struct zd_mac *mac =
747 container_of(work, struct zd_mac, set_rx_filter_work);
748 int r;
749
750 dev_dbg_f(zd_mac_dev(mac), "\n");
751 r = set_rx_filter(mac);
752 if (r)
753 dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r);
754}
755
756#define SUPPORTED_FIF_FLAGS \
757 (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
758 FIF_OTHER_BSS)
759static void zd_op_configure_filter(struct ieee80211_hw *hw,
760 unsigned int changed_flags,
761 unsigned int *new_flags,
762 int mc_count, struct dev_mc_list *mclist)
763{
764 struct zd_mc_hash hash;
765 struct zd_mac *mac = zd_hw_mac(hw);
766 unsigned long flags;
767 int i;
768
769 /* Only deal with supported flags */
770 changed_flags &= SUPPORTED_FIF_FLAGS;
771 *new_flags &= SUPPORTED_FIF_FLAGS;
772
773 /* changed_flags is always populated but this driver
774 * doesn't support all FIF flags so its possible we don't
775 * need to do anything */
776 if (!changed_flags)
777 return;
778
779 if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI)) {
780 zd_mc_add_all(&hash);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +0100781 } else {
Daniel Drake459c51a2007-11-19 15:00:29 +0000782 DECLARE_MAC_BUF(macbuf);
783
784 zd_mc_clear(&hash);
785 for (i = 0; i < mc_count; i++) {
786 if (!mclist)
787 break;
788 dev_dbg_f(zd_mac_dev(mac), "mc addr %s\n",
789 print_mac(macbuf, mclist->dmi_addr));
790 zd_mc_add_addr(&hash, mclist->dmi_addr);
791 mclist = mclist->next;
792 }
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +0100793 }
Daniel Drake459c51a2007-11-19 15:00:29 +0000794
795 spin_lock_irqsave(&mac->lock, flags);
796 mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
797 mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
798 mac->multicast_hash = hash;
799 spin_unlock_irqrestore(&mac->lock, flags);
800 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
801
802 if (changed_flags & FIF_CONTROL)
803 queue_work(zd_workqueue, &mac->set_rx_filter_work);
804
805 /* no handling required for FIF_OTHER_BSS as we don't currently
806 * do BSSID filtering */
807 /* FIXME: in future it would be nice to enable the probe response
808 * filter (so that the driver doesn't see them) until
809 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
810 * have to schedule work to enable prbresp reception, which might
811 * happen too late. For now we'll just listen and forward them all the
812 * time. */
813}
814
815static void set_rts_cts_work(struct work_struct *work)
816{
817 struct zd_mac *mac =
818 container_of(work, struct zd_mac, set_rts_cts_work);
819 unsigned long flags;
820 unsigned int short_preamble;
821
822 mutex_lock(&mac->chip.mutex);
823
824 spin_lock_irqsave(&mac->lock, flags);
825 mac->updating_rts_rate = 0;
826 short_preamble = mac->short_preamble;
827 spin_unlock_irqrestore(&mac->lock, flags);
828
829 zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
830 mutex_unlock(&mac->chip.mutex);
831}
832
833static void zd_op_erp_ie_changed(struct ieee80211_hw *hw, u8 changes,
834 int cts_protection, int preamble)
835{
836 struct zd_mac *mac = zd_hw_mac(hw);
837 unsigned long flags;
838
839 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
840
841 if (changes & IEEE80211_ERP_CHANGE_PREAMBLE) {
842 spin_lock_irqsave(&mac->lock, flags);
843 mac->short_preamble = !preamble;
844 if (!mac->updating_rts_rate) {
845 mac->updating_rts_rate = 1;
846 /* FIXME: should disable TX here, until work has
847 * completed and RTS_CTS reg is updated */
848 queue_work(zd_workqueue, &mac->set_rts_cts_work);
849 }
850 spin_unlock_irqrestore(&mac->lock, flags);
851 }
852}
853
854static const struct ieee80211_ops zd_ops = {
855 .tx = zd_op_tx,
856 .start = zd_op_start,
857 .stop = zd_op_stop,
858 .add_interface = zd_op_add_interface,
859 .remove_interface = zd_op_remove_interface,
860 .config = zd_op_config,
861 .config_interface = zd_op_config_interface,
862 .configure_filter = zd_op_configure_filter,
863 .erp_ie_changed = zd_op_erp_ie_changed,
864};
865
866struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
867{
868 struct zd_mac *mac;
869 struct ieee80211_hw *hw;
870 int i;
871
872 hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
873 if (!hw) {
874 dev_dbg_f(&intf->dev, "out of memory\n");
875 return NULL;
876 }
877
878 mac = zd_hw_mac(hw);
879
880 memset(mac, 0, sizeof(*mac));
881 spin_lock_init(&mac->lock);
882 mac->hw = hw;
883
884 mac->type = IEEE80211_IF_TYPE_INVALID;
885
886 memcpy(mac->channels, zd_channels, sizeof(zd_channels));
887 memcpy(mac->rates, zd_rates, sizeof(zd_rates));
888 mac->modes[0].mode = MODE_IEEE80211G;
889 mac->modes[0].num_rates = ARRAY_SIZE(zd_rates);
890 mac->modes[0].rates = mac->rates;
891 mac->modes[0].num_channels = ARRAY_SIZE(zd_channels);
892 mac->modes[0].channels = mac->channels;
893 mac->modes[1].mode = MODE_IEEE80211B;
894 mac->modes[1].num_rates = 4;
895 mac->modes[1].rates = mac->rates;
896 mac->modes[1].num_channels = ARRAY_SIZE(zd_channels);
897 mac->modes[1].channels = mac->channels;
898
899 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
900 IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED;
901 hw->max_rssi = 100;
902 hw->max_signal = 100;
903
904 hw->queues = 1;
905 hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
906
907 skb_queue_head_init(&mac->ack_wait_queue);
908
909 for (i = 0; i < 2; i++) {
910 if (ieee80211_register_hwmode(hw, &mac->modes[i])) {
911 dev_dbg_f(&intf->dev, "cannot register hwmode\n");
912 ieee80211_free_hw(hw);
913 return NULL;
914 }
915 }
916
917 zd_chip_init(&mac->chip, hw, intf);
918 housekeeping_init(mac);
919 INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
920 INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
921 INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler);
922
923 SET_IEEE80211_DEV(hw, &intf->dev);
924 return hw;
Daniel Drakee85d0912006-06-02 17:11:32 +0100925}
926
Ulrich Kunitz583afd12006-09-13 02:42:38 +0100927#define LINK_LED_WORK_DELAY HZ
928
David Howellsc4028952006-11-22 14:57:56 +0000929static void link_led_handler(struct work_struct *work)
Ulrich Kunitz583afd12006-09-13 02:42:38 +0100930{
David Howellsc4028952006-11-22 14:57:56 +0000931 struct zd_mac *mac =
932 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
Ulrich Kunitz583afd12006-09-13 02:42:38 +0100933 struct zd_chip *chip = &mac->chip;
Ulrich Kunitz583afd12006-09-13 02:42:38 +0100934 int is_associated;
935 int r;
936
937 spin_lock_irq(&mac->lock);
Daniel Drake459c51a2007-11-19 15:00:29 +0000938 is_associated = mac->associated;
Ulrich Kunitz583afd12006-09-13 02:42:38 +0100939 spin_unlock_irq(&mac->lock);
940
941 r = zd_chip_control_leds(chip,
942 is_associated ? LED_ASSOCIATED : LED_SCANNING);
943 if (r)
Daniel Drake459c51a2007-11-19 15:00:29 +0000944 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
Ulrich Kunitz583afd12006-09-13 02:42:38 +0100945
946 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
947 LINK_LED_WORK_DELAY);
948}
949
950static void housekeeping_init(struct zd_mac *mac)
951{
David Howellsc4028952006-11-22 14:57:56 +0000952 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
Ulrich Kunitz583afd12006-09-13 02:42:38 +0100953}
954
955static void housekeeping_enable(struct zd_mac *mac)
956{
957 dev_dbg_f(zd_mac_dev(mac), "\n");
958 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
959 0);
960}
961
962static void housekeeping_disable(struct zd_mac *mac)
963{
964 dev_dbg_f(zd_mac_dev(mac), "\n");
965 cancel_rearming_delayed_workqueue(zd_workqueue,
966 &mac->housekeeping.link_led_work);
967 zd_chip_control_leds(&mac->chip, LED_OFF);
968}