blob: 3409cf97f5f8a5fb72279c26905ef273988f552b [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[] = {
37 { .rate = 10,
38 .val = ZD_CCK_RATE_1M,
39 .flags = IEEE80211_RATE_CCK },
40 { .rate = 20,
41 .val = ZD_CCK_RATE_2M,
42 .val2 = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
43 .flags = IEEE80211_RATE_CCK_2 },
44 { .rate = 55,
45 .val = ZD_CCK_RATE_5_5M,
46 .val2 = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
47 .flags = IEEE80211_RATE_CCK_2 },
48 { .rate = 110,
49 .val = ZD_CCK_RATE_11M,
50 .val2 = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
51 .flags = IEEE80211_RATE_CCK_2 },
52 { .rate = 60,
53 .val = ZD_OFDM_RATE_6M,
54 .flags = IEEE80211_RATE_OFDM },
55 { .rate = 90,
56 .val = ZD_OFDM_RATE_9M,
57 .flags = IEEE80211_RATE_OFDM },
58 { .rate = 120,
59 .val = ZD_OFDM_RATE_12M,
60 .flags = IEEE80211_RATE_OFDM },
61 { .rate = 180,
62 .val = ZD_OFDM_RATE_18M,
63 .flags = IEEE80211_RATE_OFDM },
64 { .rate = 240,
65 .val = ZD_OFDM_RATE_24M,
66 .flags = IEEE80211_RATE_OFDM },
67 { .rate = 360,
68 .val = ZD_OFDM_RATE_36M,
69 .flags = IEEE80211_RATE_OFDM },
70 { .rate = 480,
71 .val = ZD_OFDM_RATE_48M,
72 .flags = IEEE80211_RATE_OFDM },
73 { .rate = 540,
74 .val = ZD_OFDM_RATE_54M,
75 .flags = IEEE80211_RATE_OFDM },
76};
77
78static const struct ieee80211_channel zd_channels[] = {
79 { .chan = 1,
80 .freq = 2412},
81 { .chan = 2,
82 .freq = 2417},
83 { .chan = 3,
84 .freq = 2422},
85 { .chan = 4,
86 .freq = 2427},
87 { .chan = 5,
88 .freq = 2432},
89 { .chan = 6,
90 .freq = 2437},
91 { .chan = 7,
92 .freq = 2442},
93 { .chan = 8,
94 .freq = 2447},
95 { .chan = 9,
96 .freq = 2452},
97 { .chan = 10,
98 .freq = 2457},
99 { .chan = 11,
100 .freq = 2462},
101 { .chan = 12,
102 .freq = 2467},
103 { .chan = 13,
104 .freq = 2472},
105 { .chan = 14,
106 .freq = 2484}
107};
Daniel Drakee85d0912006-06-02 17:11:32 +0100108
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100109static void housekeeping_init(struct zd_mac *mac);
110static void housekeeping_enable(struct zd_mac *mac);
111static void housekeeping_disable(struct zd_mac *mac);
112
Daniel Drake459c51a2007-11-19 15:00:29 +0000113int zd_mac_preinit_hw(struct ieee80211_hw *hw)
Daniel Drake74553ae2007-07-01 18:22:32 +0100114{
115 int r;
116 u8 addr[ETH_ALEN];
Daniel Drake459c51a2007-11-19 15:00:29 +0000117 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drake74553ae2007-07-01 18:22:32 +0100118
119 r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
120 if (r)
121 return r;
122
Daniel Drake459c51a2007-11-19 15:00:29 +0000123 SET_IEEE80211_PERM_ADDR(hw, addr);
124
Daniel Drake74553ae2007-07-01 18:22:32 +0100125 return 0;
126}
127
Daniel Drake459c51a2007-11-19 15:00:29 +0000128int zd_mac_init_hw(struct ieee80211_hw *hw)
Daniel Drakee85d0912006-06-02 17:11:32 +0100129{
130 int r;
Daniel Drake459c51a2007-11-19 15:00:29 +0000131 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100132 struct zd_chip *chip = &mac->chip;
Daniel Drakee85d0912006-06-02 17:11:32 +0100133 u8 default_regdomain;
134
135 r = zd_chip_enable_int(chip);
136 if (r)
137 goto out;
Daniel Drake74553ae2007-07-01 18:22:32 +0100138 r = zd_chip_init_hw(chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100139 if (r)
140 goto disable_int;
141
Daniel Drakee85d0912006-06-02 17:11:32 +0100142 ZD_ASSERT(!irqs_disabled());
Daniel Drakee85d0912006-06-02 17:11:32 +0100143
144 r = zd_read_regdomain(chip, &default_regdomain);
145 if (r)
146 goto disable_int;
Daniel Drakee85d0912006-06-02 17:11:32 +0100147 spin_lock_irq(&mac->lock);
148 mac->regdomain = mac->default_regdomain = default_regdomain;
149 spin_unlock_irq(&mac->lock);
Daniel Drakee85d0912006-06-02 17:11:32 +0100150
Daniel Drake40da08b2006-08-01 23:43:32 +0200151 /* We must inform the device that we are doing encryption/decryption in
152 * software at the moment. */
153 r = zd_set_encryption_type(chip, ENC_SNIFFER);
Daniel Drakee85d0912006-06-02 17:11:32 +0100154 if (r)
155 goto disable_int;
156
Daniel Drake459c51a2007-11-19 15:00:29 +0000157 zd_geo_init(hw, mac->regdomain);
Daniel Drakee85d0912006-06-02 17:11:32 +0100158
159 r = 0;
160disable_int:
161 zd_chip_disable_int(chip);
162out:
163 return r;
164}
165
166void zd_mac_clear(struct zd_mac *mac)
167{
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000168 flush_workqueue(zd_workqueue);
Daniel Drakee85d0912006-06-02 17:11:32 +0100169 zd_chip_clear(&mac->chip);
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100170 ZD_ASSERT(!spin_is_locked(&mac->lock));
171 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
Daniel Drakee85d0912006-06-02 17:11:32 +0100172}
173
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100174static int set_rx_filter(struct zd_mac *mac)
Daniel Drakee85d0912006-06-02 17:11:32 +0100175{
Daniel Drake459c51a2007-11-19 15:00:29 +0000176 unsigned long flags;
177 u32 filter = STA_RX_FILTER;
Daniel Drakee85d0912006-06-02 17:11:32 +0100178
Daniel Drake459c51a2007-11-19 15:00:29 +0000179 spin_lock_irqsave(&mac->lock, flags);
180 if (mac->pass_ctrl)
181 filter |= RX_FILTER_CTRL;
182 spin_unlock_irqrestore(&mac->lock, flags);
183
184 return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100185}
186
187static int set_mc_hash(struct zd_mac *mac)
188{
189 struct zd_mc_hash hash;
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100190 zd_mc_clear(&hash);
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100191 return zd_chip_set_multicast_hash(&mac->chip, &hash);
192}
193
Daniel Drake459c51a2007-11-19 15:00:29 +0000194static int zd_op_start(struct ieee80211_hw *hw)
Daniel Drakee85d0912006-06-02 17:11:32 +0100195{
Daniel Drake459c51a2007-11-19 15:00:29 +0000196 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100197 struct zd_chip *chip = &mac->chip;
Daniel Drake74553ae2007-07-01 18:22:32 +0100198 struct zd_usb *usb = &chip->usb;
Daniel Drakee85d0912006-06-02 17:11:32 +0100199 int r;
200
Daniel Drake74553ae2007-07-01 18:22:32 +0100201 if (!usb->initialized) {
202 r = zd_usb_init_hw(usb);
203 if (r)
204 goto out;
205 }
206
Daniel Drakee85d0912006-06-02 17:11:32 +0100207 r = zd_chip_enable_int(chip);
208 if (r < 0)
209 goto out;
210
211 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
212 if (r < 0)
213 goto disable_int;
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100214 r = set_rx_filter(mac);
215 if (r)
216 goto disable_int;
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100217 r = set_mc_hash(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100218 if (r)
219 goto disable_int;
220 r = zd_chip_switch_radio_on(chip);
221 if (r < 0)
222 goto disable_int;
Daniel Drake459c51a2007-11-19 15:00:29 +0000223 r = zd_chip_enable_rxtx(chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100224 if (r < 0)
225 goto disable_radio;
226 r = zd_chip_enable_hwint(chip);
227 if (r < 0)
Daniel Drake459c51a2007-11-19 15:00:29 +0000228 goto disable_rxtx;
Daniel Drakee85d0912006-06-02 17:11:32 +0100229
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100230 housekeeping_enable(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100231 return 0;
Daniel Drake459c51a2007-11-19 15:00:29 +0000232disable_rxtx:
233 zd_chip_disable_rxtx(chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100234disable_radio:
235 zd_chip_switch_radio_off(chip);
236disable_int:
237 zd_chip_disable_int(chip);
238out:
239 return r;
240}
241
Daniel Drake459c51a2007-11-19 15:00:29 +0000242/**
243 * clear_tx_skb_control_block - clears the control block of tx skbuffs
244 * @skb: a &struct sk_buff pointer
245 *
246 * This clears the control block of skbuff buffers, which were transmitted to
247 * the device. Notify that the function is not thread-safe, so prevent
248 * multiple calls.
249 */
250static void clear_tx_skb_control_block(struct sk_buff *skb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100251{
Daniel Drake459c51a2007-11-19 15:00:29 +0000252 struct zd_tx_skb_control_block *cb =
253 (struct zd_tx_skb_control_block *)skb->cb;
254
255 kfree(cb->control);
256 cb->control = NULL;
257}
258
259/**
260 * kfree_tx_skb - frees a tx skbuff
261 * @skb: a &struct sk_buff pointer
262 *
263 * Frees the tx skbuff. Frees also the allocated control structure in the
264 * control block if necessary.
265 */
266static void kfree_tx_skb(struct sk_buff *skb)
267{
268 clear_tx_skb_control_block(skb);
269 dev_kfree_skb_any(skb);
270}
271
272static void zd_op_stop(struct ieee80211_hw *hw)
273{
274 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100275 struct zd_chip *chip = &mac->chip;
Daniel Drake459c51a2007-11-19 15:00:29 +0000276 struct sk_buff *skb;
277 struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
Daniel Drakee85d0912006-06-02 17:11:32 +0100278
Daniel Drake459c51a2007-11-19 15:00:29 +0000279 /* The order here deliberately is a little different from the open()
Daniel Drakee85d0912006-06-02 17:11:32 +0100280 * method, since we need to make sure there is no opportunity for RX
Daniel Drake459c51a2007-11-19 15:00:29 +0000281 * frames to be processed by mac80211 after we have stopped it.
Daniel Drakee85d0912006-06-02 17:11:32 +0100282 */
283
Daniel Drake459c51a2007-11-19 15:00:29 +0000284 zd_chip_disable_rxtx(chip);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100285 housekeeping_disable(mac);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000286 flush_workqueue(zd_workqueue);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000287
Daniel Drakee85d0912006-06-02 17:11:32 +0100288 zd_chip_disable_hwint(chip);
289 zd_chip_switch_radio_off(chip);
290 zd_chip_disable_int(chip);
291
Daniel Drake459c51a2007-11-19 15:00:29 +0000292
293 while ((skb = skb_dequeue(ack_wait_queue)))
294 kfree_tx_skb(skb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100295}
296
Daniel Drake459c51a2007-11-19 15:00:29 +0000297/**
298 * init_tx_skb_control_block - initializes skb control block
299 * @skb: a &sk_buff pointer
300 * @dev: pointer to the mac80221 device
301 * @control: mac80211 tx control applying for the frame in @skb
302 *
303 * Initializes the control block of the skbuff to be transmitted.
304 */
305static int init_tx_skb_control_block(struct sk_buff *skb,
306 struct ieee80211_hw *hw,
307 struct ieee80211_tx_control *control)
Daniel Drakee85d0912006-06-02 17:11:32 +0100308{
Daniel Drake459c51a2007-11-19 15:00:29 +0000309 struct zd_tx_skb_control_block *cb =
310 (struct zd_tx_skb_control_block *)skb->cb;
Daniel Drakee85d0912006-06-02 17:11:32 +0100311
Daniel Drake459c51a2007-11-19 15:00:29 +0000312 ZD_ASSERT(sizeof(*cb) <= sizeof(skb->cb));
313 memset(cb, 0, sizeof(*cb));
314 cb->hw= hw;
315 cb->control = kmalloc(sizeof(*control), GFP_ATOMIC);
316 if (cb->control == NULL)
317 return -ENOMEM;
318 memcpy(cb->control, control, sizeof(*control));
Daniel Drakee85d0912006-06-02 17:11:32 +0100319
320 return 0;
321}
322
Daniel Drake459c51a2007-11-19 15:00:29 +0000323/**
324 * tx_status - reports tx status of a packet if required
325 * @hw - a &struct ieee80211_hw pointer
326 * @skb - a sk-buffer
327 * @status - the tx status of the packet without control information
328 * @success - True for successfull transmission of the frame
329 *
330 * This information calls ieee80211_tx_status_irqsafe() if required by the
331 * control information. It copies the control information into the status
332 * information.
333 *
334 * If no status information has been requested, the skb is freed.
335 */
336static void tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
337 struct ieee80211_tx_status *status,
338 bool success)
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000339{
Daniel Drake459c51a2007-11-19 15:00:29 +0000340 struct zd_tx_skb_control_block *cb = (struct zd_tx_skb_control_block *)
341 skb->cb;
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000342
Daniel Drake459c51a2007-11-19 15:00:29 +0000343 ZD_ASSERT(cb->control != NULL);
344 memcpy(&status->control, cb->control, sizeof(status->control));
345 if (!success)
346 status->excessive_retries = 1;
347 clear_tx_skb_control_block(skb);
348 ieee80211_tx_status_irqsafe(hw, skb, status);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000349}
350
Daniel Drake459c51a2007-11-19 15:00:29 +0000351/**
352 * zd_mac_tx_failed - callback for failed frames
353 * @dev: the mac80211 wireless device
354 *
355 * This function is called if a frame couldn't be succesfully be
356 * transferred. The first frame from the tx queue, will be selected and
357 * reported as error to the upper layers.
358 */
359void zd_mac_tx_failed(struct ieee80211_hw *hw)
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000360{
Daniel Drake459c51a2007-11-19 15:00:29 +0000361 struct sk_buff_head *q = &zd_hw_mac(hw)->ack_wait_queue;
362 struct sk_buff *skb;
363 struct ieee80211_tx_status status = {{0}};
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000364
Daniel Drake459c51a2007-11-19 15:00:29 +0000365 skb = skb_dequeue(q);
366 if (skb == NULL)
367 return;
368 tx_status(hw, skb, &status, 0);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000369}
370
Daniel Drake459c51a2007-11-19 15:00:29 +0000371/**
372 * zd_mac_tx_to_dev - callback for USB layer
373 * @skb: a &sk_buff pointer
374 * @error: error value, 0 if transmission successful
375 *
376 * Informs the MAC layer that the frame has successfully transferred to the
377 * device. If an ACK is required and the transfer to the device has been
378 * successful, the packets are put on the @ack_wait_queue with
379 * the control set removed.
380 */
381void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
Daniel Drakee85d0912006-06-02 17:11:32 +0100382{
Daniel Drake459c51a2007-11-19 15:00:29 +0000383 struct zd_tx_skb_control_block *cb =
384 (struct zd_tx_skb_control_block *)skb->cb;
385 struct ieee80211_hw *hw = cb->hw;
Daniel Drakee85d0912006-06-02 17:11:32 +0100386
Daniel Drake459c51a2007-11-19 15:00:29 +0000387 if (likely(cb->control)) {
388 skb_pull(skb, sizeof(struct zd_ctrlset));
389 if (unlikely(error ||
390 (cb->control->flags & IEEE80211_TXCTL_NO_ACK)))
391 {
392 struct ieee80211_tx_status status = {{0}};
393 tx_status(hw, skb, &status, !error);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000394 } else {
Daniel Drake459c51a2007-11-19 15:00:29 +0000395 struct sk_buff_head *q =
396 &zd_hw_mac(hw)->ack_wait_queue;
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000397
Daniel Drake459c51a2007-11-19 15:00:29 +0000398 skb_queue_tail(q, skb);
399 while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS)
400 zd_mac_tx_failed(hw);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000401 }
Daniel Drake459c51a2007-11-19 15:00:29 +0000402 } else {
403 kfree_tx_skb(skb);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000404 }
Daniel Drakee85d0912006-06-02 17:11:32 +0100405}
406
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000407static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
Daniel Drakee85d0912006-06-02 17:11:32 +0100408{
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100409 /* ZD_PURE_RATE() must be used to remove the modulation type flag of
Daniel Drake459c51a2007-11-19 15:00:29 +0000410 * the zd-rate values.
411 */
Daniel Drakee85d0912006-06-02 17:11:32 +0100412 static const u8 rate_divisor[] = {
Daniel Drake459c51a2007-11-19 15:00:29 +0000413 [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
414 [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
415 /* Bits must be doubled. */
416 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
417 [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
418 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
419 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
420 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
421 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
422 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
423 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
424 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
425 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
Daniel Drakee85d0912006-06-02 17:11:32 +0100426 };
427
428 u32 bits = (u32)tx_length * 8;
429 u32 divisor;
430
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100431 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
Daniel Drakee85d0912006-06-02 17:11:32 +0100432 if (divisor == 0)
433 return -EINVAL;
434
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000435 switch (zd_rate) {
436 case ZD_CCK_RATE_5_5M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100437 bits = (2*bits) + 10; /* round up to the next integer */
438 break;
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000439 case ZD_CCK_RATE_11M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100440 if (service) {
441 u32 t = bits % 11;
442 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
443 if (0 < t && t <= 3) {
444 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
445 }
446 }
447 bits += 10; /* round up to the next integer */
448 break;
449 }
450
451 return bits/divisor;
452}
453
Daniel Drakee85d0912006-06-02 17:11:32 +0100454static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
Daniel Drake459c51a2007-11-19 15:00:29 +0000455 struct ieee80211_hdr *header, u32 flags)
Daniel Drakee85d0912006-06-02 17:11:32 +0100456{
Daniel Drake459c51a2007-11-19 15:00:29 +0000457 u16 fctl = le16_to_cpu(header->frame_control);
Daniel Drakee85d0912006-06-02 17:11:32 +0100458
459 /*
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000460 * CONTROL TODO:
Daniel Drakee85d0912006-06-02 17:11:32 +0100461 * - if backoff needed, enable bit 0
462 * - if burst (backoff not needed) disable bit 0
Daniel Drakee85d0912006-06-02 17:11:32 +0100463 */
464
465 cs->control = 0;
466
467 /* First fragment */
Daniel Drake459c51a2007-11-19 15:00:29 +0000468 if (flags & IEEE80211_TXCTL_FIRST_FRAGMENT)
Daniel Drakee85d0912006-06-02 17:11:32 +0100469 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
470
471 /* Multicast */
472 if (is_multicast_ether_addr(header->addr1))
473 cs->control |= ZD_CS_MULTICAST;
474
475 /* PS-POLL */
Daniel Drake459c51a2007-11-19 15:00:29 +0000476 if ((fctl & (IEEE80211_FCTL_FTYPE|IEEE80211_FCTL_STYPE)) ==
477 (IEEE80211_FTYPE_CTL|IEEE80211_STYPE_PSPOLL))
Daniel Drakee85d0912006-06-02 17:11:32 +0100478 cs->control |= ZD_CS_PS_POLL_FRAME;
479
Daniel Drake459c51a2007-11-19 15:00:29 +0000480 if (flags & IEEE80211_TXCTL_USE_RTS_CTS)
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000481 cs->control |= ZD_CS_RTS;
482
Daniel Drake459c51a2007-11-19 15:00:29 +0000483 if (flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000484 cs->control |= ZD_CS_SELF_CTS;
Daniel Drakee85d0912006-06-02 17:11:32 +0100485
486 /* FIXME: Management frame? */
487}
488
489static int fill_ctrlset(struct zd_mac *mac,
Daniel Drake459c51a2007-11-19 15:00:29 +0000490 struct sk_buff *skb,
491 struct ieee80211_tx_control *control)
Daniel Drakee85d0912006-06-02 17:11:32 +0100492{
493 int r;
Daniel Drake459c51a2007-11-19 15:00:29 +0000494 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
495 unsigned int frag_len = skb->len + FCS_LEN;
Daniel Drakee85d0912006-06-02 17:11:32 +0100496 unsigned int packet_length;
497 struct zd_ctrlset *cs = (struct zd_ctrlset *)
498 skb_push(skb, sizeof(struct zd_ctrlset));
499
Daniel Drakee85d0912006-06-02 17:11:32 +0100500 ZD_ASSERT(frag_len <= 0xffff);
Daniel Drakee85d0912006-06-02 17:11:32 +0100501
Daniel Drake459c51a2007-11-19 15:00:29 +0000502 cs->modulation = control->tx_rate;
Daniel Drakee85d0912006-06-02 17:11:32 +0100503
504 cs->tx_length = cpu_to_le16(frag_len);
505
Daniel Drake459c51a2007-11-19 15:00:29 +0000506 cs_set_control(mac, cs, hdr, control->flags);
Daniel Drakee85d0912006-06-02 17:11:32 +0100507
508 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
509 ZD_ASSERT(packet_length <= 0xffff);
510 /* ZD1211B: Computing the length difference this way, gives us
511 * flexibility to compute the packet length.
512 */
Daniel Drake74553ae2007-07-01 18:22:32 +0100513 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
Daniel Drakee85d0912006-06-02 17:11:32 +0100514 packet_length - frag_len : packet_length);
515
516 /*
517 * CURRENT LENGTH:
518 * - transmit frame length in microseconds
519 * - seems to be derived from frame length
520 * - see Cal_Us_Service() in zdinlinef.h
521 * - if macp->bTxBurstEnable is enabled, then multiply by 4
522 * - bTxBurstEnable is never set in the vendor driver
523 *
524 * SERVICE:
525 * - "for PLCP configuration"
526 * - always 0 except in some situations at 802.11b 11M
527 * - see line 53 of zdinlinef.h
528 */
529 cs->service = 0;
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100530 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
Daniel Drakee85d0912006-06-02 17:11:32 +0100531 le16_to_cpu(cs->tx_length));
532 if (r < 0)
533 return r;
534 cs->current_length = cpu_to_le16(r);
Daniel Drake459c51a2007-11-19 15:00:29 +0000535 cs->next_frame_length = 0;
Daniel Drakee85d0912006-06-02 17:11:32 +0100536
537 return 0;
538}
539
Daniel Drake459c51a2007-11-19 15:00:29 +0000540/**
541 * zd_op_tx - transmits a network frame to the device
Ulrich Kunitz741fec52006-11-22 00:05:53 +0000542 *
Daniel Drake459c51a2007-11-19 15:00:29 +0000543 * @dev: mac80211 hardware device
544 * @skb: socket buffer
545 * @control: the control structure
Daniel Drakee85d0912006-06-02 17:11:32 +0100546 *
Daniel Drake459c51a2007-11-19 15:00:29 +0000547 * This function transmit an IEEE 802.11 network frame to the device. The
548 * control block of the skbuff will be initialized. If necessary the incoming
549 * mac80211 queues will be stopped.
Daniel Drakee85d0912006-06-02 17:11:32 +0100550 */
Daniel Drake459c51a2007-11-19 15:00:29 +0000551static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
552 struct ieee80211_tx_control *control)
Daniel Drakee85d0912006-06-02 17:11:32 +0100553{
Daniel Drake459c51a2007-11-19 15:00:29 +0000554 struct zd_mac *mac = zd_hw_mac(hw);
555 int r;
Daniel Drakee85d0912006-06-02 17:11:32 +0100556
Daniel Drake459c51a2007-11-19 15:00:29 +0000557 r = fill_ctrlset(mac, skb, control);
558 if (r)
559 return r;
Daniel Drakee85d0912006-06-02 17:11:32 +0100560
Daniel Drake459c51a2007-11-19 15:00:29 +0000561 r = init_tx_skb_control_block(skb, hw, control);
562 if (r)
563 return r;
564 r = zd_usb_tx(&mac->chip.usb, skb);
565 if (r) {
566 clear_tx_skb_control_block(skb);
567 return r;
568 }
569 return 0;
570}
571
572/**
573 * filter_ack - filters incoming packets for acknowledgements
574 * @dev: the mac80211 device
575 * @rx_hdr: received header
576 * @stats: the status for the received packet
577 *
578 * This functions looks for ACK packets and tries to match them with the
579 * frames in the tx queue. If a match is found the frame will be dequeued and
580 * the upper layers is informed about the successful transmission. If
581 * mac80211 queues have been stopped and the number of frames still to be
582 * transmitted is low the queues will be opened again.
583 *
584 * Returns 1 if the frame was an ACK, 0 if it was ignored.
585 */
586static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
587 struct ieee80211_rx_status *stats)
588{
589 u16 fc = le16_to_cpu(rx_hdr->frame_control);
590 struct sk_buff *skb;
591 struct sk_buff_head *q;
592 unsigned long flags;
593
594 if ((fc & (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) !=
595 (IEEE80211_FTYPE_CTL | IEEE80211_STYPE_ACK))
596 return 0;
597
598 q = &zd_hw_mac(hw)->ack_wait_queue;
599 spin_lock_irqsave(&q->lock, flags);
600 for (skb = q->next; skb != (struct sk_buff *)q; skb = skb->next) {
601 struct ieee80211_hdr *tx_hdr;
602
603 tx_hdr = (struct ieee80211_hdr *)skb->data;
604 if (likely(!compare_ether_addr(tx_hdr->addr2, rx_hdr->addr1)))
605 {
606 struct ieee80211_tx_status status = {{0}};
607 status.flags = IEEE80211_TX_STATUS_ACK;
608 status.ack_signal = stats->ssi;
609 __skb_unlink(skb, q);
610 tx_status(hw, skb, &status, 1);
611 goto out;
612 }
613 }
614out:
615 spin_unlock_irqrestore(&q->lock, flags);
616 return 1;
617}
618
619int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
620{
621 struct zd_mac *mac = zd_hw_mac(hw);
622 struct ieee80211_rx_status stats;
623 const struct rx_status *status;
624 struct sk_buff *skb;
625 int bad_frame = 0;
Michael Buesch90817282007-12-29 17:24:23 +0100626 u16 fc;
627 bool is_qos, is_4addr, need_padding;
Daniel Drake459c51a2007-11-19 15:00:29 +0000628
629 if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
630 FCS_LEN + sizeof(struct rx_status))
Daniel Drakee85d0912006-06-02 17:11:32 +0100631 return -EINVAL;
632
Daniel Drake459c51a2007-11-19 15:00:29 +0000633 memset(&stats, 0, sizeof(stats));
Daniel Drakee85d0912006-06-02 17:11:32 +0100634
Daniel Drake459c51a2007-11-19 15:00:29 +0000635 /* Note about pass_failed_fcs and pass_ctrl access below:
636 * mac locking intentionally omitted here, as this is the only unlocked
637 * reader and the only writer is configure_filter. Plus, if there were
638 * any races accessing these variables, it wouldn't really matter.
639 * If mac80211 ever provides a way for us to access filter flags
640 * from outside configure_filter, we could improve on this. Also, this
641 * situation may change once we implement some kind of DMA-into-skb
642 * RX path. */
Daniel Drakee85d0912006-06-02 17:11:32 +0100643
Daniel Drake459c51a2007-11-19 15:00:29 +0000644 /* Caller has to ensure that length >= sizeof(struct rx_status). */
645 status = (struct rx_status *)
Ulrich Kunitz937a0492007-10-02 18:36:53 +0100646 (buffer + (length - sizeof(struct rx_status)));
Daniel Drakee85d0912006-06-02 17:11:32 +0100647 if (status->frame_status & ZD_RX_ERROR) {
Daniel Drake459c51a2007-11-19 15:00:29 +0000648 if (mac->pass_failed_fcs &&
649 (status->frame_status & ZD_RX_CRC32_ERROR)) {
650 stats.flag |= RX_FLAG_FAILED_FCS_CRC;
651 bad_frame = 1;
652 } else {
653 return -EINVAL;
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000654 }
Daniel Drakee85d0912006-06-02 17:11:32 +0100655 }
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000656
Daniel Drake459c51a2007-11-19 15:00:29 +0000657 stats.channel = _zd_chip_get_channel(&mac->chip);
658 stats.freq = zd_channels[stats.channel - 1].freq;
659 stats.phymode = MODE_IEEE80211G;
660 stats.ssi = status->signal_strength;
661 stats.signal = zd_rx_qual_percent(buffer,
Daniel Drakee85d0912006-06-02 17:11:32 +0100662 length - sizeof(struct rx_status),
663 status);
Daniel Drake459c51a2007-11-19 15:00:29 +0000664 stats.rate = zd_rx_rate(buffer, status);
Daniel Drakee85d0912006-06-02 17:11:32 +0100665
Daniel Drake459c51a2007-11-19 15:00:29 +0000666 length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
667 buffer += ZD_PLCP_HEADER_SIZE;
Daniel Drakee85d0912006-06-02 17:11:32 +0100668
Daniel Drake459c51a2007-11-19 15:00:29 +0000669 /* Except for bad frames, filter each frame to see if it is an ACK, in
670 * which case our internal TX tracking is updated. Normally we then
671 * bail here as there's no need to pass ACKs on up to the stack, but
672 * there is also the case where the stack has requested us to pass
673 * control frames on up (pass_ctrl) which we must consider. */
674 if (!bad_frame &&
675 filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
676 && !mac->pass_ctrl)
677 return 0;
Daniel Drakee85d0912006-06-02 17:11:32 +0100678
Michael Buesch90817282007-12-29 17:24:23 +0100679 fc = le16_to_cpu(*((__le16 *) buffer));
680
681 is_qos = ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
682 ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_QOS_DATA);
683 is_4addr = (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
684 (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS);
685 need_padding = is_qos ^ is_4addr;
686
687 skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
Daniel Drake459c51a2007-11-19 15:00:29 +0000688 if (skb == NULL)
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800689 return -ENOMEM;
Michael Buesch90817282007-12-29 17:24:23 +0100690 if (need_padding) {
691 /* Make sure the the payload data is 4 byte aligned. */
692 skb_reserve(skb, 2);
693 }
694
Daniel Drake459c51a2007-11-19 15:00:29 +0000695 memcpy(skb_put(skb, length), buffer, length);
696
697 ieee80211_rx_irqsafe(hw, skb, &stats);
Daniel Drakee85d0912006-06-02 17:11:32 +0100698 return 0;
699}
700
Daniel Drake459c51a2007-11-19 15:00:29 +0000701static int zd_op_add_interface(struct ieee80211_hw *hw,
702 struct ieee80211_if_init_conf *conf)
Daniel Drakee85d0912006-06-02 17:11:32 +0100703{
Daniel Drake459c51a2007-11-19 15:00:29 +0000704 struct zd_mac *mac = zd_hw_mac(hw);
705
706 /* using IEEE80211_IF_TYPE_INVALID to indicate no mode selected */
707 if (mac->type != IEEE80211_IF_TYPE_INVALID)
708 return -EOPNOTSUPP;
709
710 switch (conf->type) {
711 case IEEE80211_IF_TYPE_MNTR:
712 case IEEE80211_IF_TYPE_STA:
713 mac->type = conf->type;
714 break;
715 default:
716 return -EOPNOTSUPP;
717 }
718
719 return zd_write_mac_addr(&mac->chip, conf->mac_addr);
Daniel Drakee85d0912006-06-02 17:11:32 +0100720}
721
Daniel Drake459c51a2007-11-19 15:00:29 +0000722static void zd_op_remove_interface(struct ieee80211_hw *hw,
723 struct ieee80211_if_init_conf *conf)
Daniel Drakee85d0912006-06-02 17:11:32 +0100724{
Daniel Drake459c51a2007-11-19 15:00:29 +0000725 struct zd_mac *mac = zd_hw_mac(hw);
726 mac->type = IEEE80211_IF_TYPE_INVALID;
727 zd_write_mac_addr(&mac->chip, NULL);
Daniel Drakee85d0912006-06-02 17:11:32 +0100728}
729
Daniel Drake459c51a2007-11-19 15:00:29 +0000730static int zd_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Daniel Drakee85d0912006-06-02 17:11:32 +0100731{
Daniel Drake459c51a2007-11-19 15:00:29 +0000732 struct zd_mac *mac = zd_hw_mac(hw);
733 return zd_chip_set_channel(&mac->chip, conf->channel);
Daniel Drakee85d0912006-06-02 17:11:32 +0100734}
735
Johannes Berg32bfd352007-12-19 01:31:26 +0100736static int zd_op_config_interface(struct ieee80211_hw *hw,
737 struct ieee80211_vif *vif,
Daniel Drake459c51a2007-11-19 15:00:29 +0000738 struct ieee80211_if_conf *conf)
Daniel Drakee85d0912006-06-02 17:11:32 +0100739{
Daniel Drake459c51a2007-11-19 15:00:29 +0000740 struct zd_mac *mac = zd_hw_mac(hw);
Daniel Drakee85d0912006-06-02 17:11:32 +0100741
Daniel Drakee85d0912006-06-02 17:11:32 +0100742 spin_lock_irq(&mac->lock);
Daniel Drake459c51a2007-11-19 15:00:29 +0000743 mac->associated = is_valid_ether_addr(conf->bssid);
Daniel Drakee85d0912006-06-02 17:11:32 +0100744 spin_unlock_irq(&mac->lock);
Daniel Drake459c51a2007-11-19 15:00:29 +0000745
746 /* TODO: do hardware bssid filtering */
747 return 0;
748}
749
750static void set_multicast_hash_handler(struct work_struct *work)
751{
752 struct zd_mac *mac =
753 container_of(work, struct zd_mac, set_multicast_hash_work);
754 struct zd_mc_hash hash;
755
756 spin_lock_irq(&mac->lock);
757 hash = mac->multicast_hash;
758 spin_unlock_irq(&mac->lock);
759
760 zd_chip_set_multicast_hash(&mac->chip, &hash);
761}
762
763static void set_rx_filter_handler(struct work_struct *work)
764{
765 struct zd_mac *mac =
766 container_of(work, struct zd_mac, set_rx_filter_work);
767 int r;
768
769 dev_dbg_f(zd_mac_dev(mac), "\n");
770 r = set_rx_filter(mac);
771 if (r)
772 dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r);
773}
774
775#define SUPPORTED_FIF_FLAGS \
776 (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
777 FIF_OTHER_BSS)
778static void zd_op_configure_filter(struct ieee80211_hw *hw,
779 unsigned int changed_flags,
780 unsigned int *new_flags,
781 int mc_count, struct dev_mc_list *mclist)
782{
783 struct zd_mc_hash hash;
784 struct zd_mac *mac = zd_hw_mac(hw);
785 unsigned long flags;
786 int i;
787
788 /* Only deal with supported flags */
789 changed_flags &= SUPPORTED_FIF_FLAGS;
790 *new_flags &= SUPPORTED_FIF_FLAGS;
791
792 /* changed_flags is always populated but this driver
793 * doesn't support all FIF flags so its possible we don't
794 * need to do anything */
795 if (!changed_flags)
796 return;
797
798 if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI)) {
799 zd_mc_add_all(&hash);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +0100800 } else {
Daniel Drake459c51a2007-11-19 15:00:29 +0000801 DECLARE_MAC_BUF(macbuf);
802
803 zd_mc_clear(&hash);
804 for (i = 0; i < mc_count; i++) {
805 if (!mclist)
806 break;
807 dev_dbg_f(zd_mac_dev(mac), "mc addr %s\n",
808 print_mac(macbuf, mclist->dmi_addr));
809 zd_mc_add_addr(&hash, mclist->dmi_addr);
810 mclist = mclist->next;
811 }
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +0100812 }
Daniel Drake459c51a2007-11-19 15:00:29 +0000813
814 spin_lock_irqsave(&mac->lock, flags);
815 mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
816 mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
817 mac->multicast_hash = hash;
818 spin_unlock_irqrestore(&mac->lock, flags);
819 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
820
821 if (changed_flags & FIF_CONTROL)
822 queue_work(zd_workqueue, &mac->set_rx_filter_work);
823
824 /* no handling required for FIF_OTHER_BSS as we don't currently
825 * do BSSID filtering */
826 /* FIXME: in future it would be nice to enable the probe response
827 * filter (so that the driver doesn't see them) until
828 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
829 * have to schedule work to enable prbresp reception, which might
830 * happen too late. For now we'll just listen and forward them all the
831 * time. */
832}
833
834static void set_rts_cts_work(struct work_struct *work)
835{
836 struct zd_mac *mac =
837 container_of(work, struct zd_mac, set_rts_cts_work);
838 unsigned long flags;
839 unsigned int short_preamble;
840
841 mutex_lock(&mac->chip.mutex);
842
843 spin_lock_irqsave(&mac->lock, flags);
844 mac->updating_rts_rate = 0;
845 short_preamble = mac->short_preamble;
846 spin_unlock_irqrestore(&mac->lock, flags);
847
848 zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
849 mutex_unlock(&mac->chip.mutex);
850}
851
852static void zd_op_erp_ie_changed(struct ieee80211_hw *hw, u8 changes,
853 int cts_protection, int preamble)
854{
855 struct zd_mac *mac = zd_hw_mac(hw);
856 unsigned long flags;
857
858 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
859
860 if (changes & IEEE80211_ERP_CHANGE_PREAMBLE) {
861 spin_lock_irqsave(&mac->lock, flags);
862 mac->short_preamble = !preamble;
863 if (!mac->updating_rts_rate) {
864 mac->updating_rts_rate = 1;
865 /* FIXME: should disable TX here, until work has
866 * completed and RTS_CTS reg is updated */
867 queue_work(zd_workqueue, &mac->set_rts_cts_work);
868 }
869 spin_unlock_irqrestore(&mac->lock, flags);
870 }
871}
872
873static const struct ieee80211_ops zd_ops = {
874 .tx = zd_op_tx,
875 .start = zd_op_start,
876 .stop = zd_op_stop,
877 .add_interface = zd_op_add_interface,
878 .remove_interface = zd_op_remove_interface,
879 .config = zd_op_config,
880 .config_interface = zd_op_config_interface,
881 .configure_filter = zd_op_configure_filter,
882 .erp_ie_changed = zd_op_erp_ie_changed,
883};
884
885struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
886{
887 struct zd_mac *mac;
888 struct ieee80211_hw *hw;
889 int i;
890
891 hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
892 if (!hw) {
893 dev_dbg_f(&intf->dev, "out of memory\n");
894 return NULL;
895 }
896
897 mac = zd_hw_mac(hw);
898
899 memset(mac, 0, sizeof(*mac));
900 spin_lock_init(&mac->lock);
901 mac->hw = hw;
902
903 mac->type = IEEE80211_IF_TYPE_INVALID;
904
905 memcpy(mac->channels, zd_channels, sizeof(zd_channels));
906 memcpy(mac->rates, zd_rates, sizeof(zd_rates));
907 mac->modes[0].mode = MODE_IEEE80211G;
908 mac->modes[0].num_rates = ARRAY_SIZE(zd_rates);
909 mac->modes[0].rates = mac->rates;
910 mac->modes[0].num_channels = ARRAY_SIZE(zd_channels);
911 mac->modes[0].channels = mac->channels;
912 mac->modes[1].mode = MODE_IEEE80211B;
913 mac->modes[1].num_rates = 4;
914 mac->modes[1].rates = mac->rates;
915 mac->modes[1].num_channels = ARRAY_SIZE(zd_channels);
916 mac->modes[1].channels = mac->channels;
917
918 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
919 IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED;
920 hw->max_rssi = 100;
921 hw->max_signal = 100;
922
923 hw->queues = 1;
924 hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
925
926 skb_queue_head_init(&mac->ack_wait_queue);
927
928 for (i = 0; i < 2; i++) {
929 if (ieee80211_register_hwmode(hw, &mac->modes[i])) {
930 dev_dbg_f(&intf->dev, "cannot register hwmode\n");
931 ieee80211_free_hw(hw);
932 return NULL;
933 }
934 }
935
936 zd_chip_init(&mac->chip, hw, intf);
937 housekeeping_init(mac);
938 INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
939 INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
940 INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler);
941
942 SET_IEEE80211_DEV(hw, &intf->dev);
943 return hw;
Daniel Drakee85d0912006-06-02 17:11:32 +0100944}
945
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100946#define LINK_LED_WORK_DELAY HZ
947
David Howellsc4028952006-11-22 14:57:56 +0000948static void link_led_handler(struct work_struct *work)
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100949{
David Howellsc4028952006-11-22 14:57:56 +0000950 struct zd_mac *mac =
951 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100952 struct zd_chip *chip = &mac->chip;
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100953 int is_associated;
954 int r;
955
956 spin_lock_irq(&mac->lock);
Daniel Drake459c51a2007-11-19 15:00:29 +0000957 is_associated = mac->associated;
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100958 spin_unlock_irq(&mac->lock);
959
960 r = zd_chip_control_leds(chip,
961 is_associated ? LED_ASSOCIATED : LED_SCANNING);
962 if (r)
Daniel Drake459c51a2007-11-19 15:00:29 +0000963 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100964
965 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
966 LINK_LED_WORK_DELAY);
967}
968
969static void housekeeping_init(struct zd_mac *mac)
970{
David Howellsc4028952006-11-22 14:57:56 +0000971 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100972}
973
974static void housekeeping_enable(struct zd_mac *mac)
975{
976 dev_dbg_f(zd_mac_dev(mac), "\n");
977 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
978 0);
979}
980
981static void housekeeping_disable(struct zd_mac *mac)
982{
983 dev_dbg_f(zd_mac_dev(mac), "\n");
984 cancel_rearming_delayed_workqueue(zd_workqueue,
985 &mac->housekeeping.link_led_work);
986 zd_chip_control_leds(&mac->chip, LED_OFF);
987}