blob: 451308d7095d2b2a84d105b8bd736817221ed6e6 [file] [log] [blame]
Daniel Drakee85d0912006-06-02 17:11:32 +01001/* zd_mac.c
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18#include <linux/netdevice.h>
19#include <linux/etherdevice.h>
20#include <linux/wireless.h>
21#include <linux/usb.h>
22#include <linux/jiffies.h>
23#include <net/ieee80211_radiotap.h>
24
25#include "zd_def.h"
26#include "zd_chip.h"
27#include "zd_mac.h"
28#include "zd_ieee80211.h"
29#include "zd_netdev.h"
30#include "zd_rf.h"
31#include "zd_util.h"
32
33static void ieee_init(struct ieee80211_device *ieee);
34static void softmac_init(struct ieee80211softmac_device *sm);
David Howells6d5aefb2006-12-05 19:36:26 +000035static void set_rts_cts_work(struct work_struct *work);
36static void set_basic_rates_work(struct work_struct *work);
Daniel Drakee85d0912006-06-02 17:11:32 +010037
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +010038static void housekeeping_init(struct zd_mac *mac);
39static void housekeeping_enable(struct zd_mac *mac);
40static void housekeeping_disable(struct zd_mac *mac);
41
Jeff Garzik0ae85132006-12-07 06:30:30 -050042static void set_multicast_hash_handler(struct work_struct *work);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +000043
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -080044static void do_rx(unsigned long mac_ptr);
45
Daniel Drakee85d0912006-06-02 17:11:32 +010046int zd_mac_init(struct zd_mac *mac,
47 struct net_device *netdev,
48 struct usb_interface *intf)
49{
50 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
51
52 memset(mac, 0, sizeof(*mac));
53 spin_lock_init(&mac->lock);
54 mac->netdev = netdev;
David Howells6d5aefb2006-12-05 19:36:26 +000055 INIT_DELAYED_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
56 INIT_DELAYED_WORK(&mac->set_basic_rates_work, set_basic_rates_work);
Daniel Drakee85d0912006-06-02 17:11:32 +010057
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -080058 skb_queue_head_init(&mac->rx_queue);
59 tasklet_init(&mac->rx_tasklet, do_rx, (unsigned long)mac);
60 tasklet_disable(&mac->rx_tasklet);
61
Daniel Drakee85d0912006-06-02 17:11:32 +010062 ieee_init(ieee);
63 softmac_init(ieee80211_priv(netdev));
64 zd_chip_init(&mac->chip, netdev, intf);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +010065 housekeeping_init(mac);
Jeff Garzik0ae85132006-12-07 06:30:30 -050066 INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
Daniel Drakee85d0912006-06-02 17:11:32 +010067 return 0;
68}
69
70static int reset_channel(struct zd_mac *mac)
71{
72 int r;
73 unsigned long flags;
74 const struct channel_range *range;
75
76 spin_lock_irqsave(&mac->lock, flags);
77 range = zd_channel_range(mac->regdomain);
78 if (!range->start) {
79 r = -EINVAL;
80 goto out;
81 }
82 mac->requested_channel = range->start;
83 r = 0;
84out:
85 spin_unlock_irqrestore(&mac->lock, flags);
86 return r;
87}
88
Daniel Drake74553ae2007-07-01 18:22:32 +010089int zd_mac_preinit_hw(struct zd_mac *mac)
90{
91 int r;
92 u8 addr[ETH_ALEN];
93
94 r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
95 if (r)
96 return r;
97
98 memcpy(mac->netdev->dev_addr, addr, ETH_ALEN);
99 return 0;
100}
101
102int zd_mac_init_hw(struct zd_mac *mac)
Daniel Drakee85d0912006-06-02 17:11:32 +0100103{
104 int r;
105 struct zd_chip *chip = &mac->chip;
Daniel Drakee85d0912006-06-02 17:11:32 +0100106 u8 default_regdomain;
107
108 r = zd_chip_enable_int(chip);
109 if (r)
110 goto out;
Daniel Drake74553ae2007-07-01 18:22:32 +0100111 r = zd_chip_init_hw(chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100112 if (r)
113 goto disable_int;
114
Daniel Drakee85d0912006-06-02 17:11:32 +0100115 ZD_ASSERT(!irqs_disabled());
Daniel Drakee85d0912006-06-02 17:11:32 +0100116
117 r = zd_read_regdomain(chip, &default_regdomain);
118 if (r)
119 goto disable_int;
120 if (!zd_regdomain_supported(default_regdomain)) {
Daniel Drake86d95c212007-07-01 18:21:49 +0100121 /* The vendor driver overrides the regulatory domain and
122 * allowed channel registers and unconditionally restricts
123 * available channels to 1-11 everywhere. Match their
124 * questionable behaviour only for regdomains which we don't
125 * recognise. */
126 dev_warn(zd_mac_dev(mac), "Unrecognised regulatory domain: "
127 "%#04x. Defaulting to FCC.\n", default_regdomain);
128 default_regdomain = ZD_REGDOMAIN_FCC;
Daniel Drakee85d0912006-06-02 17:11:32 +0100129 }
130 spin_lock_irq(&mac->lock);
131 mac->regdomain = mac->default_regdomain = default_regdomain;
132 spin_unlock_irq(&mac->lock);
133 r = reset_channel(mac);
134 if (r)
135 goto disable_int;
136
Daniel Drake40da08b2006-08-01 23:43:32 +0200137 /* We must inform the device that we are doing encryption/decryption in
138 * software at the moment. */
139 r = zd_set_encryption_type(chip, ENC_SNIFFER);
Daniel Drakee85d0912006-06-02 17:11:32 +0100140 if (r)
141 goto disable_int;
142
143 r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
144 if (r)
145 goto disable_int;
146
147 r = 0;
148disable_int:
149 zd_chip_disable_int(chip);
150out:
151 return r;
152}
153
154void zd_mac_clear(struct zd_mac *mac)
155{
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000156 flush_workqueue(zd_workqueue);
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800157 skb_queue_purge(&mac->rx_queue);
158 tasklet_kill(&mac->rx_tasklet);
Daniel Drakee85d0912006-06-02 17:11:32 +0100159 zd_chip_clear(&mac->chip);
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100160 ZD_ASSERT(!spin_is_locked(&mac->lock));
161 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
Daniel Drakee85d0912006-06-02 17:11:32 +0100162}
163
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100164static int set_rx_filter(struct zd_mac *mac)
Daniel Drakee85d0912006-06-02 17:11:32 +0100165{
166 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
Daniel Drake44713b12007-03-26 00:59:47 +0100167 u32 filter = (ieee->iw_mode == IW_MODE_MONITOR) ? ~0 : STA_RX_FILTER;
168 return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
Daniel Drakee85d0912006-06-02 17:11:32 +0100169}
170
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100171static int set_sniffer(struct zd_mac *mac)
172{
173 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
174 return zd_iowrite32(&mac->chip, CR_SNIFFER_ON,
175 ieee->iw_mode == IW_MODE_MONITOR ? 1 : 0);
176 return 0;
177}
178
179static int set_mc_hash(struct zd_mac *mac)
180{
181 struct zd_mc_hash hash;
182 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
183
184 zd_mc_clear(&hash);
185 if (ieee->iw_mode == IW_MODE_MONITOR)
186 zd_mc_add_all(&hash);
187
188 return zd_chip_set_multicast_hash(&mac->chip, &hash);
189}
190
Daniel Drakee85d0912006-06-02 17:11:32 +0100191int zd_mac_open(struct net_device *netdev)
192{
193 struct zd_mac *mac = zd_netdev_mac(netdev);
194 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
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800204 tasklet_enable(&mac->rx_tasklet);
205
Daniel Drakee85d0912006-06-02 17:11:32 +0100206 r = zd_chip_enable_int(chip);
207 if (r < 0)
208 goto out;
209
Daniel Drake74553ae2007-07-01 18:22:32 +0100210 r = zd_write_mac_addr(chip, netdev->dev_addr);
211 if (r)
212 goto disable_int;
213
Daniel Drakee85d0912006-06-02 17:11:32 +0100214 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
215 if (r < 0)
216 goto disable_int;
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100217 r = set_rx_filter(mac);
218 if (r)
219 goto disable_int;
220 r = set_sniffer(mac);
221 if (r)
222 goto disable_int;
223 r = set_mc_hash(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100224 if (r)
225 goto disable_int;
226 r = zd_chip_switch_radio_on(chip);
227 if (r < 0)
228 goto disable_int;
229 r = zd_chip_set_channel(chip, mac->requested_channel);
230 if (r < 0)
231 goto disable_radio;
232 r = zd_chip_enable_rx(chip);
233 if (r < 0)
234 goto disable_radio;
235 r = zd_chip_enable_hwint(chip);
236 if (r < 0)
237 goto disable_rx;
238
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100239 housekeeping_enable(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100240 ieee80211softmac_start(netdev);
241 return 0;
242disable_rx:
243 zd_chip_disable_rx(chip);
244disable_radio:
245 zd_chip_switch_radio_off(chip);
246disable_int:
247 zd_chip_disable_int(chip);
248out:
249 return r;
250}
251
252int zd_mac_stop(struct net_device *netdev)
253{
254 struct zd_mac *mac = zd_netdev_mac(netdev);
255 struct zd_chip *chip = &mac->chip;
256
Daniel Drakec9a4b352006-06-11 23:18:54 +0100257 netif_stop_queue(netdev);
258
Daniel Drakee85d0912006-06-02 17:11:32 +0100259 /*
260 * The order here deliberately is a little different from the open()
261 * method, since we need to make sure there is no opportunity for RX
262 * frames to be processed by softmac after we have stopped it.
263 */
264
265 zd_chip_disable_rx(chip);
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800266 skb_queue_purge(&mac->rx_queue);
267 tasklet_disable(&mac->rx_tasklet);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100268 housekeeping_disable(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100269 ieee80211softmac_stop(netdev);
270
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000271 /* Ensure no work items are running or queued from this point */
272 cancel_delayed_work(&mac->set_rts_cts_work);
273 cancel_delayed_work(&mac->set_basic_rates_work);
274 flush_workqueue(zd_workqueue);
275 mac->updating_rts_rate = 0;
276 mac->updating_basic_rates = 0;
277
Daniel Drakee85d0912006-06-02 17:11:32 +0100278 zd_chip_disable_hwint(chip);
279 zd_chip_switch_radio_off(chip);
280 zd_chip_disable_int(chip);
281
282 return 0;
283}
284
285int zd_mac_set_mac_address(struct net_device *netdev, void *p)
286{
287 int r;
288 unsigned long flags;
289 struct sockaddr *addr = p;
290 struct zd_mac *mac = zd_netdev_mac(netdev);
291 struct zd_chip *chip = &mac->chip;
292
293 if (!is_valid_ether_addr(addr->sa_data))
294 return -EADDRNOTAVAIL;
295
296 dev_dbg_f(zd_mac_dev(mac),
297 "Setting MAC to " MAC_FMT "\n", MAC_ARG(addr->sa_data));
298
Daniel Drake74553ae2007-07-01 18:22:32 +0100299 if (netdev->flags & IFF_UP) {
300 r = zd_write_mac_addr(chip, addr->sa_data);
301 if (r)
302 return r;
303 }
Daniel Drakee85d0912006-06-02 17:11:32 +0100304
305 spin_lock_irqsave(&mac->lock, flags);
306 memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
307 spin_unlock_irqrestore(&mac->lock, flags);
308
309 return 0;
310}
311
Jeff Garzik0ae85132006-12-07 06:30:30 -0500312static void set_multicast_hash_handler(struct work_struct *work)
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000313{
Jeff Garzik0ae85132006-12-07 06:30:30 -0500314 struct zd_mac *mac = container_of(work, struct zd_mac,
315 set_multicast_hash_work);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000316 struct zd_mc_hash hash;
317
318 spin_lock_irq(&mac->lock);
319 hash = mac->multicast_hash;
320 spin_unlock_irq(&mac->lock);
321
322 zd_chip_set_multicast_hash(&mac->chip, &hash);
323}
324
325void zd_mac_set_multicast_list(struct net_device *dev)
326{
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000327 struct zd_mac *mac = zd_netdev_mac(dev);
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100328 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
329 struct zd_mc_hash hash;
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000330 struct dev_mc_list *mc;
331 unsigned long flags;
332
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100333 if (dev->flags & (IFF_PROMISC|IFF_ALLMULTI) ||
334 ieee->iw_mode == IW_MODE_MONITOR) {
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000335 zd_mc_add_all(&hash);
336 } else {
337 zd_mc_clear(&hash);
338 for (mc = dev->mc_list; mc; mc = mc->next) {
339 dev_dbg_f(zd_mac_dev(mac), "mc addr " MAC_FMT "\n",
340 MAC_ARG(mc->dmi_addr));
341 zd_mc_add_addr(&hash, mc->dmi_addr);
342 }
343 }
344
345 spin_lock_irqsave(&mac->lock, flags);
346 mac->multicast_hash = hash;
347 spin_unlock_irqrestore(&mac->lock, flags);
348 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
349}
350
Daniel Drakee85d0912006-06-02 17:11:32 +0100351int zd_mac_set_regdomain(struct zd_mac *mac, u8 regdomain)
352{
353 int r;
354 u8 channel;
355
356 ZD_ASSERT(!irqs_disabled());
357 spin_lock_irq(&mac->lock);
358 if (regdomain == 0) {
359 regdomain = mac->default_regdomain;
360 }
361 if (!zd_regdomain_supported(regdomain)) {
362 spin_unlock_irq(&mac->lock);
363 return -EINVAL;
364 }
365 mac->regdomain = regdomain;
366 channel = mac->requested_channel;
367 spin_unlock_irq(&mac->lock);
368
369 r = zd_geo_init(zd_mac_to_ieee80211(mac), regdomain);
370 if (r)
371 return r;
372 if (!zd_regdomain_supports_channel(regdomain, channel)) {
373 r = reset_channel(mac);
374 if (r)
375 return r;
376 }
377
378 return 0;
379}
380
381u8 zd_mac_get_regdomain(struct zd_mac *mac)
382{
383 unsigned long flags;
384 u8 regdomain;
385
386 spin_lock_irqsave(&mac->lock, flags);
387 regdomain = mac->regdomain;
388 spin_unlock_irqrestore(&mac->lock, flags);
389 return regdomain;
390}
391
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000392/* Fallback to lowest rate, if rate is unknown. */
393static u8 rate_to_zd_rate(u8 rate)
394{
395 switch (rate) {
396 case IEEE80211_CCK_RATE_2MB:
397 return ZD_CCK_RATE_2M;
398 case IEEE80211_CCK_RATE_5MB:
399 return ZD_CCK_RATE_5_5M;
400 case IEEE80211_CCK_RATE_11MB:
401 return ZD_CCK_RATE_11M;
402 case IEEE80211_OFDM_RATE_6MB:
403 return ZD_OFDM_RATE_6M;
404 case IEEE80211_OFDM_RATE_9MB:
405 return ZD_OFDM_RATE_9M;
406 case IEEE80211_OFDM_RATE_12MB:
407 return ZD_OFDM_RATE_12M;
408 case IEEE80211_OFDM_RATE_18MB:
409 return ZD_OFDM_RATE_18M;
410 case IEEE80211_OFDM_RATE_24MB:
411 return ZD_OFDM_RATE_24M;
412 case IEEE80211_OFDM_RATE_36MB:
413 return ZD_OFDM_RATE_36M;
414 case IEEE80211_OFDM_RATE_48MB:
415 return ZD_OFDM_RATE_48M;
416 case IEEE80211_OFDM_RATE_54MB:
417 return ZD_OFDM_RATE_54M;
418 }
419 return ZD_CCK_RATE_1M;
420}
421
422static u16 rate_to_cr_rate(u8 rate)
423{
424 switch (rate) {
425 case IEEE80211_CCK_RATE_2MB:
426 return CR_RATE_1M;
427 case IEEE80211_CCK_RATE_5MB:
428 return CR_RATE_5_5M;
429 case IEEE80211_CCK_RATE_11MB:
430 return CR_RATE_11M;
431 case IEEE80211_OFDM_RATE_6MB:
432 return CR_RATE_6M;
433 case IEEE80211_OFDM_RATE_9MB:
434 return CR_RATE_9M;
435 case IEEE80211_OFDM_RATE_12MB:
436 return CR_RATE_12M;
437 case IEEE80211_OFDM_RATE_18MB:
438 return CR_RATE_18M;
439 case IEEE80211_OFDM_RATE_24MB:
440 return CR_RATE_24M;
441 case IEEE80211_OFDM_RATE_36MB:
442 return CR_RATE_36M;
443 case IEEE80211_OFDM_RATE_48MB:
444 return CR_RATE_48M;
445 case IEEE80211_OFDM_RATE_54MB:
446 return CR_RATE_54M;
447 }
448 return CR_RATE_1M;
449}
450
451static void try_enable_tx(struct zd_mac *mac)
452{
453 unsigned long flags;
454
455 spin_lock_irqsave(&mac->lock, flags);
456 if (mac->updating_rts_rate == 0 && mac->updating_basic_rates == 0)
457 netif_wake_queue(mac->netdev);
458 spin_unlock_irqrestore(&mac->lock, flags);
459}
460
David Howells6d5aefb2006-12-05 19:36:26 +0000461static void set_rts_cts_work(struct work_struct *work)
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000462{
David Howells6d5aefb2006-12-05 19:36:26 +0000463 struct zd_mac *mac =
464 container_of(work, struct zd_mac, set_rts_cts_work.work);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000465 unsigned long flags;
466 u8 rts_rate;
467 unsigned int short_preamble;
468
469 mutex_lock(&mac->chip.mutex);
470
471 spin_lock_irqsave(&mac->lock, flags);
472 mac->updating_rts_rate = 0;
473 rts_rate = mac->rts_rate;
474 short_preamble = mac->short_preamble;
475 spin_unlock_irqrestore(&mac->lock, flags);
476
477 zd_chip_set_rts_cts_rate_locked(&mac->chip, rts_rate, short_preamble);
478 mutex_unlock(&mac->chip.mutex);
479
480 try_enable_tx(mac);
481}
482
David Howells6d5aefb2006-12-05 19:36:26 +0000483static void set_basic_rates_work(struct work_struct *work)
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000484{
David Howells6d5aefb2006-12-05 19:36:26 +0000485 struct zd_mac *mac =
486 container_of(work, struct zd_mac, set_basic_rates_work.work);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000487 unsigned long flags;
488 u16 basic_rates;
489
490 mutex_lock(&mac->chip.mutex);
491
492 spin_lock_irqsave(&mac->lock, flags);
493 mac->updating_basic_rates = 0;
494 basic_rates = mac->basic_rates;
495 spin_unlock_irqrestore(&mac->lock, flags);
496
497 zd_chip_set_basic_rates_locked(&mac->chip, basic_rates);
498 mutex_unlock(&mac->chip.mutex);
499
500 try_enable_tx(mac);
501}
502
503static void bssinfo_change(struct net_device *netdev, u32 changes)
504{
505 struct zd_mac *mac = zd_netdev_mac(netdev);
506 struct ieee80211softmac_device *softmac = ieee80211_priv(netdev);
507 struct ieee80211softmac_bss_info *bssinfo = &softmac->bssinfo;
508 int need_set_rts_cts = 0;
509 int need_set_rates = 0;
510 u16 basic_rates;
511 unsigned long flags;
512
513 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
514
515 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_SHORT_PREAMBLE) {
516 spin_lock_irqsave(&mac->lock, flags);
517 mac->short_preamble = bssinfo->short_preamble;
518 spin_unlock_irqrestore(&mac->lock, flags);
519 need_set_rts_cts = 1;
520 }
521
522 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_RATES) {
523 /* Set RTS rate to highest available basic rate */
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800524 u8 hi_rate = ieee80211softmac_highest_supported_rate(softmac,
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000525 &bssinfo->supported_rates, 1);
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800526 hi_rate = rate_to_zd_rate(hi_rate);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000527
528 spin_lock_irqsave(&mac->lock, flags);
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800529 if (hi_rate != mac->rts_rate) {
530 mac->rts_rate = hi_rate;
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000531 need_set_rts_cts = 1;
532 }
533 spin_unlock_irqrestore(&mac->lock, flags);
534
535 /* Set basic rates */
536 need_set_rates = 1;
537 if (bssinfo->supported_rates.count == 0) {
538 /* Allow the device to be flexible */
539 basic_rates = CR_RATES_80211B | CR_RATES_80211G;
540 } else {
541 int i = 0;
542 basic_rates = 0;
543
544 for (i = 0; i < bssinfo->supported_rates.count; i++) {
545 u16 rate = bssinfo->supported_rates.rates[i];
546 if ((rate & IEEE80211_BASIC_RATE_MASK) == 0)
547 continue;
548
549 rate &= ~IEEE80211_BASIC_RATE_MASK;
550 basic_rates |= rate_to_cr_rate(rate);
551 }
552 }
553 spin_lock_irqsave(&mac->lock, flags);
554 mac->basic_rates = basic_rates;
555 spin_unlock_irqrestore(&mac->lock, flags);
556 }
557
558 /* Schedule any changes we made above */
559
560 spin_lock_irqsave(&mac->lock, flags);
561 if (need_set_rts_cts && !mac->updating_rts_rate) {
562 mac->updating_rts_rate = 1;
563 netif_stop_queue(mac->netdev);
David Howells6d5aefb2006-12-05 19:36:26 +0000564 queue_delayed_work(zd_workqueue, &mac->set_rts_cts_work, 0);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000565 }
566 if (need_set_rates && !mac->updating_basic_rates) {
567 mac->updating_basic_rates = 1;
568 netif_stop_queue(mac->netdev);
David Howells6d5aefb2006-12-05 19:36:26 +0000569 queue_delayed_work(zd_workqueue, &mac->set_basic_rates_work,
570 0);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000571 }
572 spin_unlock_irqrestore(&mac->lock, flags);
573}
574
Daniel Drakee85d0912006-06-02 17:11:32 +0100575static void set_channel(struct net_device *netdev, u8 channel)
576{
577 struct zd_mac *mac = zd_netdev_mac(netdev);
578
579 dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
580
581 zd_chip_set_channel(&mac->chip, channel);
582}
583
Daniel Drakee85d0912006-06-02 17:11:32 +0100584int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
585{
586 unsigned long lock_flags;
587 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
588
589 if (ieee->iw_mode == IW_MODE_INFRA)
590 return -EPERM;
591
592 spin_lock_irqsave(&mac->lock, lock_flags);
593 if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
594 spin_unlock_irqrestore(&mac->lock, lock_flags);
595 return -EINVAL;
596 }
597 mac->requested_channel = channel;
598 spin_unlock_irqrestore(&mac->lock, lock_flags);
599 if (netif_running(mac->netdev))
600 return zd_chip_set_channel(&mac->chip, channel);
601 else
602 return 0;
603}
604
Daniel Drake84bc7152006-11-22 00:05:30 +0000605u8 zd_mac_get_channel(struct zd_mac *mac)
Daniel Drakee85d0912006-06-02 17:11:32 +0100606{
Daniel Drake84bc7152006-11-22 00:05:30 +0000607 u8 channel = zd_chip_get_channel(&mac->chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100608
Daniel Drake84bc7152006-11-22 00:05:30 +0000609 dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
610 return channel;
Daniel Drakee85d0912006-06-02 17:11:32 +0100611}
612
Daniel Drakee85d0912006-06-02 17:11:32 +0100613int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
614{
615 struct ieee80211_device *ieee;
616
617 switch (mode) {
618 case IW_MODE_AUTO:
619 case IW_MODE_ADHOC:
620 case IW_MODE_INFRA:
621 mac->netdev->type = ARPHRD_ETHER;
622 break;
623 case IW_MODE_MONITOR:
624 mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
625 break;
626 default:
627 dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
628 return -EINVAL;
629 }
630
631 ieee = zd_mac_to_ieee80211(mac);
632 ZD_ASSERT(!irqs_disabled());
633 spin_lock_irq(&ieee->lock);
634 ieee->iw_mode = mode;
635 spin_unlock_irq(&ieee->lock);
636
Ulrich Kunitzc5691232007-07-21 22:42:13 +0100637 if (netif_running(mac->netdev)) {
638 int r = set_rx_filter(mac);
639 if (r)
640 return r;
641 return set_sniffer(mac);
642 }
Daniel Drakee85d0912006-06-02 17:11:32 +0100643
644 return 0;
645}
646
647int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
648{
649 unsigned long flags;
650 struct ieee80211_device *ieee;
651
652 ieee = zd_mac_to_ieee80211(mac);
653 spin_lock_irqsave(&ieee->lock, flags);
654 *mode = ieee->iw_mode;
655 spin_unlock_irqrestore(&ieee->lock, flags);
656 return 0;
657}
658
659int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
660{
661 int i;
662 const struct channel_range *channel_range;
663 u8 regdomain;
664
665 memset(range, 0, sizeof(*range));
666
667 /* FIXME: Not so important and depends on the mode. For 802.11g
668 * usually this value is used. It seems to be that Bit/s number is
669 * given here.
670 */
671 range->throughput = 27 * 1000 * 1000;
672
673 range->max_qual.qual = 100;
674 range->max_qual.level = 100;
675
676 /* FIXME: Needs still to be tuned. */
677 range->avg_qual.qual = 71;
678 range->avg_qual.level = 80;
679
680 /* FIXME: depends on standard? */
681 range->min_rts = 256;
682 range->max_rts = 2346;
683
684 range->min_frag = MIN_FRAG_THRESHOLD;
685 range->max_frag = MAX_FRAG_THRESHOLD;
686
687 range->max_encoding_tokens = WEP_KEYS;
688 range->num_encoding_sizes = 2;
689 range->encoding_size[0] = 5;
690 range->encoding_size[1] = WEP_KEY_LEN;
691
692 range->we_version_compiled = WIRELESS_EXT;
693 range->we_version_source = 20;
694
Daniel Drakeff9b99b2006-12-01 00:57:11 +0000695 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
696 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
697
Daniel Drakee85d0912006-06-02 17:11:32 +0100698 ZD_ASSERT(!irqs_disabled());
699 spin_lock_irq(&mac->lock);
700 regdomain = mac->regdomain;
701 spin_unlock_irq(&mac->lock);
702 channel_range = zd_channel_range(regdomain);
703
704 range->num_channels = channel_range->end - channel_range->start;
705 range->old_num_channels = range->num_channels;
706 range->num_frequency = range->num_channels;
707 range->old_num_frequency = range->num_frequency;
708
709 for (i = 0; i < range->num_frequency; i++) {
710 struct iw_freq *freq = &range->freq[i];
711 freq->i = channel_range->start + i;
712 zd_channel_to_freq(freq, freq->i);
713 }
714
715 return 0;
716}
717
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000718static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
Daniel Drakee85d0912006-06-02 17:11:32 +0100719{
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100720 /* ZD_PURE_RATE() must be used to remove the modulation type flag of
721 * the zd-rate values. */
Daniel Drakee85d0912006-06-02 17:11:32 +0100722 static const u8 rate_divisor[] = {
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100723 [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
724 [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
725
726 /* bits must be doubled */
727 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
728
729 [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
730 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
731 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
732 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
733 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
734 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
735 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
736 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
737 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
Daniel Drakee85d0912006-06-02 17:11:32 +0100738 };
739
740 u32 bits = (u32)tx_length * 8;
741 u32 divisor;
742
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100743 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
Daniel Drakee85d0912006-06-02 17:11:32 +0100744 if (divisor == 0)
745 return -EINVAL;
746
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000747 switch (zd_rate) {
748 case ZD_CCK_RATE_5_5M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100749 bits = (2*bits) + 10; /* round up to the next integer */
750 break;
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000751 case ZD_CCK_RATE_11M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100752 if (service) {
753 u32 t = bits % 11;
754 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
755 if (0 < t && t <= 3) {
756 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
757 }
758 }
759 bits += 10; /* round up to the next integer */
760 break;
761 }
762
763 return bits/divisor;
764}
765
Daniel Drakee85d0912006-06-02 17:11:32 +0100766static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
767 struct ieee80211_hdr_4addr *hdr)
768{
769 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
770 u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100771 u8 rate;
Daniel Drakee85d0912006-06-02 17:11:32 +0100772 int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000773 int is_multicast = is_multicast_ether_addr(hdr->addr1);
774 int short_preamble = ieee80211softmac_short_preamble_ok(softmac,
775 is_multicast, is_mgt);
Daniel Drakee85d0912006-06-02 17:11:32 +0100776
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000777 rate = ieee80211softmac_suggest_txrate(softmac, is_multicast, is_mgt);
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100778 cs->modulation = rate_to_zd_rate(rate);
Daniel Drakee85d0912006-06-02 17:11:32 +0100779
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100780 /* Set short preamble bit when appropriate */
781 if (short_preamble && ZD_MODULATION_TYPE(cs->modulation) == ZD_CCK
782 && cs->modulation != ZD_CCK_RATE_1M)
783 cs->modulation |= ZD_CCK_PREA_SHORT;
Daniel Drakee85d0912006-06-02 17:11:32 +0100784}
785
786static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
787 struct ieee80211_hdr_4addr *header)
788{
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000789 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
Daniel Drakee85d0912006-06-02 17:11:32 +0100790 unsigned int tx_length = le16_to_cpu(cs->tx_length);
791 u16 fctl = le16_to_cpu(header->frame_ctl);
792 u16 ftype = WLAN_FC_GET_TYPE(fctl);
793 u16 stype = WLAN_FC_GET_STYPE(fctl);
794
795 /*
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000796 * CONTROL TODO:
Daniel Drakee85d0912006-06-02 17:11:32 +0100797 * - if backoff needed, enable bit 0
798 * - if burst (backoff not needed) disable bit 0
Daniel Drakee85d0912006-06-02 17:11:32 +0100799 */
800
801 cs->control = 0;
802
803 /* First fragment */
804 if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
805 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
806
807 /* Multicast */
808 if (is_multicast_ether_addr(header->addr1))
809 cs->control |= ZD_CS_MULTICAST;
810
811 /* PS-POLL */
Ulrich Kunitz69dad6e2007-07-21 22:42:02 +0100812 if (ftype == IEEE80211_FTYPE_CTL && stype == IEEE80211_STYPE_PSPOLL)
Daniel Drakee85d0912006-06-02 17:11:32 +0100813 cs->control |= ZD_CS_PS_POLL_FRAME;
814
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000815 /* Unicast data frames over the threshold should have RTS */
Daniel Drakee85d0912006-06-02 17:11:32 +0100816 if (!is_multicast_ether_addr(header->addr1) &&
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000817 ftype != IEEE80211_FTYPE_MGMT &&
818 tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
819 cs->control |= ZD_CS_RTS;
820
821 /* Use CTS-to-self protection if required */
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100822 if (ZD_MODULATION_TYPE(cs->modulation) == ZD_OFDM &&
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000823 ieee80211softmac_protection_needed(softmac)) {
824 /* FIXME: avoid sending RTS *and* self-CTS, is that correct? */
825 cs->control &= ~ZD_CS_RTS;
826 cs->control |= ZD_CS_SELF_CTS;
Daniel Drakee85d0912006-06-02 17:11:32 +0100827 }
828
829 /* FIXME: Management frame? */
830}
831
832static int fill_ctrlset(struct zd_mac *mac,
833 struct ieee80211_txb *txb,
834 int frag_num)
835{
836 int r;
837 struct sk_buff *skb = txb->fragments[frag_num];
838 struct ieee80211_hdr_4addr *hdr =
839 (struct ieee80211_hdr_4addr *) skb->data;
840 unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
841 unsigned int next_frag_len;
842 unsigned int packet_length;
843 struct zd_ctrlset *cs = (struct zd_ctrlset *)
844 skb_push(skb, sizeof(struct zd_ctrlset));
845
846 if (frag_num+1 < txb->nr_frags) {
847 next_frag_len = txb->fragments[frag_num+1]->len +
848 IEEE80211_FCS_LEN;
849 } else {
850 next_frag_len = 0;
851 }
852 ZD_ASSERT(frag_len <= 0xffff);
853 ZD_ASSERT(next_frag_len <= 0xffff);
854
855 cs_set_modulation(mac, cs, hdr);
856
857 cs->tx_length = cpu_to_le16(frag_len);
858
859 cs_set_control(mac, cs, hdr);
860
861 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
862 ZD_ASSERT(packet_length <= 0xffff);
863 /* ZD1211B: Computing the length difference this way, gives us
864 * flexibility to compute the packet length.
865 */
Daniel Drake74553ae2007-07-01 18:22:32 +0100866 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
Daniel Drakee85d0912006-06-02 17:11:32 +0100867 packet_length - frag_len : packet_length);
868
869 /*
870 * CURRENT LENGTH:
871 * - transmit frame length in microseconds
872 * - seems to be derived from frame length
873 * - see Cal_Us_Service() in zdinlinef.h
874 * - if macp->bTxBurstEnable is enabled, then multiply by 4
875 * - bTxBurstEnable is never set in the vendor driver
876 *
877 * SERVICE:
878 * - "for PLCP configuration"
879 * - always 0 except in some situations at 802.11b 11M
880 * - see line 53 of zdinlinef.h
881 */
882 cs->service = 0;
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100883 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
Daniel Drakee85d0912006-06-02 17:11:32 +0100884 le16_to_cpu(cs->tx_length));
885 if (r < 0)
886 return r;
887 cs->current_length = cpu_to_le16(r);
888
889 if (next_frag_len == 0) {
890 cs->next_frame_length = 0;
891 } else {
Ulrich Kunitz64f222c2007-08-06 01:24:31 +0100892 r = zd_calc_tx_length_us(NULL, ZD_RATE(cs->modulation),
Daniel Drakee85d0912006-06-02 17:11:32 +0100893 next_frag_len);
894 if (r < 0)
895 return r;
896 cs->next_frame_length = cpu_to_le16(r);
897 }
898
899 return 0;
900}
901
902static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
903{
904 int i, r;
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000905 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100906
907 for (i = 0; i < txb->nr_frags; i++) {
908 struct sk_buff *skb = txb->fragments[i];
909
910 r = fill_ctrlset(mac, txb, i);
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000911 if (r) {
912 ieee->stats.tx_dropped++;
Daniel Drakee85d0912006-06-02 17:11:32 +0100913 return r;
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000914 }
Daniel Drakee85d0912006-06-02 17:11:32 +0100915 r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000916 if (r) {
917 ieee->stats.tx_dropped++;
Daniel Drakee85d0912006-06-02 17:11:32 +0100918 return r;
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000919 }
Daniel Drakee85d0912006-06-02 17:11:32 +0100920 }
921
922 /* FIXME: shouldn't this be handled by the upper layers? */
923 mac->netdev->trans_start = jiffies;
924
925 ieee80211_txb_free(txb);
926 return 0;
927}
928
929struct zd_rt_hdr {
930 struct ieee80211_radiotap_header rt_hdr;
931 u8 rt_flags;
Ulrich Kunitz99f65f22006-08-01 23:43:30 +0200932 u8 rt_rate;
Daniel Drakee85d0912006-06-02 17:11:32 +0100933 u16 rt_channel;
934 u16 rt_chbitmask;
John W. Linvillea88556a2006-11-28 14:16:37 -0500935} __attribute__((packed));
Daniel Drakee85d0912006-06-02 17:11:32 +0100936
937static void fill_rt_header(void *buffer, struct zd_mac *mac,
938 const struct ieee80211_rx_stats *stats,
939 const struct rx_status *status)
940{
941 struct zd_rt_hdr *hdr = buffer;
942
943 hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
944 hdr->rt_hdr.it_pad = 0;
945 hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
946 hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
947 (1 << IEEE80211_RADIOTAP_CHANNEL) |
948 (1 << IEEE80211_RADIOTAP_RATE));
949
950 hdr->rt_flags = 0;
951 if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
952 hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
953
Ulrich Kunitz99f65f22006-08-01 23:43:30 +0200954 hdr->rt_rate = stats->rate / 5;
955
Daniel Drakee85d0912006-06-02 17:11:32 +0100956 /* FIXME: 802.11a */
957 hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
958 _zd_chip_get_channel(&mac->chip)));
959 hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
960 ((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
961 ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
Daniel Drakee85d0912006-06-02 17:11:32 +0100962}
963
964/* Returns 1 if the data packet is for us and 0 otherwise. */
965static int is_data_packet_for_us(struct ieee80211_device *ieee,
966 struct ieee80211_hdr_4addr *hdr)
967{
968 struct net_device *netdev = ieee->dev;
969 u16 fc = le16_to_cpu(hdr->frame_ctl);
970
971 ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
972
973 switch (ieee->iw_mode) {
974 case IW_MODE_ADHOC:
975 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
Daniel Drake832855d2007-03-11 19:53:40 +0000976 compare_ether_addr(hdr->addr3, ieee->bssid) != 0)
Daniel Drakee85d0912006-06-02 17:11:32 +0100977 return 0;
978 break;
979 case IW_MODE_AUTO:
980 case IW_MODE_INFRA:
981 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
982 IEEE80211_FCTL_FROMDS ||
Daniel Drake832855d2007-03-11 19:53:40 +0000983 compare_ether_addr(hdr->addr2, ieee->bssid) != 0)
Daniel Drakee85d0912006-06-02 17:11:32 +0100984 return 0;
985 break;
986 default:
987 ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
988 return 0;
989 }
990
Daniel Drake832855d2007-03-11 19:53:40 +0000991 return compare_ether_addr(hdr->addr1, netdev->dev_addr) == 0 ||
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000992 (is_multicast_ether_addr(hdr->addr1) &&
Daniel Drake832855d2007-03-11 19:53:40 +0000993 compare_ether_addr(hdr->addr3, netdev->dev_addr) != 0) ||
Daniel Drakee85d0912006-06-02 17:11:32 +0100994 (netdev->flags & IFF_PROMISC);
995}
996
Ulrich Kunitz741fec52006-11-22 00:05:53 +0000997/* Filters received packets. The function returns 1 if the packet should be
998 * forwarded to ieee80211_rx(). If the packet should be ignored the function
999 * returns 0. If an invalid packet is found the function returns -EINVAL.
1000 *
1001 * The function calls ieee80211_rx_mgt() directly.
Daniel Drakee85d0912006-06-02 17:11:32 +01001002 *
1003 * It has been based on ieee80211_rx_any.
1004 */
1005static int filter_rx(struct ieee80211_device *ieee,
1006 const u8 *buffer, unsigned int length,
1007 struct ieee80211_rx_stats *stats)
1008{
1009 struct ieee80211_hdr_4addr *hdr;
1010 u16 fc;
1011
1012 if (ieee->iw_mode == IW_MODE_MONITOR)
1013 return 1;
1014
1015 hdr = (struct ieee80211_hdr_4addr *)buffer;
1016 fc = le16_to_cpu(hdr->frame_ctl);
1017 if ((fc & IEEE80211_FCTL_VERS) != 0)
1018 return -EINVAL;
1019
1020 switch (WLAN_FC_GET_TYPE(fc)) {
1021 case IEEE80211_FTYPE_MGMT:
1022 if (length < sizeof(struct ieee80211_hdr_3addr))
1023 return -EINVAL;
1024 ieee80211_rx_mgt(ieee, hdr, stats);
1025 return 0;
1026 case IEEE80211_FTYPE_CTL:
Daniel Drakee85d0912006-06-02 17:11:32 +01001027 return 0;
1028 case IEEE80211_FTYPE_DATA:
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001029 /* Ignore invalid short buffers */
Daniel Drakee85d0912006-06-02 17:11:32 +01001030 if (length < sizeof(struct ieee80211_hdr_3addr))
1031 return -EINVAL;
1032 return is_data_packet_for_us(ieee, hdr);
1033 }
1034
1035 return -EINVAL;
1036}
1037
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001038static void update_qual_rssi(struct zd_mac *mac,
1039 const u8 *buffer, unsigned int length,
1040 u8 qual_percent, u8 rssi_percent)
Daniel Drakee85d0912006-06-02 17:11:32 +01001041{
1042 unsigned long flags;
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001043 struct ieee80211_hdr_3addr *hdr;
1044 int i;
1045
1046 hdr = (struct ieee80211_hdr_3addr *)buffer;
1047 if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
1048 return;
Daniel Drake832855d2007-03-11 19:53:40 +00001049 if (compare_ether_addr(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid) != 0)
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001050 return;
Daniel Drakee85d0912006-06-02 17:11:32 +01001051
1052 spin_lock_irqsave(&mac->lock, flags);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001053 i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
1054 mac->qual_buffer[i] = qual_percent;
1055 mac->rssi_buffer[i] = rssi_percent;
1056 mac->stats_count++;
Daniel Drakee85d0912006-06-02 17:11:32 +01001057 spin_unlock_irqrestore(&mac->lock, flags);
1058}
1059
1060static int fill_rx_stats(struct ieee80211_rx_stats *stats,
1061 const struct rx_status **pstatus,
1062 struct zd_mac *mac,
1063 const u8 *buffer, unsigned int length)
1064{
1065 const struct rx_status *status;
1066
1067 *pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
1068 if (status->frame_status & ZD_RX_ERROR) {
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001069 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1070 ieee->stats.rx_errors++;
1071 if (status->frame_status & ZD_RX_TIMEOUT_ERROR)
1072 ieee->stats.rx_missed_errors++;
1073 else if (status->frame_status & ZD_RX_FIFO_OVERRUN_ERROR)
1074 ieee->stats.rx_fifo_errors++;
1075 else if (status->frame_status & ZD_RX_DECRYPTION_ERROR)
1076 ieee->ieee_stats.rx_discards_undecryptable++;
1077 else if (status->frame_status & ZD_RX_CRC32_ERROR) {
1078 ieee->stats.rx_crc_errors++;
1079 ieee->ieee_stats.rx_fcs_errors++;
1080 }
1081 else if (status->frame_status & ZD_RX_CRC16_ERROR)
1082 ieee->stats.rx_crc_errors++;
Daniel Drakee85d0912006-06-02 17:11:32 +01001083 return -EINVAL;
1084 }
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001085
Daniel Drakee85d0912006-06-02 17:11:32 +01001086 memset(stats, 0, sizeof(struct ieee80211_rx_stats));
1087 stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
1088 + sizeof(struct rx_status));
1089 /* FIXME: 802.11a */
1090 stats->freq = IEEE80211_24GHZ_BAND;
1091 stats->received_channel = _zd_chip_get_channel(&mac->chip);
1092 stats->rssi = zd_rx_strength_percent(status->signal_strength);
1093 stats->signal = zd_rx_qual_percent(buffer,
1094 length - sizeof(struct rx_status),
1095 status);
1096 stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
1097 stats->rate = zd_rx_rate(buffer, status);
1098 if (stats->rate)
1099 stats->mask |= IEEE80211_STATMASK_RATE;
1100
Daniel Drakee85d0912006-06-02 17:11:32 +01001101 return 0;
1102}
1103
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001104static void zd_mac_rx(struct zd_mac *mac, struct sk_buff *skb)
Daniel Drakee85d0912006-06-02 17:11:32 +01001105{
1106 int r;
1107 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1108 struct ieee80211_rx_stats stats;
1109 const struct rx_status *status;
Daniel Drakee85d0912006-06-02 17:11:32 +01001110
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001111 if (skb->len < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
1112 IEEE80211_FCS_LEN + sizeof(struct rx_status))
1113 {
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001114 ieee->stats.rx_errors++;
1115 ieee->stats.rx_length_errors++;
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001116 goto free_skb;
1117 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001118
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001119 r = fill_rx_stats(&stats, &status, mac, skb->data, skb->len);
1120 if (r) {
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001121 /* Only packets with rx errors are included here.
1122 * The error stats have already been set in fill_rx_stats.
1123 */
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001124 goto free_skb;
1125 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001126
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001127 __skb_pull(skb, ZD_PLCP_HEADER_SIZE);
1128 __skb_trim(skb, skb->len -
1129 (IEEE80211_FCS_LEN + sizeof(struct rx_status)));
Daniel Drakee85d0912006-06-02 17:11:32 +01001130
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001131 update_qual_rssi(mac, skb->data, skb->len, stats.signal,
1132 status->signal_strength);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001133
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001134 r = filter_rx(ieee, skb->data, skb->len, &stats);
1135 if (r <= 0) {
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001136 if (r < 0) {
1137 ieee->stats.rx_errors++;
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001138 dev_dbg_f(zd_mac_dev(mac), "Error in packet.\n");
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001139 }
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001140 goto free_skb;
1141 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001142
Daniel Drakee85d0912006-06-02 17:11:32 +01001143 if (ieee->iw_mode == IW_MODE_MONITOR)
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001144 fill_rt_header(skb_push(skb, sizeof(struct zd_rt_hdr)), mac,
Daniel Drakee85d0912006-06-02 17:11:32 +01001145 &stats, status);
Daniel Drakee85d0912006-06-02 17:11:32 +01001146
1147 r = ieee80211_rx(ieee, skb, &stats);
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001148 if (r)
1149 return;
1150free_skb:
1151 /* We are always in a soft irq. */
1152 dev_kfree_skb(skb);
1153}
1154
1155static void do_rx(unsigned long mac_ptr)
1156{
1157 struct zd_mac *mac = (struct zd_mac *)mac_ptr;
1158 struct sk_buff *skb;
1159
1160 while ((skb = skb_dequeue(&mac->rx_queue)) != NULL)
1161 zd_mac_rx(mac, skb);
1162}
1163
1164int zd_mac_rx_irq(struct zd_mac *mac, const u8 *buffer, unsigned int length)
1165{
1166 struct sk_buff *skb;
1167
1168 skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
1169 if (!skb) {
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001170 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001171 dev_warn(zd_mac_dev(mac), "Could not allocate skb.\n");
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001172 ieee->stats.rx_dropped++;
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001173 return -ENOMEM;
1174 }
1175 skb_reserve(skb, sizeof(struct zd_rt_hdr));
1176 memcpy(__skb_put(skb, length), buffer, length);
1177 skb_queue_tail(&mac->rx_queue, skb);
1178 tasklet_schedule(&mac->rx_tasklet);
Daniel Drakee85d0912006-06-02 17:11:32 +01001179 return 0;
1180}
1181
1182static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
1183 int pri)
1184{
1185 return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
1186}
1187
1188static void set_security(struct net_device *netdev,
1189 struct ieee80211_security *sec)
1190{
1191 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
1192 struct ieee80211_security *secinfo = &ieee->sec;
1193 int keyidx;
1194
1195 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
1196
1197 for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
1198 if (sec->flags & (1<<keyidx)) {
1199 secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
1200 secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
1201 memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
1202 SCM_KEY_LEN);
1203 }
1204
1205 if (sec->flags & SEC_ACTIVE_KEY) {
1206 secinfo->active_key = sec->active_key;
1207 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1208 " .active_key = %d\n", sec->active_key);
1209 }
1210 if (sec->flags & SEC_UNICAST_GROUP) {
1211 secinfo->unicast_uses_group = sec->unicast_uses_group;
1212 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1213 " .unicast_uses_group = %d\n",
1214 sec->unicast_uses_group);
1215 }
1216 if (sec->flags & SEC_LEVEL) {
1217 secinfo->level = sec->level;
1218 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1219 " .level = %d\n", sec->level);
1220 }
1221 if (sec->flags & SEC_ENABLED) {
1222 secinfo->enabled = sec->enabled;
1223 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1224 " .enabled = %d\n", sec->enabled);
1225 }
1226 if (sec->flags & SEC_ENCRYPT) {
1227 secinfo->encrypt = sec->encrypt;
1228 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1229 " .encrypt = %d\n", sec->encrypt);
1230 }
1231 if (sec->flags & SEC_AUTH_MODE) {
1232 secinfo->auth_mode = sec->auth_mode;
1233 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1234 " .auth_mode = %d\n", sec->auth_mode);
1235 }
1236}
1237
1238static void ieee_init(struct ieee80211_device *ieee)
1239{
1240 ieee->mode = IEEE_B | IEEE_G;
1241 ieee->freq_band = IEEE80211_24GHZ_BAND;
1242 ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
1243 ieee->tx_headroom = sizeof(struct zd_ctrlset);
1244 ieee->set_security = set_security;
1245 ieee->hard_start_xmit = netdev_tx;
1246
1247 /* Software encryption/decryption for now */
1248 ieee->host_build_iv = 0;
1249 ieee->host_encrypt = 1;
1250 ieee->host_decrypt = 1;
1251
1252 /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
1253 * correctly support AUTO */
1254 ieee->iw_mode = IW_MODE_INFRA;
1255}
1256
1257static void softmac_init(struct ieee80211softmac_device *sm)
1258{
1259 sm->set_channel = set_channel;
Daniel Drakeb1382ed2006-11-22 00:06:48 +00001260 sm->bssinfo_change = bssinfo_change;
Daniel Drakee85d0912006-06-02 17:11:32 +01001261}
1262
1263struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
1264{
1265 struct zd_mac *mac = zd_netdev_mac(ndev);
1266 struct iw_statistics *iw_stats = &mac->iw_stats;
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001267 unsigned int i, count, qual_total, rssi_total;
Daniel Drakee85d0912006-06-02 17:11:32 +01001268
1269 memset(iw_stats, 0, sizeof(struct iw_statistics));
1270 /* We are not setting the status, because ieee->state is not updated
1271 * at all and this driver doesn't track authentication state.
1272 */
1273 spin_lock_irq(&mac->lock);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001274 count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
1275 mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
1276 qual_total = rssi_total = 0;
1277 for (i = 0; i < count; i++) {
1278 qual_total += mac->qual_buffer[i];
1279 rssi_total += mac->rssi_buffer[i];
1280 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001281 spin_unlock_irq(&mac->lock);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001282 iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
1283 if (count > 0) {
1284 iw_stats->qual.qual = qual_total / count;
1285 iw_stats->qual.level = rssi_total / count;
1286 iw_stats->qual.updated |=
1287 IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
1288 } else {
1289 iw_stats->qual.updated |=
1290 IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
1291 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001292 /* TODO: update counter */
1293 return iw_stats;
1294}
1295
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001296#define LINK_LED_WORK_DELAY HZ
1297
David Howellsc4028952006-11-22 14:57:56 +00001298static void link_led_handler(struct work_struct *work)
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001299{
David Howellsc4028952006-11-22 14:57:56 +00001300 struct zd_mac *mac =
1301 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001302 struct zd_chip *chip = &mac->chip;
1303 struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
1304 int is_associated;
1305 int r;
1306
1307 spin_lock_irq(&mac->lock);
John W. Linville41072a12006-10-17 13:47:40 -04001308 is_associated = sm->associnfo.associated != 0;
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001309 spin_unlock_irq(&mac->lock);
1310
1311 r = zd_chip_control_leds(chip,
1312 is_associated ? LED_ASSOCIATED : LED_SCANNING);
1313 if (r)
1314 dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1315
1316 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1317 LINK_LED_WORK_DELAY);
1318}
1319
1320static void housekeeping_init(struct zd_mac *mac)
1321{
David Howellsc4028952006-11-22 14:57:56 +00001322 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001323}
1324
1325static void housekeeping_enable(struct zd_mac *mac)
1326{
1327 dev_dbg_f(zd_mac_dev(mac), "\n");
1328 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1329 0);
1330}
1331
1332static void housekeeping_disable(struct zd_mac *mac)
1333{
1334 dev_dbg_f(zd_mac_dev(mac), "\n");
1335 cancel_rearming_delayed_workqueue(zd_workqueue,
1336 &mac->housekeeping.link_led_work);
1337 zd_chip_control_leds(&mac->chip, LED_OFF);
1338}