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_preinit_hw(struct zd_mac *mac)
94 r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
98 memcpy(mac->netdev->dev_addr, addr, ETH_ALEN);
102 int zd_mac_init_hw(struct zd_mac *mac)
105 struct zd_chip *chip = &mac->chip;
106 u8 default_regdomain;
108 r = zd_chip_enable_int(chip);
111 r = zd_chip_init_hw(chip);
115 ZD_ASSERT(!irqs_disabled());
117 r = zd_read_regdomain(chip, &default_regdomain);
120 if (!zd_regdomain_supported(default_regdomain)) {
121 /* 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
126 dev_warn(zd_mac_dev(mac), "Unrecognised regulatory domain: "
127 "%#04x. Defaulting to FCC.\n", default_regdomain);
128 default_regdomain = ZD_REGDOMAIN_FCC;
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);
137 /* 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);
143 r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
149 zd_chip_disable_int(chip);
154 void zd_mac_clear(struct zd_mac *mac)
156 flush_workqueue(zd_workqueue);
157 skb_queue_purge(&mac->rx_queue);
158 tasklet_kill(&mac->rx_tasklet);
159 zd_chip_clear(&mac->chip);
160 ZD_ASSERT(!spin_is_locked(&mac->lock));
161 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
164 static int reset_mode(struct zd_mac *mac)
166 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
167 u32 filter = (ieee->iw_mode == IW_MODE_MONITOR) ? ~0 : STA_RX_FILTER;
168 return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
171 int zd_mac_open(struct net_device *netdev)
173 struct zd_mac *mac = zd_netdev_mac(netdev);
174 struct zd_chip *chip = &mac->chip;
175 struct zd_usb *usb = &chip->usb;
178 if (!usb->initialized) {
179 r = zd_usb_init_hw(usb);
184 tasklet_enable(&mac->rx_tasklet);
186 r = zd_chip_enable_int(chip);
190 r = zd_write_mac_addr(chip, netdev->dev_addr);
194 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
200 r = zd_chip_switch_radio_on(chip);
203 r = zd_chip_set_channel(chip, mac->requested_channel);
206 r = zd_chip_enable_rx(chip);
209 r = zd_chip_enable_hwint(chip);
213 housekeeping_enable(mac);
214 ieee80211softmac_start(netdev);
217 zd_chip_disable_rx(chip);
219 zd_chip_switch_radio_off(chip);
221 zd_chip_disable_int(chip);
226 int zd_mac_stop(struct net_device *netdev)
228 struct zd_mac *mac = zd_netdev_mac(netdev);
229 struct zd_chip *chip = &mac->chip;
231 netif_stop_queue(netdev);
234 * The order here deliberately is a little different from the open()
235 * method, since we need to make sure there is no opportunity for RX
236 * frames to be processed by softmac after we have stopped it.
239 zd_chip_disable_rx(chip);
240 skb_queue_purge(&mac->rx_queue);
241 tasklet_disable(&mac->rx_tasklet);
242 housekeeping_disable(mac);
243 ieee80211softmac_stop(netdev);
245 /* Ensure no work items are running or queued from this point */
246 cancel_delayed_work(&mac->set_rts_cts_work);
247 cancel_delayed_work(&mac->set_basic_rates_work);
248 flush_workqueue(zd_workqueue);
249 mac->updating_rts_rate = 0;
250 mac->updating_basic_rates = 0;
252 zd_chip_disable_hwint(chip);
253 zd_chip_switch_radio_off(chip);
254 zd_chip_disable_int(chip);
259 int zd_mac_set_mac_address(struct net_device *netdev, void *p)
263 struct sockaddr *addr = p;
264 struct zd_mac *mac = zd_netdev_mac(netdev);
265 struct zd_chip *chip = &mac->chip;
267 if (!is_valid_ether_addr(addr->sa_data))
268 return -EADDRNOTAVAIL;
270 dev_dbg_f(zd_mac_dev(mac),
271 "Setting MAC to " MAC_FMT "\n", MAC_ARG(addr->sa_data));
273 if (netdev->flags & IFF_UP) {
274 r = zd_write_mac_addr(chip, addr->sa_data);
279 spin_lock_irqsave(&mac->lock, flags);
280 memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
281 spin_unlock_irqrestore(&mac->lock, flags);
286 static void set_multicast_hash_handler(struct work_struct *work)
288 struct zd_mac *mac = container_of(work, struct zd_mac,
289 set_multicast_hash_work);
290 struct zd_mc_hash hash;
292 spin_lock_irq(&mac->lock);
293 hash = mac->multicast_hash;
294 spin_unlock_irq(&mac->lock);
296 zd_chip_set_multicast_hash(&mac->chip, &hash);
299 void zd_mac_set_multicast_list(struct net_device *dev)
301 struct zd_mc_hash hash;
302 struct zd_mac *mac = zd_netdev_mac(dev);
303 struct dev_mc_list *mc;
306 if (dev->flags & (IFF_PROMISC|IFF_ALLMULTI)) {
307 zd_mc_add_all(&hash);
310 for (mc = dev->mc_list; mc; mc = mc->next) {
311 dev_dbg_f(zd_mac_dev(mac), "mc addr " MAC_FMT "\n",
312 MAC_ARG(mc->dmi_addr));
313 zd_mc_add_addr(&hash, mc->dmi_addr);
317 spin_lock_irqsave(&mac->lock, flags);
318 mac->multicast_hash = hash;
319 spin_unlock_irqrestore(&mac->lock, flags);
320 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
323 int zd_mac_set_regdomain(struct zd_mac *mac, u8 regdomain)
328 ZD_ASSERT(!irqs_disabled());
329 spin_lock_irq(&mac->lock);
330 if (regdomain == 0) {
331 regdomain = mac->default_regdomain;
333 if (!zd_regdomain_supported(regdomain)) {
334 spin_unlock_irq(&mac->lock);
337 mac->regdomain = regdomain;
338 channel = mac->requested_channel;
339 spin_unlock_irq(&mac->lock);
341 r = zd_geo_init(zd_mac_to_ieee80211(mac), regdomain);
344 if (!zd_regdomain_supports_channel(regdomain, channel)) {
345 r = reset_channel(mac);
353 u8 zd_mac_get_regdomain(struct zd_mac *mac)
358 spin_lock_irqsave(&mac->lock, flags);
359 regdomain = mac->regdomain;
360 spin_unlock_irqrestore(&mac->lock, flags);
364 /* Fallback to lowest rate, if rate is unknown. */
365 static u8 rate_to_zd_rate(u8 rate)
368 case IEEE80211_CCK_RATE_2MB:
369 return ZD_CCK_RATE_2M;
370 case IEEE80211_CCK_RATE_5MB:
371 return ZD_CCK_RATE_5_5M;
372 case IEEE80211_CCK_RATE_11MB:
373 return ZD_CCK_RATE_11M;
374 case IEEE80211_OFDM_RATE_6MB:
375 return ZD_OFDM_RATE_6M;
376 case IEEE80211_OFDM_RATE_9MB:
377 return ZD_OFDM_RATE_9M;
378 case IEEE80211_OFDM_RATE_12MB:
379 return ZD_OFDM_RATE_12M;
380 case IEEE80211_OFDM_RATE_18MB:
381 return ZD_OFDM_RATE_18M;
382 case IEEE80211_OFDM_RATE_24MB:
383 return ZD_OFDM_RATE_24M;
384 case IEEE80211_OFDM_RATE_36MB:
385 return ZD_OFDM_RATE_36M;
386 case IEEE80211_OFDM_RATE_48MB:
387 return ZD_OFDM_RATE_48M;
388 case IEEE80211_OFDM_RATE_54MB:
389 return ZD_OFDM_RATE_54M;
391 return ZD_CCK_RATE_1M;
394 static u16 rate_to_cr_rate(u8 rate)
397 case IEEE80211_CCK_RATE_2MB:
399 case IEEE80211_CCK_RATE_5MB:
401 case IEEE80211_CCK_RATE_11MB:
403 case IEEE80211_OFDM_RATE_6MB:
405 case IEEE80211_OFDM_RATE_9MB:
407 case IEEE80211_OFDM_RATE_12MB:
409 case IEEE80211_OFDM_RATE_18MB:
411 case IEEE80211_OFDM_RATE_24MB:
413 case IEEE80211_OFDM_RATE_36MB:
415 case IEEE80211_OFDM_RATE_48MB:
417 case IEEE80211_OFDM_RATE_54MB:
423 static void try_enable_tx(struct zd_mac *mac)
427 spin_lock_irqsave(&mac->lock, flags);
428 if (mac->updating_rts_rate == 0 && mac->updating_basic_rates == 0)
429 netif_wake_queue(mac->netdev);
430 spin_unlock_irqrestore(&mac->lock, flags);
433 static void set_rts_cts_work(struct work_struct *work)
436 container_of(work, struct zd_mac, set_rts_cts_work.work);
439 unsigned int short_preamble;
441 mutex_lock(&mac->chip.mutex);
443 spin_lock_irqsave(&mac->lock, flags);
444 mac->updating_rts_rate = 0;
445 rts_rate = mac->rts_rate;
446 short_preamble = mac->short_preamble;
447 spin_unlock_irqrestore(&mac->lock, flags);
449 zd_chip_set_rts_cts_rate_locked(&mac->chip, rts_rate, short_preamble);
450 mutex_unlock(&mac->chip.mutex);
455 static void set_basic_rates_work(struct work_struct *work)
458 container_of(work, struct zd_mac, set_basic_rates_work.work);
462 mutex_lock(&mac->chip.mutex);
464 spin_lock_irqsave(&mac->lock, flags);
465 mac->updating_basic_rates = 0;
466 basic_rates = mac->basic_rates;
467 spin_unlock_irqrestore(&mac->lock, flags);
469 zd_chip_set_basic_rates_locked(&mac->chip, basic_rates);
470 mutex_unlock(&mac->chip.mutex);
475 static void bssinfo_change(struct net_device *netdev, u32 changes)
477 struct zd_mac *mac = zd_netdev_mac(netdev);
478 struct ieee80211softmac_device *softmac = ieee80211_priv(netdev);
479 struct ieee80211softmac_bss_info *bssinfo = &softmac->bssinfo;
480 int need_set_rts_cts = 0;
481 int need_set_rates = 0;
485 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
487 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_SHORT_PREAMBLE) {
488 spin_lock_irqsave(&mac->lock, flags);
489 mac->short_preamble = bssinfo->short_preamble;
490 spin_unlock_irqrestore(&mac->lock, flags);
491 need_set_rts_cts = 1;
494 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_RATES) {
495 /* Set RTS rate to highest available basic rate */
496 u8 hi_rate = ieee80211softmac_highest_supported_rate(softmac,
497 &bssinfo->supported_rates, 1);
498 hi_rate = rate_to_zd_rate(hi_rate);
500 spin_lock_irqsave(&mac->lock, flags);
501 if (hi_rate != mac->rts_rate) {
502 mac->rts_rate = hi_rate;
503 need_set_rts_cts = 1;
505 spin_unlock_irqrestore(&mac->lock, flags);
507 /* Set basic rates */
509 if (bssinfo->supported_rates.count == 0) {
510 /* Allow the device to be flexible */
511 basic_rates = CR_RATES_80211B | CR_RATES_80211G;
516 for (i = 0; i < bssinfo->supported_rates.count; i++) {
517 u16 rate = bssinfo->supported_rates.rates[i];
518 if ((rate & IEEE80211_BASIC_RATE_MASK) == 0)
521 rate &= ~IEEE80211_BASIC_RATE_MASK;
522 basic_rates |= rate_to_cr_rate(rate);
525 spin_lock_irqsave(&mac->lock, flags);
526 mac->basic_rates = basic_rates;
527 spin_unlock_irqrestore(&mac->lock, flags);
530 /* Schedule any changes we made above */
532 spin_lock_irqsave(&mac->lock, flags);
533 if (need_set_rts_cts && !mac->updating_rts_rate) {
534 mac->updating_rts_rate = 1;
535 netif_stop_queue(mac->netdev);
536 queue_delayed_work(zd_workqueue, &mac->set_rts_cts_work, 0);
538 if (need_set_rates && !mac->updating_basic_rates) {
539 mac->updating_basic_rates = 1;
540 netif_stop_queue(mac->netdev);
541 queue_delayed_work(zd_workqueue, &mac->set_basic_rates_work,
544 spin_unlock_irqrestore(&mac->lock, flags);
547 static void set_channel(struct net_device *netdev, u8 channel)
549 struct zd_mac *mac = zd_netdev_mac(netdev);
551 dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
553 zd_chip_set_channel(&mac->chip, channel);
556 int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
558 unsigned long lock_flags;
559 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
561 if (ieee->iw_mode == IW_MODE_INFRA)
564 spin_lock_irqsave(&mac->lock, lock_flags);
565 if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
566 spin_unlock_irqrestore(&mac->lock, lock_flags);
569 mac->requested_channel = channel;
570 spin_unlock_irqrestore(&mac->lock, lock_flags);
571 if (netif_running(mac->netdev))
572 return zd_chip_set_channel(&mac->chip, channel);
577 u8 zd_mac_get_channel(struct zd_mac *mac)
579 u8 channel = zd_chip_get_channel(&mac->chip);
581 dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
585 /* If wrong rate is given, we are falling back to the slowest rate: 1MBit/s */
586 static u8 zd_rate_typed(u8 zd_rate)
588 static const u8 typed_rates[16] = {
589 [ZD_CCK_RATE_1M] = ZD_CS_CCK|ZD_CCK_RATE_1M,
590 [ZD_CCK_RATE_2M] = ZD_CS_CCK|ZD_CCK_RATE_2M,
591 [ZD_CCK_RATE_5_5M] = ZD_CS_CCK|ZD_CCK_RATE_5_5M,
592 [ZD_CCK_RATE_11M] = ZD_CS_CCK|ZD_CCK_RATE_11M,
593 [ZD_OFDM_RATE_6M] = ZD_CS_OFDM|ZD_OFDM_RATE_6M,
594 [ZD_OFDM_RATE_9M] = ZD_CS_OFDM|ZD_OFDM_RATE_9M,
595 [ZD_OFDM_RATE_12M] = ZD_CS_OFDM|ZD_OFDM_RATE_12M,
596 [ZD_OFDM_RATE_18M] = ZD_CS_OFDM|ZD_OFDM_RATE_18M,
597 [ZD_OFDM_RATE_24M] = ZD_CS_OFDM|ZD_OFDM_RATE_24M,
598 [ZD_OFDM_RATE_36M] = ZD_CS_OFDM|ZD_OFDM_RATE_36M,
599 [ZD_OFDM_RATE_48M] = ZD_CS_OFDM|ZD_OFDM_RATE_48M,
600 [ZD_OFDM_RATE_54M] = ZD_CS_OFDM|ZD_OFDM_RATE_54M,
603 ZD_ASSERT(ZD_CS_RATE_MASK == 0x0f);
604 return typed_rates[zd_rate & ZD_CS_RATE_MASK];
607 int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
609 struct ieee80211_device *ieee;
615 mac->netdev->type = ARPHRD_ETHER;
617 case IW_MODE_MONITOR:
618 mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
621 dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
625 ieee = zd_mac_to_ieee80211(mac);
626 ZD_ASSERT(!irqs_disabled());
627 spin_lock_irq(&ieee->lock);
628 ieee->iw_mode = mode;
629 spin_unlock_irq(&ieee->lock);
631 if (netif_running(mac->netdev))
632 return reset_mode(mac);
637 int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
640 struct ieee80211_device *ieee;
642 ieee = zd_mac_to_ieee80211(mac);
643 spin_lock_irqsave(&ieee->lock, flags);
644 *mode = ieee->iw_mode;
645 spin_unlock_irqrestore(&ieee->lock, flags);
649 int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
652 const struct channel_range *channel_range;
655 memset(range, 0, sizeof(*range));
657 /* FIXME: Not so important and depends on the mode. For 802.11g
658 * usually this value is used. It seems to be that Bit/s number is
661 range->throughput = 27 * 1000 * 1000;
663 range->max_qual.qual = 100;
664 range->max_qual.level = 100;
666 /* FIXME: Needs still to be tuned. */
667 range->avg_qual.qual = 71;
668 range->avg_qual.level = 80;
670 /* FIXME: depends on standard? */
671 range->min_rts = 256;
672 range->max_rts = 2346;
674 range->min_frag = MIN_FRAG_THRESHOLD;
675 range->max_frag = MAX_FRAG_THRESHOLD;
677 range->max_encoding_tokens = WEP_KEYS;
678 range->num_encoding_sizes = 2;
679 range->encoding_size[0] = 5;
680 range->encoding_size[1] = WEP_KEY_LEN;
682 range->we_version_compiled = WIRELESS_EXT;
683 range->we_version_source = 20;
685 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
686 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
688 ZD_ASSERT(!irqs_disabled());
689 spin_lock_irq(&mac->lock);
690 regdomain = mac->regdomain;
691 spin_unlock_irq(&mac->lock);
692 channel_range = zd_channel_range(regdomain);
694 range->num_channels = channel_range->end - channel_range->start;
695 range->old_num_channels = range->num_channels;
696 range->num_frequency = range->num_channels;
697 range->old_num_frequency = range->num_frequency;
699 for (i = 0; i < range->num_frequency; i++) {
700 struct iw_freq *freq = &range->freq[i];
701 freq->i = channel_range->start + i;
702 zd_channel_to_freq(freq, freq->i);
708 static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
710 static const u8 rate_divisor[] = {
711 [ZD_CCK_RATE_1M] = 1,
712 [ZD_CCK_RATE_2M] = 2,
713 [ZD_CCK_RATE_5_5M] = 11, /* bits must be doubled */
714 [ZD_CCK_RATE_11M] = 11,
715 [ZD_OFDM_RATE_6M] = 6,
716 [ZD_OFDM_RATE_9M] = 9,
717 [ZD_OFDM_RATE_12M] = 12,
718 [ZD_OFDM_RATE_18M] = 18,
719 [ZD_OFDM_RATE_24M] = 24,
720 [ZD_OFDM_RATE_36M] = 36,
721 [ZD_OFDM_RATE_48M] = 48,
722 [ZD_OFDM_RATE_54M] = 54,
725 u32 bits = (u32)tx_length * 8;
728 divisor = rate_divisor[zd_rate];
733 case ZD_CCK_RATE_5_5M:
734 bits = (2*bits) + 10; /* round up to the next integer */
736 case ZD_CCK_RATE_11M:
739 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
740 if (0 < t && t <= 3) {
741 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
744 bits += 10; /* round up to the next integer */
752 R2M_SHORT_PREAMBLE = 0x01,
756 static u8 zd_rate_to_modulation(u8 zd_rate, int flags)
760 modulation = zd_rate_typed(zd_rate);
761 if (flags & R2M_SHORT_PREAMBLE) {
762 switch (ZD_CS_RATE(modulation)) {
764 case ZD_CCK_RATE_5_5M:
765 case ZD_CCK_RATE_11M:
766 modulation |= ZD_CS_CCK_PREA_SHORT;
770 if (flags & R2M_11A) {
771 if (ZD_CS_TYPE(modulation) == ZD_CS_OFDM)
772 modulation |= ZD_CS_OFDM_MODE_11A;
777 static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
778 struct ieee80211_hdr_4addr *hdr)
780 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
781 u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
783 int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
784 int is_multicast = is_multicast_ether_addr(hdr->addr1);
785 int short_preamble = ieee80211softmac_short_preamble_ok(softmac,
786 is_multicast, is_mgt);
789 /* FIXME: 802.11a? */
790 rate = ieee80211softmac_suggest_txrate(softmac, is_multicast, is_mgt);
793 flags |= R2M_SHORT_PREAMBLE;
795 zd_rate = rate_to_zd_rate(rate);
796 cs->modulation = zd_rate_to_modulation(zd_rate, flags);
799 static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
800 struct ieee80211_hdr_4addr *header)
802 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
803 unsigned int tx_length = le16_to_cpu(cs->tx_length);
804 u16 fctl = le16_to_cpu(header->frame_ctl);
805 u16 ftype = WLAN_FC_GET_TYPE(fctl);
806 u16 stype = WLAN_FC_GET_STYPE(fctl);
810 * - if backoff needed, enable bit 0
811 * - if burst (backoff not needed) disable bit 0
817 if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
818 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
821 if (is_multicast_ether_addr(header->addr1))
822 cs->control |= ZD_CS_MULTICAST;
825 if (stype == IEEE80211_STYPE_PSPOLL)
826 cs->control |= ZD_CS_PS_POLL_FRAME;
828 /* Unicast data frames over the threshold should have RTS */
829 if (!is_multicast_ether_addr(header->addr1) &&
830 ftype != IEEE80211_FTYPE_MGMT &&
831 tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
832 cs->control |= ZD_CS_RTS;
834 /* Use CTS-to-self protection if required */
835 if (ZD_CS_TYPE(cs->modulation) == ZD_CS_OFDM &&
836 ieee80211softmac_protection_needed(softmac)) {
837 /* FIXME: avoid sending RTS *and* self-CTS, is that correct? */
838 cs->control &= ~ZD_CS_RTS;
839 cs->control |= ZD_CS_SELF_CTS;
842 /* FIXME: Management frame? */
845 static int fill_ctrlset(struct zd_mac *mac,
846 struct ieee80211_txb *txb,
850 struct sk_buff *skb = txb->fragments[frag_num];
851 struct ieee80211_hdr_4addr *hdr =
852 (struct ieee80211_hdr_4addr *) skb->data;
853 unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
854 unsigned int next_frag_len;
855 unsigned int packet_length;
856 struct zd_ctrlset *cs = (struct zd_ctrlset *)
857 skb_push(skb, sizeof(struct zd_ctrlset));
859 if (frag_num+1 < txb->nr_frags) {
860 next_frag_len = txb->fragments[frag_num+1]->len +
865 ZD_ASSERT(frag_len <= 0xffff);
866 ZD_ASSERT(next_frag_len <= 0xffff);
868 cs_set_modulation(mac, cs, hdr);
870 cs->tx_length = cpu_to_le16(frag_len);
872 cs_set_control(mac, cs, hdr);
874 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
875 ZD_ASSERT(packet_length <= 0xffff);
876 /* ZD1211B: Computing the length difference this way, gives us
877 * flexibility to compute the packet length.
879 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
880 packet_length - frag_len : packet_length);
884 * - transmit frame length in microseconds
885 * - seems to be derived from frame length
886 * - see Cal_Us_Service() in zdinlinef.h
887 * - if macp->bTxBurstEnable is enabled, then multiply by 4
888 * - bTxBurstEnable is never set in the vendor driver
891 * - "for PLCP configuration"
892 * - always 0 except in some situations at 802.11b 11M
893 * - see line 53 of zdinlinef.h
896 r = zd_calc_tx_length_us(&cs->service, ZD_CS_RATE(cs->modulation),
897 le16_to_cpu(cs->tx_length));
900 cs->current_length = cpu_to_le16(r);
902 if (next_frag_len == 0) {
903 cs->next_frame_length = 0;
905 r = zd_calc_tx_length_us(NULL, ZD_CS_RATE(cs->modulation),
909 cs->next_frame_length = cpu_to_le16(r);
915 static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
918 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
920 for (i = 0; i < txb->nr_frags; i++) {
921 struct sk_buff *skb = txb->fragments[i];
923 r = fill_ctrlset(mac, txb, i);
925 ieee->stats.tx_dropped++;
928 r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
930 ieee->stats.tx_dropped++;
935 /* FIXME: shouldn't this be handled by the upper layers? */
936 mac->netdev->trans_start = jiffies;
938 ieee80211_txb_free(txb);
943 struct ieee80211_radiotap_header rt_hdr;
948 } __attribute__((packed));
950 static void fill_rt_header(void *buffer, struct zd_mac *mac,
951 const struct ieee80211_rx_stats *stats,
952 const struct rx_status *status)
954 struct zd_rt_hdr *hdr = buffer;
956 hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
957 hdr->rt_hdr.it_pad = 0;
958 hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
959 hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
960 (1 << IEEE80211_RADIOTAP_CHANNEL) |
961 (1 << IEEE80211_RADIOTAP_RATE));
964 if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
965 hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
967 hdr->rt_rate = stats->rate / 5;
970 hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
971 _zd_chip_get_channel(&mac->chip)));
972 hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
973 ((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
974 ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
977 /* Returns 1 if the data packet is for us and 0 otherwise. */
978 static int is_data_packet_for_us(struct ieee80211_device *ieee,
979 struct ieee80211_hdr_4addr *hdr)
981 struct net_device *netdev = ieee->dev;
982 u16 fc = le16_to_cpu(hdr->frame_ctl);
984 ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
986 switch (ieee->iw_mode) {
988 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
989 compare_ether_addr(hdr->addr3, ieee->bssid) != 0)
994 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
995 IEEE80211_FCTL_FROMDS ||
996 compare_ether_addr(hdr->addr2, ieee->bssid) != 0)
1000 ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
1004 return compare_ether_addr(hdr->addr1, netdev->dev_addr) == 0 ||
1005 (is_multicast_ether_addr(hdr->addr1) &&
1006 compare_ether_addr(hdr->addr3, netdev->dev_addr) != 0) ||
1007 (netdev->flags & IFF_PROMISC);
1010 /* Filters received packets. The function returns 1 if the packet should be
1011 * forwarded to ieee80211_rx(). If the packet should be ignored the function
1012 * returns 0. If an invalid packet is found the function returns -EINVAL.
1014 * The function calls ieee80211_rx_mgt() directly.
1016 * It has been based on ieee80211_rx_any.
1018 static int filter_rx(struct ieee80211_device *ieee,
1019 const u8 *buffer, unsigned int length,
1020 struct ieee80211_rx_stats *stats)
1022 struct ieee80211_hdr_4addr *hdr;
1025 if (ieee->iw_mode == IW_MODE_MONITOR)
1028 hdr = (struct ieee80211_hdr_4addr *)buffer;
1029 fc = le16_to_cpu(hdr->frame_ctl);
1030 if ((fc & IEEE80211_FCTL_VERS) != 0)
1033 switch (WLAN_FC_GET_TYPE(fc)) {
1034 case IEEE80211_FTYPE_MGMT:
1035 if (length < sizeof(struct ieee80211_hdr_3addr))
1037 ieee80211_rx_mgt(ieee, hdr, stats);
1039 case IEEE80211_FTYPE_CTL:
1041 case IEEE80211_FTYPE_DATA:
1042 /* Ignore invalid short buffers */
1043 if (length < sizeof(struct ieee80211_hdr_3addr))
1045 return is_data_packet_for_us(ieee, hdr);
1051 static void update_qual_rssi(struct zd_mac *mac,
1052 const u8 *buffer, unsigned int length,
1053 u8 qual_percent, u8 rssi_percent)
1055 unsigned long flags;
1056 struct ieee80211_hdr_3addr *hdr;
1059 hdr = (struct ieee80211_hdr_3addr *)buffer;
1060 if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
1062 if (compare_ether_addr(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid) != 0)
1065 spin_lock_irqsave(&mac->lock, flags);
1066 i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
1067 mac->qual_buffer[i] = qual_percent;
1068 mac->rssi_buffer[i] = rssi_percent;
1070 spin_unlock_irqrestore(&mac->lock, flags);
1073 static int fill_rx_stats(struct ieee80211_rx_stats *stats,
1074 const struct rx_status **pstatus,
1076 const u8 *buffer, unsigned int length)
1078 const struct rx_status *status;
1080 *pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
1081 if (status->frame_status & ZD_RX_ERROR) {
1082 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1083 ieee->stats.rx_errors++;
1084 if (status->frame_status & ZD_RX_TIMEOUT_ERROR)
1085 ieee->stats.rx_missed_errors++;
1086 else if (status->frame_status & ZD_RX_FIFO_OVERRUN_ERROR)
1087 ieee->stats.rx_fifo_errors++;
1088 else if (status->frame_status & ZD_RX_DECRYPTION_ERROR)
1089 ieee->ieee_stats.rx_discards_undecryptable++;
1090 else if (status->frame_status & ZD_RX_CRC32_ERROR) {
1091 ieee->stats.rx_crc_errors++;
1092 ieee->ieee_stats.rx_fcs_errors++;
1094 else if (status->frame_status & ZD_RX_CRC16_ERROR)
1095 ieee->stats.rx_crc_errors++;
1099 memset(stats, 0, sizeof(struct ieee80211_rx_stats));
1100 stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
1101 + sizeof(struct rx_status));
1102 /* FIXME: 802.11a */
1103 stats->freq = IEEE80211_24GHZ_BAND;
1104 stats->received_channel = _zd_chip_get_channel(&mac->chip);
1105 stats->rssi = zd_rx_strength_percent(status->signal_strength);
1106 stats->signal = zd_rx_qual_percent(buffer,
1107 length - sizeof(struct rx_status),
1109 stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
1110 stats->rate = zd_rx_rate(buffer, status);
1112 stats->mask |= IEEE80211_STATMASK_RATE;
1117 static void zd_mac_rx(struct zd_mac *mac, struct sk_buff *skb)
1120 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1121 struct ieee80211_rx_stats stats;
1122 const struct rx_status *status;
1124 if (skb->len < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
1125 IEEE80211_FCS_LEN + sizeof(struct rx_status))
1127 ieee->stats.rx_errors++;
1128 ieee->stats.rx_length_errors++;
1132 r = fill_rx_stats(&stats, &status, mac, skb->data, skb->len);
1134 /* Only packets with rx errors are included here.
1135 * The error stats have already been set in fill_rx_stats.
1140 __skb_pull(skb, ZD_PLCP_HEADER_SIZE);
1141 __skb_trim(skb, skb->len -
1142 (IEEE80211_FCS_LEN + sizeof(struct rx_status)));
1144 update_qual_rssi(mac, skb->data, skb->len, stats.signal,
1145 status->signal_strength);
1147 r = filter_rx(ieee, skb->data, skb->len, &stats);
1150 ieee->stats.rx_errors++;
1151 dev_dbg_f(zd_mac_dev(mac), "Error in packet.\n");
1156 if (ieee->iw_mode == IW_MODE_MONITOR)
1157 fill_rt_header(skb_push(skb, sizeof(struct zd_rt_hdr)), mac,
1160 r = ieee80211_rx(ieee, skb, &stats);
1164 /* We are always in a soft irq. */
1168 static void do_rx(unsigned long mac_ptr)
1170 struct zd_mac *mac = (struct zd_mac *)mac_ptr;
1171 struct sk_buff *skb;
1173 while ((skb = skb_dequeue(&mac->rx_queue)) != NULL)
1174 zd_mac_rx(mac, skb);
1177 int zd_mac_rx_irq(struct zd_mac *mac, const u8 *buffer, unsigned int length)
1179 struct sk_buff *skb;
1181 skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
1183 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1184 dev_warn(zd_mac_dev(mac), "Could not allocate skb.\n");
1185 ieee->stats.rx_dropped++;
1188 skb_reserve(skb, sizeof(struct zd_rt_hdr));
1189 memcpy(__skb_put(skb, length), buffer, length);
1190 skb_queue_tail(&mac->rx_queue, skb);
1191 tasklet_schedule(&mac->rx_tasklet);
1195 static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
1198 return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
1201 static void set_security(struct net_device *netdev,
1202 struct ieee80211_security *sec)
1204 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
1205 struct ieee80211_security *secinfo = &ieee->sec;
1208 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
1210 for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
1211 if (sec->flags & (1<<keyidx)) {
1212 secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
1213 secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
1214 memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
1218 if (sec->flags & SEC_ACTIVE_KEY) {
1219 secinfo->active_key = sec->active_key;
1220 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1221 " .active_key = %d\n", sec->active_key);
1223 if (sec->flags & SEC_UNICAST_GROUP) {
1224 secinfo->unicast_uses_group = sec->unicast_uses_group;
1225 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1226 " .unicast_uses_group = %d\n",
1227 sec->unicast_uses_group);
1229 if (sec->flags & SEC_LEVEL) {
1230 secinfo->level = sec->level;
1231 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1232 " .level = %d\n", sec->level);
1234 if (sec->flags & SEC_ENABLED) {
1235 secinfo->enabled = sec->enabled;
1236 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1237 " .enabled = %d\n", sec->enabled);
1239 if (sec->flags & SEC_ENCRYPT) {
1240 secinfo->encrypt = sec->encrypt;
1241 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1242 " .encrypt = %d\n", sec->encrypt);
1244 if (sec->flags & SEC_AUTH_MODE) {
1245 secinfo->auth_mode = sec->auth_mode;
1246 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1247 " .auth_mode = %d\n", sec->auth_mode);
1251 static void ieee_init(struct ieee80211_device *ieee)
1253 ieee->mode = IEEE_B | IEEE_G;
1254 ieee->freq_band = IEEE80211_24GHZ_BAND;
1255 ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
1256 ieee->tx_headroom = sizeof(struct zd_ctrlset);
1257 ieee->set_security = set_security;
1258 ieee->hard_start_xmit = netdev_tx;
1260 /* Software encryption/decryption for now */
1261 ieee->host_build_iv = 0;
1262 ieee->host_encrypt = 1;
1263 ieee->host_decrypt = 1;
1265 /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
1266 * correctly support AUTO */
1267 ieee->iw_mode = IW_MODE_INFRA;
1270 static void softmac_init(struct ieee80211softmac_device *sm)
1272 sm->set_channel = set_channel;
1273 sm->bssinfo_change = bssinfo_change;
1276 struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
1278 struct zd_mac *mac = zd_netdev_mac(ndev);
1279 struct iw_statistics *iw_stats = &mac->iw_stats;
1280 unsigned int i, count, qual_total, rssi_total;
1282 memset(iw_stats, 0, sizeof(struct iw_statistics));
1283 /* We are not setting the status, because ieee->state is not updated
1284 * at all and this driver doesn't track authentication state.
1286 spin_lock_irq(&mac->lock);
1287 count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
1288 mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
1289 qual_total = rssi_total = 0;
1290 for (i = 0; i < count; i++) {
1291 qual_total += mac->qual_buffer[i];
1292 rssi_total += mac->rssi_buffer[i];
1294 spin_unlock_irq(&mac->lock);
1295 iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
1297 iw_stats->qual.qual = qual_total / count;
1298 iw_stats->qual.level = rssi_total / count;
1299 iw_stats->qual.updated |=
1300 IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
1302 iw_stats->qual.updated |=
1303 IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
1305 /* TODO: update counter */
1309 #define LINK_LED_WORK_DELAY HZ
1311 static void link_led_handler(struct work_struct *work)
1313 struct zd_mac *mac =
1314 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
1315 struct zd_chip *chip = &mac->chip;
1316 struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
1320 spin_lock_irq(&mac->lock);
1321 is_associated = sm->associnfo.associated != 0;
1322 spin_unlock_irq(&mac->lock);
1324 r = zd_chip_control_leds(chip,
1325 is_associated ? LED_ASSOCIATED : LED_SCANNING);
1327 dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1329 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1330 LINK_LED_WORK_DELAY);
1333 static void housekeeping_init(struct zd_mac *mac)
1335 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
1338 static void housekeeping_enable(struct zd_mac *mac)
1340 dev_dbg_f(zd_mac_dev(mac), "\n");
1341 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1345 static void housekeeping_disable(struct zd_mac *mac)
1347 dev_dbg_f(zd_mac_dev(mac), "\n");
1348 cancel_rearming_delayed_workqueue(zd_workqueue,
1349 &mac->housekeeping.link_led_work);
1350 zd_chip_control_leds(&mac->chip, LED_OFF);