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.
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.
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
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>
28 #include "zd_ieee80211.h"
29 #include "zd_netdev.h"
33 static void ieee_init(struct ieee80211_device *ieee);
34 static void softmac_init(struct ieee80211softmac_device *sm);
35 static void set_rts_cts_work(struct work_struct *work);
36 static void set_basic_rates_work(struct work_struct *work);
38 static void housekeeping_init(struct zd_mac *mac);
39 static void housekeeping_enable(struct zd_mac *mac);
40 static void housekeeping_disable(struct zd_mac *mac);
42 static void set_multicast_hash_handler(struct work_struct *work);
44 static void do_rx(unsigned long mac_ptr);
46 int zd_mac_init(struct zd_mac *mac,
47 struct net_device *netdev,
48 struct usb_interface *intf)
50 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
52 memset(mac, 0, sizeof(*mac));
53 spin_lock_init(&mac->lock);
55 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);
58 skb_queue_head_init(&mac->rx_queue);
59 tasklet_init(&mac->rx_tasklet, do_rx, (unsigned long)mac);
60 tasklet_disable(&mac->rx_tasklet);
63 softmac_init(ieee80211_priv(netdev));
64 zd_chip_init(&mac->chip, netdev, intf);
65 housekeeping_init(mac);
66 INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
70 static int reset_channel(struct zd_mac *mac)
74 const struct channel_range *range;
76 spin_lock_irqsave(&mac->lock, flags);
77 range = zd_channel_range(mac->regdomain);
82 mac->requested_channel = range->start;
85 spin_unlock_irqrestore(&mac->lock, flags);
89 int zd_mac_init_hw(struct zd_mac *mac, u8 device_type)
92 struct zd_chip *chip = &mac->chip;
96 r = zd_chip_enable_int(chip);
99 r = zd_chip_init_hw(chip, device_type);
103 zd_get_e2p_mac_addr(chip, addr);
104 r = zd_write_mac_addr(chip, addr);
107 ZD_ASSERT(!irqs_disabled());
108 spin_lock_irq(&mac->lock);
109 memcpy(mac->netdev->dev_addr, addr, ETH_ALEN);
110 spin_unlock_irq(&mac->lock);
112 r = zd_read_regdomain(chip, &default_regdomain);
115 if (!zd_regdomain_supported(default_regdomain)) {
116 dev_dbg_f(zd_mac_dev(mac),
117 "Regulatory Domain %#04x is not supported.\n",
122 spin_lock_irq(&mac->lock);
123 mac->regdomain = mac->default_regdomain = default_regdomain;
124 spin_unlock_irq(&mac->lock);
125 r = reset_channel(mac);
129 /* We must inform the device that we are doing encryption/decryption in
130 * software at the moment. */
131 r = zd_set_encryption_type(chip, ENC_SNIFFER);
135 r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
141 zd_chip_disable_int(chip);
146 void zd_mac_clear(struct zd_mac *mac)
148 flush_workqueue(zd_workqueue);
149 skb_queue_purge(&mac->rx_queue);
150 tasklet_kill(&mac->rx_tasklet);
151 zd_chip_clear(&mac->chip);
152 ZD_ASSERT(!spin_is_locked(&mac->lock));
153 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
156 static int reset_mode(struct zd_mac *mac)
158 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
159 struct zd_ioreq32 ioreqs[] = {
160 { CR_RX_FILTER, STA_RX_FILTER },
161 { CR_SNIFFER_ON, 0U },
164 if (ieee->iw_mode == IW_MODE_MONITOR) {
165 ioreqs[0].value = 0xffffffff;
166 ioreqs[1].value = 0x1;
169 return zd_iowrite32a(&mac->chip, ioreqs, ARRAY_SIZE(ioreqs));
172 int zd_mac_open(struct net_device *netdev)
174 struct zd_mac *mac = zd_netdev_mac(netdev);
175 struct zd_chip *chip = &mac->chip;
178 tasklet_enable(&mac->rx_tasklet);
180 r = zd_chip_enable_int(chip);
184 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
190 r = zd_chip_switch_radio_on(chip);
193 r = zd_chip_set_channel(chip, mac->requested_channel);
196 r = zd_chip_enable_rx(chip);
199 r = zd_chip_enable_hwint(chip);
203 housekeeping_enable(mac);
204 ieee80211softmac_start(netdev);
207 zd_chip_disable_rx(chip);
209 zd_chip_switch_radio_off(chip);
211 zd_chip_disable_int(chip);
216 int zd_mac_stop(struct net_device *netdev)
218 struct zd_mac *mac = zd_netdev_mac(netdev);
219 struct zd_chip *chip = &mac->chip;
221 netif_stop_queue(netdev);
224 * The order here deliberately is a little different from the open()
225 * method, since we need to make sure there is no opportunity for RX
226 * frames to be processed by softmac after we have stopped it.
229 zd_chip_disable_rx(chip);
230 skb_queue_purge(&mac->rx_queue);
231 tasklet_disable(&mac->rx_tasklet);
232 housekeeping_disable(mac);
233 ieee80211softmac_stop(netdev);
235 /* Ensure no work items are running or queued from this point */
236 cancel_delayed_work(&mac->set_rts_cts_work);
237 cancel_delayed_work(&mac->set_basic_rates_work);
238 flush_workqueue(zd_workqueue);
239 mac->updating_rts_rate = 0;
240 mac->updating_basic_rates = 0;
242 zd_chip_disable_hwint(chip);
243 zd_chip_switch_radio_off(chip);
244 zd_chip_disable_int(chip);
249 int zd_mac_set_mac_address(struct net_device *netdev, void *p)
253 struct sockaddr *addr = p;
254 struct zd_mac *mac = zd_netdev_mac(netdev);
255 struct zd_chip *chip = &mac->chip;
257 if (!is_valid_ether_addr(addr->sa_data))
258 return -EADDRNOTAVAIL;
260 dev_dbg_f(zd_mac_dev(mac),
261 "Setting MAC to " MAC_FMT "\n", MAC_ARG(addr->sa_data));
263 r = zd_write_mac_addr(chip, addr->sa_data);
267 spin_lock_irqsave(&mac->lock, flags);
268 memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
269 spin_unlock_irqrestore(&mac->lock, flags);
274 static void set_multicast_hash_handler(struct work_struct *work)
276 struct zd_mac *mac = container_of(work, struct zd_mac,
277 set_multicast_hash_work);
278 struct zd_mc_hash hash;
280 spin_lock_irq(&mac->lock);
281 hash = mac->multicast_hash;
282 spin_unlock_irq(&mac->lock);
284 zd_chip_set_multicast_hash(&mac->chip, &hash);
287 void zd_mac_set_multicast_list(struct net_device *dev)
289 struct zd_mc_hash hash;
290 struct zd_mac *mac = zd_netdev_mac(dev);
291 struct dev_mc_list *mc;
294 if (dev->flags & (IFF_PROMISC|IFF_ALLMULTI)) {
295 zd_mc_add_all(&hash);
298 for (mc = dev->mc_list; mc; mc = mc->next) {
299 dev_dbg_f(zd_mac_dev(mac), "mc addr " MAC_FMT "\n",
300 MAC_ARG(mc->dmi_addr));
301 zd_mc_add_addr(&hash, mc->dmi_addr);
305 spin_lock_irqsave(&mac->lock, flags);
306 mac->multicast_hash = hash;
307 spin_unlock_irqrestore(&mac->lock, flags);
308 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
311 int zd_mac_set_regdomain(struct zd_mac *mac, u8 regdomain)
316 ZD_ASSERT(!irqs_disabled());
317 spin_lock_irq(&mac->lock);
318 if (regdomain == 0) {
319 regdomain = mac->default_regdomain;
321 if (!zd_regdomain_supported(regdomain)) {
322 spin_unlock_irq(&mac->lock);
325 mac->regdomain = regdomain;
326 channel = mac->requested_channel;
327 spin_unlock_irq(&mac->lock);
329 r = zd_geo_init(zd_mac_to_ieee80211(mac), regdomain);
332 if (!zd_regdomain_supports_channel(regdomain, channel)) {
333 r = reset_channel(mac);
341 u8 zd_mac_get_regdomain(struct zd_mac *mac)
346 spin_lock_irqsave(&mac->lock, flags);
347 regdomain = mac->regdomain;
348 spin_unlock_irqrestore(&mac->lock, flags);
352 /* Fallback to lowest rate, if rate is unknown. */
353 static u8 rate_to_zd_rate(u8 rate)
356 case IEEE80211_CCK_RATE_2MB:
357 return ZD_CCK_RATE_2M;
358 case IEEE80211_CCK_RATE_5MB:
359 return ZD_CCK_RATE_5_5M;
360 case IEEE80211_CCK_RATE_11MB:
361 return ZD_CCK_RATE_11M;
362 case IEEE80211_OFDM_RATE_6MB:
363 return ZD_OFDM_RATE_6M;
364 case IEEE80211_OFDM_RATE_9MB:
365 return ZD_OFDM_RATE_9M;
366 case IEEE80211_OFDM_RATE_12MB:
367 return ZD_OFDM_RATE_12M;
368 case IEEE80211_OFDM_RATE_18MB:
369 return ZD_OFDM_RATE_18M;
370 case IEEE80211_OFDM_RATE_24MB:
371 return ZD_OFDM_RATE_24M;
372 case IEEE80211_OFDM_RATE_36MB:
373 return ZD_OFDM_RATE_36M;
374 case IEEE80211_OFDM_RATE_48MB:
375 return ZD_OFDM_RATE_48M;
376 case IEEE80211_OFDM_RATE_54MB:
377 return ZD_OFDM_RATE_54M;
379 return ZD_CCK_RATE_1M;
382 static u16 rate_to_cr_rate(u8 rate)
385 case IEEE80211_CCK_RATE_2MB:
387 case IEEE80211_CCK_RATE_5MB:
389 case IEEE80211_CCK_RATE_11MB:
391 case IEEE80211_OFDM_RATE_6MB:
393 case IEEE80211_OFDM_RATE_9MB:
395 case IEEE80211_OFDM_RATE_12MB:
397 case IEEE80211_OFDM_RATE_18MB:
399 case IEEE80211_OFDM_RATE_24MB:
401 case IEEE80211_OFDM_RATE_36MB:
403 case IEEE80211_OFDM_RATE_48MB:
405 case IEEE80211_OFDM_RATE_54MB:
411 static void try_enable_tx(struct zd_mac *mac)
415 spin_lock_irqsave(&mac->lock, flags);
416 if (mac->updating_rts_rate == 0 && mac->updating_basic_rates == 0)
417 netif_wake_queue(mac->netdev);
418 spin_unlock_irqrestore(&mac->lock, flags);
421 static void set_rts_cts_work(struct work_struct *work)
424 container_of(work, struct zd_mac, set_rts_cts_work.work);
427 unsigned int short_preamble;
429 mutex_lock(&mac->chip.mutex);
431 spin_lock_irqsave(&mac->lock, flags);
432 mac->updating_rts_rate = 0;
433 rts_rate = mac->rts_rate;
434 short_preamble = mac->short_preamble;
435 spin_unlock_irqrestore(&mac->lock, flags);
437 zd_chip_set_rts_cts_rate_locked(&mac->chip, rts_rate, short_preamble);
438 mutex_unlock(&mac->chip.mutex);
443 static void set_basic_rates_work(struct work_struct *work)
446 container_of(work, struct zd_mac, set_basic_rates_work.work);
450 mutex_lock(&mac->chip.mutex);
452 spin_lock_irqsave(&mac->lock, flags);
453 mac->updating_basic_rates = 0;
454 basic_rates = mac->basic_rates;
455 spin_unlock_irqrestore(&mac->lock, flags);
457 zd_chip_set_basic_rates_locked(&mac->chip, basic_rates);
458 mutex_unlock(&mac->chip.mutex);
463 static void bssinfo_change(struct net_device *netdev, u32 changes)
465 struct zd_mac *mac = zd_netdev_mac(netdev);
466 struct ieee80211softmac_device *softmac = ieee80211_priv(netdev);
467 struct ieee80211softmac_bss_info *bssinfo = &softmac->bssinfo;
468 int need_set_rts_cts = 0;
469 int need_set_rates = 0;
473 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
475 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_SHORT_PREAMBLE) {
476 spin_lock_irqsave(&mac->lock, flags);
477 mac->short_preamble = bssinfo->short_preamble;
478 spin_unlock_irqrestore(&mac->lock, flags);
479 need_set_rts_cts = 1;
482 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_RATES) {
483 /* Set RTS rate to highest available basic rate */
484 u8 hi_rate = ieee80211softmac_highest_supported_rate(softmac,
485 &bssinfo->supported_rates, 1);
486 hi_rate = rate_to_zd_rate(hi_rate);
488 spin_lock_irqsave(&mac->lock, flags);
489 if (hi_rate != mac->rts_rate) {
490 mac->rts_rate = hi_rate;
491 need_set_rts_cts = 1;
493 spin_unlock_irqrestore(&mac->lock, flags);
495 /* Set basic rates */
497 if (bssinfo->supported_rates.count == 0) {
498 /* Allow the device to be flexible */
499 basic_rates = CR_RATES_80211B | CR_RATES_80211G;
504 for (i = 0; i < bssinfo->supported_rates.count; i++) {
505 u16 rate = bssinfo->supported_rates.rates[i];
506 if ((rate & IEEE80211_BASIC_RATE_MASK) == 0)
509 rate &= ~IEEE80211_BASIC_RATE_MASK;
510 basic_rates |= rate_to_cr_rate(rate);
513 spin_lock_irqsave(&mac->lock, flags);
514 mac->basic_rates = basic_rates;
515 spin_unlock_irqrestore(&mac->lock, flags);
518 /* Schedule any changes we made above */
520 spin_lock_irqsave(&mac->lock, flags);
521 if (need_set_rts_cts && !mac->updating_rts_rate) {
522 mac->updating_rts_rate = 1;
523 netif_stop_queue(mac->netdev);
524 queue_delayed_work(zd_workqueue, &mac->set_rts_cts_work, 0);
526 if (need_set_rates && !mac->updating_basic_rates) {
527 mac->updating_basic_rates = 1;
528 netif_stop_queue(mac->netdev);
529 queue_delayed_work(zd_workqueue, &mac->set_basic_rates_work,
532 spin_unlock_irqrestore(&mac->lock, flags);
535 static void set_channel(struct net_device *netdev, u8 channel)
537 struct zd_mac *mac = zd_netdev_mac(netdev);
539 dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
541 zd_chip_set_channel(&mac->chip, channel);
544 int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
546 unsigned long lock_flags;
547 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
549 if (ieee->iw_mode == IW_MODE_INFRA)
552 spin_lock_irqsave(&mac->lock, lock_flags);
553 if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
554 spin_unlock_irqrestore(&mac->lock, lock_flags);
557 mac->requested_channel = channel;
558 spin_unlock_irqrestore(&mac->lock, lock_flags);
559 if (netif_running(mac->netdev))
560 return zd_chip_set_channel(&mac->chip, channel);
565 u8 zd_mac_get_channel(struct zd_mac *mac)
567 u8 channel = zd_chip_get_channel(&mac->chip);
569 dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
573 /* If wrong rate is given, we are falling back to the slowest rate: 1MBit/s */
574 static u8 zd_rate_typed(u8 zd_rate)
576 static const u8 typed_rates[16] = {
577 [ZD_CCK_RATE_1M] = ZD_CS_CCK|ZD_CCK_RATE_1M,
578 [ZD_CCK_RATE_2M] = ZD_CS_CCK|ZD_CCK_RATE_2M,
579 [ZD_CCK_RATE_5_5M] = ZD_CS_CCK|ZD_CCK_RATE_5_5M,
580 [ZD_CCK_RATE_11M] = ZD_CS_CCK|ZD_CCK_RATE_11M,
581 [ZD_OFDM_RATE_6M] = ZD_CS_OFDM|ZD_OFDM_RATE_6M,
582 [ZD_OFDM_RATE_9M] = ZD_CS_OFDM|ZD_OFDM_RATE_9M,
583 [ZD_OFDM_RATE_12M] = ZD_CS_OFDM|ZD_OFDM_RATE_12M,
584 [ZD_OFDM_RATE_18M] = ZD_CS_OFDM|ZD_OFDM_RATE_18M,
585 [ZD_OFDM_RATE_24M] = ZD_CS_OFDM|ZD_OFDM_RATE_24M,
586 [ZD_OFDM_RATE_36M] = ZD_CS_OFDM|ZD_OFDM_RATE_36M,
587 [ZD_OFDM_RATE_48M] = ZD_CS_OFDM|ZD_OFDM_RATE_48M,
588 [ZD_OFDM_RATE_54M] = ZD_CS_OFDM|ZD_OFDM_RATE_54M,
591 ZD_ASSERT(ZD_CS_RATE_MASK == 0x0f);
592 return typed_rates[zd_rate & ZD_CS_RATE_MASK];
595 int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
597 struct ieee80211_device *ieee;
603 mac->netdev->type = ARPHRD_ETHER;
605 case IW_MODE_MONITOR:
606 mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
609 dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
613 ieee = zd_mac_to_ieee80211(mac);
614 ZD_ASSERT(!irqs_disabled());
615 spin_lock_irq(&ieee->lock);
616 ieee->iw_mode = mode;
617 spin_unlock_irq(&ieee->lock);
619 if (netif_running(mac->netdev))
620 return reset_mode(mac);
625 int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
628 struct ieee80211_device *ieee;
630 ieee = zd_mac_to_ieee80211(mac);
631 spin_lock_irqsave(&ieee->lock, flags);
632 *mode = ieee->iw_mode;
633 spin_unlock_irqrestore(&ieee->lock, flags);
637 int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
640 const struct channel_range *channel_range;
643 memset(range, 0, sizeof(*range));
645 /* FIXME: Not so important and depends on the mode. For 802.11g
646 * usually this value is used. It seems to be that Bit/s number is
649 range->throughput = 27 * 1000 * 1000;
651 range->max_qual.qual = 100;
652 range->max_qual.level = 100;
654 /* FIXME: Needs still to be tuned. */
655 range->avg_qual.qual = 71;
656 range->avg_qual.level = 80;
658 /* FIXME: depends on standard? */
659 range->min_rts = 256;
660 range->max_rts = 2346;
662 range->min_frag = MIN_FRAG_THRESHOLD;
663 range->max_frag = MAX_FRAG_THRESHOLD;
665 range->max_encoding_tokens = WEP_KEYS;
666 range->num_encoding_sizes = 2;
667 range->encoding_size[0] = 5;
668 range->encoding_size[1] = WEP_KEY_LEN;
670 range->we_version_compiled = WIRELESS_EXT;
671 range->we_version_source = 20;
673 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
674 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
676 ZD_ASSERT(!irqs_disabled());
677 spin_lock_irq(&mac->lock);
678 regdomain = mac->regdomain;
679 spin_unlock_irq(&mac->lock);
680 channel_range = zd_channel_range(regdomain);
682 range->num_channels = channel_range->end - channel_range->start;
683 range->old_num_channels = range->num_channels;
684 range->num_frequency = range->num_channels;
685 range->old_num_frequency = range->num_frequency;
687 for (i = 0; i < range->num_frequency; i++) {
688 struct iw_freq *freq = &range->freq[i];
689 freq->i = channel_range->start + i;
690 zd_channel_to_freq(freq, freq->i);
696 static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
698 static const u8 rate_divisor[] = {
699 [ZD_CCK_RATE_1M] = 1,
700 [ZD_CCK_RATE_2M] = 2,
701 [ZD_CCK_RATE_5_5M] = 11, /* bits must be doubled */
702 [ZD_CCK_RATE_11M] = 11,
703 [ZD_OFDM_RATE_6M] = 6,
704 [ZD_OFDM_RATE_9M] = 9,
705 [ZD_OFDM_RATE_12M] = 12,
706 [ZD_OFDM_RATE_18M] = 18,
707 [ZD_OFDM_RATE_24M] = 24,
708 [ZD_OFDM_RATE_36M] = 36,
709 [ZD_OFDM_RATE_48M] = 48,
710 [ZD_OFDM_RATE_54M] = 54,
713 u32 bits = (u32)tx_length * 8;
716 divisor = rate_divisor[zd_rate];
721 case ZD_CCK_RATE_5_5M:
722 bits = (2*bits) + 10; /* round up to the next integer */
724 case ZD_CCK_RATE_11M:
727 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
728 if (0 < t && t <= 3) {
729 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
732 bits += 10; /* round up to the next integer */
740 R2M_SHORT_PREAMBLE = 0x01,
744 static u8 zd_rate_to_modulation(u8 zd_rate, int flags)
748 modulation = zd_rate_typed(zd_rate);
749 if (flags & R2M_SHORT_PREAMBLE) {
750 switch (ZD_CS_RATE(modulation)) {
752 case ZD_CCK_RATE_5_5M:
753 case ZD_CCK_RATE_11M:
754 modulation |= ZD_CS_CCK_PREA_SHORT;
758 if (flags & R2M_11A) {
759 if (ZD_CS_TYPE(modulation) == ZD_CS_OFDM)
760 modulation |= ZD_CS_OFDM_MODE_11A;
765 static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
766 struct ieee80211_hdr_4addr *hdr)
768 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
769 u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
771 int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
772 int is_multicast = is_multicast_ether_addr(hdr->addr1);
773 int short_preamble = ieee80211softmac_short_preamble_ok(softmac,
774 is_multicast, is_mgt);
777 /* FIXME: 802.11a? */
778 rate = ieee80211softmac_suggest_txrate(softmac, is_multicast, is_mgt);
781 flags |= R2M_SHORT_PREAMBLE;
783 zd_rate = rate_to_zd_rate(rate);
784 cs->modulation = zd_rate_to_modulation(zd_rate, flags);
787 static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
788 struct ieee80211_hdr_4addr *header)
790 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
791 unsigned int tx_length = le16_to_cpu(cs->tx_length);
792 u16 fctl = le16_to_cpu(header->frame_ctl);
793 u16 ftype = WLAN_FC_GET_TYPE(fctl);
794 u16 stype = WLAN_FC_GET_STYPE(fctl);
798 * - if backoff needed, enable bit 0
799 * - if burst (backoff not needed) disable bit 0
805 if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
806 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
809 if (is_multicast_ether_addr(header->addr1))
810 cs->control |= ZD_CS_MULTICAST;
813 if (stype == IEEE80211_STYPE_PSPOLL)
814 cs->control |= ZD_CS_PS_POLL_FRAME;
816 /* Unicast data frames over the threshold should have RTS */
817 if (!is_multicast_ether_addr(header->addr1) &&
818 ftype != IEEE80211_FTYPE_MGMT &&
819 tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
820 cs->control |= ZD_CS_RTS;
822 /* Use CTS-to-self protection if required */
823 if (ZD_CS_TYPE(cs->modulation) == ZD_CS_OFDM &&
824 ieee80211softmac_protection_needed(softmac)) {
825 /* FIXME: avoid sending RTS *and* self-CTS, is that correct? */
826 cs->control &= ~ZD_CS_RTS;
827 cs->control |= ZD_CS_SELF_CTS;
830 /* FIXME: Management frame? */
833 static int fill_ctrlset(struct zd_mac *mac,
834 struct ieee80211_txb *txb,
838 struct sk_buff *skb = txb->fragments[frag_num];
839 struct ieee80211_hdr_4addr *hdr =
840 (struct ieee80211_hdr_4addr *) skb->data;
841 unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
842 unsigned int next_frag_len;
843 unsigned int packet_length;
844 struct zd_ctrlset *cs = (struct zd_ctrlset *)
845 skb_push(skb, sizeof(struct zd_ctrlset));
847 if (frag_num+1 < txb->nr_frags) {
848 next_frag_len = txb->fragments[frag_num+1]->len +
853 ZD_ASSERT(frag_len <= 0xffff);
854 ZD_ASSERT(next_frag_len <= 0xffff);
856 cs_set_modulation(mac, cs, hdr);
858 cs->tx_length = cpu_to_le16(frag_len);
860 cs_set_control(mac, cs, hdr);
862 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
863 ZD_ASSERT(packet_length <= 0xffff);
864 /* ZD1211B: Computing the length difference this way, gives us
865 * flexibility to compute the packet length.
867 cs->packet_length = cpu_to_le16(mac->chip.is_zd1211b ?
868 packet_length - frag_len : packet_length);
872 * - transmit frame length in microseconds
873 * - seems to be derived from frame length
874 * - see Cal_Us_Service() in zdinlinef.h
875 * - if macp->bTxBurstEnable is enabled, then multiply by 4
876 * - bTxBurstEnable is never set in the vendor driver
879 * - "for PLCP configuration"
880 * - always 0 except in some situations at 802.11b 11M
881 * - see line 53 of zdinlinef.h
884 r = zd_calc_tx_length_us(&cs->service, ZD_CS_RATE(cs->modulation),
885 le16_to_cpu(cs->tx_length));
888 cs->current_length = cpu_to_le16(r);
890 if (next_frag_len == 0) {
891 cs->next_frame_length = 0;
893 r = zd_calc_tx_length_us(NULL, ZD_CS_RATE(cs->modulation),
897 cs->next_frame_length = cpu_to_le16(r);
903 static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
906 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
908 for (i = 0; i < txb->nr_frags; i++) {
909 struct sk_buff *skb = txb->fragments[i];
911 r = fill_ctrlset(mac, txb, i);
913 ieee->stats.tx_dropped++;
916 r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
918 ieee->stats.tx_dropped++;
923 /* FIXME: shouldn't this be handled by the upper layers? */
924 mac->netdev->trans_start = jiffies;
926 ieee80211_txb_free(txb);
931 struct ieee80211_radiotap_header rt_hdr;
936 } __attribute__((packed));
938 static void fill_rt_header(void *buffer, struct zd_mac *mac,
939 const struct ieee80211_rx_stats *stats,
940 const struct rx_status *status)
942 struct zd_rt_hdr *hdr = buffer;
944 hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
945 hdr->rt_hdr.it_pad = 0;
946 hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
947 hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
948 (1 << IEEE80211_RADIOTAP_CHANNEL) |
949 (1 << IEEE80211_RADIOTAP_RATE));
952 if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
953 hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
955 hdr->rt_rate = stats->rate / 5;
958 hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
959 _zd_chip_get_channel(&mac->chip)));
960 hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
961 ((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
962 ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
965 /* Returns 1 if the data packet is for us and 0 otherwise. */
966 static int is_data_packet_for_us(struct ieee80211_device *ieee,
967 struct ieee80211_hdr_4addr *hdr)
969 struct net_device *netdev = ieee->dev;
970 u16 fc = le16_to_cpu(hdr->frame_ctl);
972 ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
974 switch (ieee->iw_mode) {
976 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
977 compare_ether_addr(hdr->addr3, ieee->bssid) != 0)
982 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
983 IEEE80211_FCTL_FROMDS ||
984 compare_ether_addr(hdr->addr2, ieee->bssid) != 0)
988 ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
992 return compare_ether_addr(hdr->addr1, netdev->dev_addr) == 0 ||
993 (is_multicast_ether_addr(hdr->addr1) &&
994 compare_ether_addr(hdr->addr3, netdev->dev_addr) != 0) ||
995 (netdev->flags & IFF_PROMISC);
998 /* Filters received packets. The function returns 1 if the packet should be
999 * forwarded to ieee80211_rx(). If the packet should be ignored the function
1000 * returns 0. If an invalid packet is found the function returns -EINVAL.
1002 * The function calls ieee80211_rx_mgt() directly.
1004 * It has been based on ieee80211_rx_any.
1006 static int filter_rx(struct ieee80211_device *ieee,
1007 const u8 *buffer, unsigned int length,
1008 struct ieee80211_rx_stats *stats)
1010 struct ieee80211_hdr_4addr *hdr;
1013 if (ieee->iw_mode == IW_MODE_MONITOR)
1016 hdr = (struct ieee80211_hdr_4addr *)buffer;
1017 fc = le16_to_cpu(hdr->frame_ctl);
1018 if ((fc & IEEE80211_FCTL_VERS) != 0)
1021 switch (WLAN_FC_GET_TYPE(fc)) {
1022 case IEEE80211_FTYPE_MGMT:
1023 if (length < sizeof(struct ieee80211_hdr_3addr))
1025 ieee80211_rx_mgt(ieee, hdr, stats);
1027 case IEEE80211_FTYPE_CTL:
1029 case IEEE80211_FTYPE_DATA:
1030 /* Ignore invalid short buffers */
1031 if (length < sizeof(struct ieee80211_hdr_3addr))
1033 return is_data_packet_for_us(ieee, hdr);
1039 static void update_qual_rssi(struct zd_mac *mac,
1040 const u8 *buffer, unsigned int length,
1041 u8 qual_percent, u8 rssi_percent)
1043 unsigned long flags;
1044 struct ieee80211_hdr_3addr *hdr;
1047 hdr = (struct ieee80211_hdr_3addr *)buffer;
1048 if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
1050 if (compare_ether_addr(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid) != 0)
1053 spin_lock_irqsave(&mac->lock, flags);
1054 i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
1055 mac->qual_buffer[i] = qual_percent;
1056 mac->rssi_buffer[i] = rssi_percent;
1058 spin_unlock_irqrestore(&mac->lock, flags);
1061 static int fill_rx_stats(struct ieee80211_rx_stats *stats,
1062 const struct rx_status **pstatus,
1064 const u8 *buffer, unsigned int length)
1066 const struct rx_status *status;
1068 *pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
1069 if (status->frame_status & ZD_RX_ERROR) {
1070 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1071 ieee->stats.rx_errors++;
1072 if (status->frame_status & ZD_RX_TIMEOUT_ERROR)
1073 ieee->stats.rx_missed_errors++;
1074 else if (status->frame_status & ZD_RX_FIFO_OVERRUN_ERROR)
1075 ieee->stats.rx_fifo_errors++;
1076 else if (status->frame_status & ZD_RX_DECRYPTION_ERROR)
1077 ieee->ieee_stats.rx_discards_undecryptable++;
1078 else if (status->frame_status & ZD_RX_CRC32_ERROR) {
1079 ieee->stats.rx_crc_errors++;
1080 ieee->ieee_stats.rx_fcs_errors++;
1082 else if (status->frame_status & ZD_RX_CRC16_ERROR)
1083 ieee->stats.rx_crc_errors++;
1087 memset(stats, 0, sizeof(struct ieee80211_rx_stats));
1088 stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
1089 + sizeof(struct rx_status));
1090 /* FIXME: 802.11a */
1091 stats->freq = IEEE80211_24GHZ_BAND;
1092 stats->received_channel = _zd_chip_get_channel(&mac->chip);
1093 stats->rssi = zd_rx_strength_percent(status->signal_strength);
1094 stats->signal = zd_rx_qual_percent(buffer,
1095 length - sizeof(struct rx_status),
1097 stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
1098 stats->rate = zd_rx_rate(buffer, status);
1100 stats->mask |= IEEE80211_STATMASK_RATE;
1105 static void zd_mac_rx(struct zd_mac *mac, struct sk_buff *skb)
1108 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1109 struct ieee80211_rx_stats stats;
1110 const struct rx_status *status;
1112 if (skb->len < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
1113 IEEE80211_FCS_LEN + sizeof(struct rx_status))
1115 ieee->stats.rx_errors++;
1116 ieee->stats.rx_length_errors++;
1120 r = fill_rx_stats(&stats, &status, mac, skb->data, skb->len);
1122 /* Only packets with rx errors are included here.
1123 * The error stats have already been set in fill_rx_stats.
1128 __skb_pull(skb, ZD_PLCP_HEADER_SIZE);
1129 __skb_trim(skb, skb->len -
1130 (IEEE80211_FCS_LEN + sizeof(struct rx_status)));
1132 update_qual_rssi(mac, skb->data, skb->len, stats.signal,
1133 status->signal_strength);
1135 r = filter_rx(ieee, skb->data, skb->len, &stats);
1138 ieee->stats.rx_errors++;
1139 dev_dbg_f(zd_mac_dev(mac), "Error in packet.\n");
1144 if (ieee->iw_mode == IW_MODE_MONITOR)
1145 fill_rt_header(skb_push(skb, sizeof(struct zd_rt_hdr)), mac,
1148 r = ieee80211_rx(ieee, skb, &stats);
1152 /* We are always in a soft irq. */
1156 static void do_rx(unsigned long mac_ptr)
1158 struct zd_mac *mac = (struct zd_mac *)mac_ptr;
1159 struct sk_buff *skb;
1161 while ((skb = skb_dequeue(&mac->rx_queue)) != NULL)
1162 zd_mac_rx(mac, skb);
1165 int zd_mac_rx_irq(struct zd_mac *mac, const u8 *buffer, unsigned int length)
1167 struct sk_buff *skb;
1169 skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
1171 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1172 dev_warn(zd_mac_dev(mac), "Could not allocate skb.\n");
1173 ieee->stats.rx_dropped++;
1176 skb_reserve(skb, sizeof(struct zd_rt_hdr));
1177 memcpy(__skb_put(skb, length), buffer, length);
1178 skb_queue_tail(&mac->rx_queue, skb);
1179 tasklet_schedule(&mac->rx_tasklet);
1183 static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
1186 return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
1189 static void set_security(struct net_device *netdev,
1190 struct ieee80211_security *sec)
1192 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
1193 struct ieee80211_security *secinfo = &ieee->sec;
1196 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
1198 for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
1199 if (sec->flags & (1<<keyidx)) {
1200 secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
1201 secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
1202 memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
1206 if (sec->flags & SEC_ACTIVE_KEY) {
1207 secinfo->active_key = sec->active_key;
1208 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1209 " .active_key = %d\n", sec->active_key);
1211 if (sec->flags & SEC_UNICAST_GROUP) {
1212 secinfo->unicast_uses_group = sec->unicast_uses_group;
1213 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1214 " .unicast_uses_group = %d\n",
1215 sec->unicast_uses_group);
1217 if (sec->flags & SEC_LEVEL) {
1218 secinfo->level = sec->level;
1219 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1220 " .level = %d\n", sec->level);
1222 if (sec->flags & SEC_ENABLED) {
1223 secinfo->enabled = sec->enabled;
1224 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1225 " .enabled = %d\n", sec->enabled);
1227 if (sec->flags & SEC_ENCRYPT) {
1228 secinfo->encrypt = sec->encrypt;
1229 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1230 " .encrypt = %d\n", sec->encrypt);
1232 if (sec->flags & SEC_AUTH_MODE) {
1233 secinfo->auth_mode = sec->auth_mode;
1234 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1235 " .auth_mode = %d\n", sec->auth_mode);
1239 static void ieee_init(struct ieee80211_device *ieee)
1241 ieee->mode = IEEE_B | IEEE_G;
1242 ieee->freq_band = IEEE80211_24GHZ_BAND;
1243 ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
1244 ieee->tx_headroom = sizeof(struct zd_ctrlset);
1245 ieee->set_security = set_security;
1246 ieee->hard_start_xmit = netdev_tx;
1248 /* Software encryption/decryption for now */
1249 ieee->host_build_iv = 0;
1250 ieee->host_encrypt = 1;
1251 ieee->host_decrypt = 1;
1253 /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
1254 * correctly support AUTO */
1255 ieee->iw_mode = IW_MODE_INFRA;
1258 static void softmac_init(struct ieee80211softmac_device *sm)
1260 sm->set_channel = set_channel;
1261 sm->bssinfo_change = bssinfo_change;
1264 struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
1266 struct zd_mac *mac = zd_netdev_mac(ndev);
1267 struct iw_statistics *iw_stats = &mac->iw_stats;
1268 unsigned int i, count, qual_total, rssi_total;
1270 memset(iw_stats, 0, sizeof(struct iw_statistics));
1271 /* We are not setting the status, because ieee->state is not updated
1272 * at all and this driver doesn't track authentication state.
1274 spin_lock_irq(&mac->lock);
1275 count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
1276 mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
1277 qual_total = rssi_total = 0;
1278 for (i = 0; i < count; i++) {
1279 qual_total += mac->qual_buffer[i];
1280 rssi_total += mac->rssi_buffer[i];
1282 spin_unlock_irq(&mac->lock);
1283 iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
1285 iw_stats->qual.qual = qual_total / count;
1286 iw_stats->qual.level = rssi_total / count;
1287 iw_stats->qual.updated |=
1288 IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
1290 iw_stats->qual.updated |=
1291 IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
1293 /* TODO: update counter */
1297 #define LINK_LED_WORK_DELAY HZ
1299 static void link_led_handler(struct work_struct *work)
1301 struct zd_mac *mac =
1302 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
1303 struct zd_chip *chip = &mac->chip;
1304 struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
1308 spin_lock_irq(&mac->lock);
1309 is_associated = sm->associnfo.associated != 0;
1310 spin_unlock_irq(&mac->lock);
1312 r = zd_chip_control_leds(chip,
1313 is_associated ? LED_ASSOCIATED : LED_SCANNING);
1315 dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1317 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1318 LINK_LED_WORK_DELAY);
1321 static void housekeeping_init(struct zd_mac *mac)
1323 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
1326 static void housekeeping_enable(struct zd_mac *mac)
1328 dev_dbg_f(zd_mac_dev(mac), "\n");
1329 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1333 static void housekeeping_disable(struct zd_mac *mac)
1335 dev_dbg_f(zd_mac_dev(mac), "\n");
1336 cancel_rearming_delayed_workqueue(zd_workqueue,
1337 &mac->housekeeping.link_led_work);
1338 zd_chip_control_leds(&mac->chip, LED_OFF);