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"
32 static void ieee_init(struct ieee80211_device *ieee);
33 static void softmac_init(struct ieee80211softmac_device *sm);
34 static void set_rts_cts_work(struct work_struct *work);
35 static void set_basic_rates_work(struct work_struct *work);
37 static void housekeeping_init(struct zd_mac *mac);
38 static void housekeeping_enable(struct zd_mac *mac);
39 static void housekeeping_disable(struct zd_mac *mac);
41 static void set_multicast_hash_handler(struct work_struct *work);
43 static void do_rx(unsigned long mac_ptr);
45 int zd_mac_init(struct zd_mac *mac,
46 struct net_device *netdev,
47 struct usb_interface *intf)
49 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
51 memset(mac, 0, sizeof(*mac));
52 spin_lock_init(&mac->lock);
54 INIT_DELAYED_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
55 INIT_DELAYED_WORK(&mac->set_basic_rates_work, set_basic_rates_work);
57 skb_queue_head_init(&mac->rx_queue);
58 tasklet_init(&mac->rx_tasklet, do_rx, (unsigned long)mac);
59 tasklet_disable(&mac->rx_tasklet);
62 softmac_init(ieee80211_priv(netdev));
63 zd_chip_init(&mac->chip, netdev, intf);
64 housekeeping_init(mac);
65 INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
69 static int reset_channel(struct zd_mac *mac)
73 const struct channel_range *range;
75 spin_lock_irqsave(&mac->lock, flags);
76 range = zd_channel_range(mac->regdomain);
81 mac->requested_channel = range->start;
84 spin_unlock_irqrestore(&mac->lock, flags);
88 int zd_mac_preinit_hw(struct zd_mac *mac)
93 r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
97 memcpy(mac->netdev->dev_addr, addr, ETH_ALEN);
101 int zd_mac_init_hw(struct zd_mac *mac)
104 struct zd_chip *chip = &mac->chip;
105 u8 default_regdomain;
107 r = zd_chip_enable_int(chip);
110 r = zd_chip_init_hw(chip);
114 ZD_ASSERT(!irqs_disabled());
116 r = zd_read_regdomain(chip, &default_regdomain);
119 if (!zd_regdomain_supported(default_regdomain)) {
120 /* The vendor driver overrides the regulatory domain and
121 * allowed channel registers and unconditionally restricts
122 * available channels to 1-11 everywhere. Match their
123 * questionable behaviour only for regdomains which we don't
125 dev_warn(zd_mac_dev(mac), "Unrecognised regulatory domain: "
126 "%#04x. Defaulting to FCC.\n", default_regdomain);
127 default_regdomain = ZD_REGDOMAIN_FCC;
129 spin_lock_irq(&mac->lock);
130 mac->regdomain = mac->default_regdomain = default_regdomain;
131 spin_unlock_irq(&mac->lock);
132 r = reset_channel(mac);
136 /* We must inform the device that we are doing encryption/decryption in
137 * software at the moment. */
138 r = zd_set_encryption_type(chip, ENC_SNIFFER);
142 r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
148 zd_chip_disable_int(chip);
153 void zd_mac_clear(struct zd_mac *mac)
155 flush_workqueue(zd_workqueue);
156 skb_queue_purge(&mac->rx_queue);
157 tasklet_kill(&mac->rx_tasklet);
158 zd_chip_clear(&mac->chip);
159 ZD_ASSERT(!spin_is_locked(&mac->lock));
160 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
163 static int set_rx_filter(struct zd_mac *mac)
165 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
166 u32 filter = (ieee->iw_mode == IW_MODE_MONITOR) ? ~0 : STA_RX_FILTER;
167 return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
170 static int set_sniffer(struct zd_mac *mac)
172 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
173 return zd_iowrite32(&mac->chip, CR_SNIFFER_ON,
174 ieee->iw_mode == IW_MODE_MONITOR ? 1 : 0);
178 static int set_mc_hash(struct zd_mac *mac)
180 struct zd_mc_hash hash;
181 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
184 if (ieee->iw_mode == IW_MODE_MONITOR)
185 zd_mc_add_all(&hash);
187 return zd_chip_set_multicast_hash(&mac->chip, &hash);
190 int zd_mac_open(struct net_device *netdev)
192 struct zd_mac *mac = zd_netdev_mac(netdev);
193 struct zd_chip *chip = &mac->chip;
194 struct zd_usb *usb = &chip->usb;
197 if (!usb->initialized) {
198 r = zd_usb_init_hw(usb);
203 tasklet_enable(&mac->rx_tasklet);
205 r = zd_chip_enable_int(chip);
209 r = zd_write_mac_addr(chip, netdev->dev_addr);
213 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
216 r = set_rx_filter(mac);
219 r = set_sniffer(mac);
222 r = set_mc_hash(mac);
225 r = zd_chip_switch_radio_on(chip);
228 r = zd_chip_set_channel(chip, mac->requested_channel);
231 r = zd_chip_enable_rx(chip);
234 r = zd_chip_enable_hwint(chip);
238 housekeeping_enable(mac);
239 ieee80211softmac_start(netdev);
242 zd_chip_disable_rx(chip);
244 zd_chip_switch_radio_off(chip);
246 zd_chip_disable_int(chip);
251 int zd_mac_stop(struct net_device *netdev)
253 struct zd_mac *mac = zd_netdev_mac(netdev);
254 struct zd_chip *chip = &mac->chip;
256 netif_stop_queue(netdev);
259 * The order here deliberately is a little different from the open()
260 * method, since we need to make sure there is no opportunity for RX
261 * frames to be processed by softmac after we have stopped it.
264 zd_chip_disable_rx(chip);
265 skb_queue_purge(&mac->rx_queue);
266 tasklet_disable(&mac->rx_tasklet);
267 housekeeping_disable(mac);
268 ieee80211softmac_stop(netdev);
270 /* Ensure no work items are running or queued from this point */
271 cancel_delayed_work(&mac->set_rts_cts_work);
272 cancel_delayed_work(&mac->set_basic_rates_work);
273 flush_workqueue(zd_workqueue);
274 mac->updating_rts_rate = 0;
275 mac->updating_basic_rates = 0;
277 zd_chip_disable_hwint(chip);
278 zd_chip_switch_radio_off(chip);
279 zd_chip_disable_int(chip);
284 int zd_mac_set_mac_address(struct net_device *netdev, void *p)
288 struct sockaddr *addr = p;
289 struct zd_mac *mac = zd_netdev_mac(netdev);
290 struct zd_chip *chip = &mac->chip;
291 DECLARE_MAC_BUF(mac2);
293 if (!is_valid_ether_addr(addr->sa_data))
294 return -EADDRNOTAVAIL;
296 dev_dbg_f(zd_mac_dev(mac),
297 "Setting MAC to %s\n", print_mac(mac2, addr->sa_data));
299 if (netdev->flags & IFF_UP) {
300 r = zd_write_mac_addr(chip, addr->sa_data);
305 spin_lock_irqsave(&mac->lock, flags);
306 memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
307 spin_unlock_irqrestore(&mac->lock, flags);
312 static void set_multicast_hash_handler(struct work_struct *work)
314 struct zd_mac *mac = container_of(work, struct zd_mac,
315 set_multicast_hash_work);
316 struct zd_mc_hash hash;
318 spin_lock_irq(&mac->lock);
319 hash = mac->multicast_hash;
320 spin_unlock_irq(&mac->lock);
322 zd_chip_set_multicast_hash(&mac->chip, &hash);
325 void zd_mac_set_multicast_list(struct net_device *dev)
327 struct zd_mac *mac = zd_netdev_mac(dev);
328 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
329 struct zd_mc_hash hash;
330 struct dev_mc_list *mc;
332 DECLARE_MAC_BUF(mac2);
334 if (dev->flags & (IFF_PROMISC|IFF_ALLMULTI) ||
335 ieee->iw_mode == IW_MODE_MONITOR) {
336 zd_mc_add_all(&hash);
339 for (mc = dev->mc_list; mc; mc = mc->next) {
340 dev_dbg_f(zd_mac_dev(mac), "mc addr %s\n",
341 print_mac(mac2, mc->dmi_addr));
342 zd_mc_add_addr(&hash, mc->dmi_addr);
346 spin_lock_irqsave(&mac->lock, flags);
347 mac->multicast_hash = hash;
348 spin_unlock_irqrestore(&mac->lock, flags);
349 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
352 int zd_mac_set_regdomain(struct zd_mac *mac, u8 regdomain)
357 ZD_ASSERT(!irqs_disabled());
358 spin_lock_irq(&mac->lock);
359 if (regdomain == 0) {
360 regdomain = mac->default_regdomain;
362 if (!zd_regdomain_supported(regdomain)) {
363 spin_unlock_irq(&mac->lock);
366 mac->regdomain = regdomain;
367 channel = mac->requested_channel;
368 spin_unlock_irq(&mac->lock);
370 r = zd_geo_init(zd_mac_to_ieee80211(mac), regdomain);
373 if (!zd_regdomain_supports_channel(regdomain, channel)) {
374 r = reset_channel(mac);
382 u8 zd_mac_get_regdomain(struct zd_mac *mac)
387 spin_lock_irqsave(&mac->lock, flags);
388 regdomain = mac->regdomain;
389 spin_unlock_irqrestore(&mac->lock, flags);
393 /* Fallback to lowest rate, if rate is unknown. */
394 static u8 rate_to_zd_rate(u8 rate)
397 case IEEE80211_CCK_RATE_2MB:
398 return ZD_CCK_RATE_2M;
399 case IEEE80211_CCK_RATE_5MB:
400 return ZD_CCK_RATE_5_5M;
401 case IEEE80211_CCK_RATE_11MB:
402 return ZD_CCK_RATE_11M;
403 case IEEE80211_OFDM_RATE_6MB:
404 return ZD_OFDM_RATE_6M;
405 case IEEE80211_OFDM_RATE_9MB:
406 return ZD_OFDM_RATE_9M;
407 case IEEE80211_OFDM_RATE_12MB:
408 return ZD_OFDM_RATE_12M;
409 case IEEE80211_OFDM_RATE_18MB:
410 return ZD_OFDM_RATE_18M;
411 case IEEE80211_OFDM_RATE_24MB:
412 return ZD_OFDM_RATE_24M;
413 case IEEE80211_OFDM_RATE_36MB:
414 return ZD_OFDM_RATE_36M;
415 case IEEE80211_OFDM_RATE_48MB:
416 return ZD_OFDM_RATE_48M;
417 case IEEE80211_OFDM_RATE_54MB:
418 return ZD_OFDM_RATE_54M;
420 return ZD_CCK_RATE_1M;
423 static u16 rate_to_cr_rate(u8 rate)
426 case IEEE80211_CCK_RATE_2MB:
428 case IEEE80211_CCK_RATE_5MB:
430 case IEEE80211_CCK_RATE_11MB:
432 case IEEE80211_OFDM_RATE_6MB:
434 case IEEE80211_OFDM_RATE_9MB:
436 case IEEE80211_OFDM_RATE_12MB:
438 case IEEE80211_OFDM_RATE_18MB:
440 case IEEE80211_OFDM_RATE_24MB:
442 case IEEE80211_OFDM_RATE_36MB:
444 case IEEE80211_OFDM_RATE_48MB:
446 case IEEE80211_OFDM_RATE_54MB:
452 static void try_enable_tx(struct zd_mac *mac)
456 spin_lock_irqsave(&mac->lock, flags);
457 if (mac->updating_rts_rate == 0 && mac->updating_basic_rates == 0)
458 netif_wake_queue(mac->netdev);
459 spin_unlock_irqrestore(&mac->lock, flags);
462 static void set_rts_cts_work(struct work_struct *work)
465 container_of(work, struct zd_mac, set_rts_cts_work.work);
468 unsigned int short_preamble;
470 mutex_lock(&mac->chip.mutex);
472 spin_lock_irqsave(&mac->lock, flags);
473 mac->updating_rts_rate = 0;
474 rts_rate = mac->rts_rate;
475 short_preamble = mac->short_preamble;
476 spin_unlock_irqrestore(&mac->lock, flags);
478 zd_chip_set_rts_cts_rate_locked(&mac->chip, rts_rate, short_preamble);
479 mutex_unlock(&mac->chip.mutex);
484 static void set_basic_rates_work(struct work_struct *work)
487 container_of(work, struct zd_mac, set_basic_rates_work.work);
491 mutex_lock(&mac->chip.mutex);
493 spin_lock_irqsave(&mac->lock, flags);
494 mac->updating_basic_rates = 0;
495 basic_rates = mac->basic_rates;
496 spin_unlock_irqrestore(&mac->lock, flags);
498 zd_chip_set_basic_rates_locked(&mac->chip, basic_rates);
499 mutex_unlock(&mac->chip.mutex);
504 static void bssinfo_change(struct net_device *netdev, u32 changes)
506 struct zd_mac *mac = zd_netdev_mac(netdev);
507 struct ieee80211softmac_device *softmac = ieee80211_priv(netdev);
508 struct ieee80211softmac_bss_info *bssinfo = &softmac->bssinfo;
509 int need_set_rts_cts = 0;
510 int need_set_rates = 0;
514 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
516 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_SHORT_PREAMBLE) {
517 spin_lock_irqsave(&mac->lock, flags);
518 mac->short_preamble = bssinfo->short_preamble;
519 spin_unlock_irqrestore(&mac->lock, flags);
520 need_set_rts_cts = 1;
523 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_RATES) {
524 /* Set RTS rate to highest available basic rate */
525 u8 hi_rate = ieee80211softmac_highest_supported_rate(softmac,
526 &bssinfo->supported_rates, 1);
527 hi_rate = rate_to_zd_rate(hi_rate);
529 spin_lock_irqsave(&mac->lock, flags);
530 if (hi_rate != mac->rts_rate) {
531 mac->rts_rate = hi_rate;
532 need_set_rts_cts = 1;
534 spin_unlock_irqrestore(&mac->lock, flags);
536 /* Set basic rates */
538 if (bssinfo->supported_rates.count == 0) {
539 /* Allow the device to be flexible */
540 basic_rates = CR_RATES_80211B | CR_RATES_80211G;
545 for (i = 0; i < bssinfo->supported_rates.count; i++) {
546 u16 rate = bssinfo->supported_rates.rates[i];
547 if ((rate & IEEE80211_BASIC_RATE_MASK) == 0)
550 rate &= ~IEEE80211_BASIC_RATE_MASK;
551 basic_rates |= rate_to_cr_rate(rate);
554 spin_lock_irqsave(&mac->lock, flags);
555 mac->basic_rates = basic_rates;
556 spin_unlock_irqrestore(&mac->lock, flags);
559 /* Schedule any changes we made above */
561 spin_lock_irqsave(&mac->lock, flags);
562 if (need_set_rts_cts && !mac->updating_rts_rate) {
563 mac->updating_rts_rate = 1;
564 netif_stop_queue(mac->netdev);
565 queue_delayed_work(zd_workqueue, &mac->set_rts_cts_work, 0);
567 if (need_set_rates && !mac->updating_basic_rates) {
568 mac->updating_basic_rates = 1;
569 netif_stop_queue(mac->netdev);
570 queue_delayed_work(zd_workqueue, &mac->set_basic_rates_work,
573 spin_unlock_irqrestore(&mac->lock, flags);
576 static void set_channel(struct net_device *netdev, u8 channel)
578 struct zd_mac *mac = zd_netdev_mac(netdev);
580 dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
582 zd_chip_set_channel(&mac->chip, channel);
585 int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
587 unsigned long lock_flags;
588 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
590 if (ieee->iw_mode == IW_MODE_INFRA)
593 spin_lock_irqsave(&mac->lock, lock_flags);
594 if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
595 spin_unlock_irqrestore(&mac->lock, lock_flags);
598 mac->requested_channel = channel;
599 spin_unlock_irqrestore(&mac->lock, lock_flags);
600 if (netif_running(mac->netdev))
601 return zd_chip_set_channel(&mac->chip, channel);
606 u8 zd_mac_get_channel(struct zd_mac *mac)
608 u8 channel = zd_chip_get_channel(&mac->chip);
610 dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
614 int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
616 struct ieee80211_device *ieee;
622 mac->netdev->type = ARPHRD_ETHER;
624 case IW_MODE_MONITOR:
625 mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
628 dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
632 ieee = zd_mac_to_ieee80211(mac);
633 ZD_ASSERT(!irqs_disabled());
634 spin_lock_irq(&ieee->lock);
635 ieee->iw_mode = mode;
636 spin_unlock_irq(&ieee->lock);
638 if (netif_running(mac->netdev)) {
639 int r = set_rx_filter(mac);
642 return set_sniffer(mac);
648 int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
651 struct ieee80211_device *ieee;
653 ieee = zd_mac_to_ieee80211(mac);
654 spin_lock_irqsave(&ieee->lock, flags);
655 *mode = ieee->iw_mode;
656 spin_unlock_irqrestore(&ieee->lock, flags);
660 int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
663 const struct channel_range *channel_range;
666 memset(range, 0, sizeof(*range));
668 /* FIXME: Not so important and depends on the mode. For 802.11g
669 * usually this value is used. It seems to be that Bit/s number is
672 range->throughput = 27 * 1000 * 1000;
674 range->max_qual.qual = 100;
675 range->max_qual.level = 100;
677 /* FIXME: Needs still to be tuned. */
678 range->avg_qual.qual = 71;
679 range->avg_qual.level = 80;
681 /* FIXME: depends on standard? */
682 range->min_rts = 256;
683 range->max_rts = 2346;
685 range->min_frag = MIN_FRAG_THRESHOLD;
686 range->max_frag = MAX_FRAG_THRESHOLD;
688 range->max_encoding_tokens = WEP_KEYS;
689 range->num_encoding_sizes = 2;
690 range->encoding_size[0] = 5;
691 range->encoding_size[1] = WEP_KEY_LEN;
693 range->we_version_compiled = WIRELESS_EXT;
694 range->we_version_source = 20;
696 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
697 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
699 ZD_ASSERT(!irqs_disabled());
700 spin_lock_irq(&mac->lock);
701 regdomain = mac->regdomain;
702 spin_unlock_irq(&mac->lock);
703 channel_range = zd_channel_range(regdomain);
705 range->num_channels = channel_range->end - channel_range->start;
706 range->old_num_channels = range->num_channels;
707 range->num_frequency = range->num_channels;
708 range->old_num_frequency = range->num_frequency;
710 for (i = 0; i < range->num_frequency; i++) {
711 struct iw_freq *freq = &range->freq[i];
712 freq->i = channel_range->start + i;
713 zd_channel_to_freq(freq, freq->i);
719 static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
721 /* ZD_PURE_RATE() must be used to remove the modulation type flag of
722 * the zd-rate values. */
723 static const u8 rate_divisor[] = {
724 [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
725 [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
727 /* bits must be doubled */
728 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
730 [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
731 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
732 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
733 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
734 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
735 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
736 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
737 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
738 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
741 u32 bits = (u32)tx_length * 8;
744 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
749 case ZD_CCK_RATE_5_5M:
750 bits = (2*bits) + 10; /* round up to the next integer */
752 case ZD_CCK_RATE_11M:
755 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
756 if (0 < t && t <= 3) {
757 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
760 bits += 10; /* round up to the next integer */
767 static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
768 struct ieee80211_hdr_4addr *hdr)
770 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
771 u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
773 int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
774 int is_multicast = is_multicast_ether_addr(hdr->addr1);
775 int short_preamble = ieee80211softmac_short_preamble_ok(softmac,
776 is_multicast, is_mgt);
778 rate = ieee80211softmac_suggest_txrate(softmac, is_multicast, is_mgt);
779 cs->modulation = rate_to_zd_rate(rate);
781 /* Set short preamble bit when appropriate */
782 if (short_preamble && ZD_MODULATION_TYPE(cs->modulation) == ZD_CCK
783 && cs->modulation != ZD_CCK_RATE_1M)
784 cs->modulation |= ZD_CCK_PREA_SHORT;
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 (ftype == IEEE80211_FTYPE_CTL && 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_MODULATION_TYPE(cs->modulation) == ZD_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(zd_chip_is_zd1211b(&mac->chip) ?
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_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_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 = (struct rx_status *)
1069 (buffer + (length - sizeof(struct rx_status)));
1070 if (status->frame_status & ZD_RX_ERROR) {
1071 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1072 ieee->stats.rx_errors++;
1073 if (status->frame_status & ZD_RX_TIMEOUT_ERROR)
1074 ieee->stats.rx_missed_errors++;
1075 else if (status->frame_status & ZD_RX_FIFO_OVERRUN_ERROR)
1076 ieee->stats.rx_fifo_errors++;
1077 else if (status->frame_status & ZD_RX_DECRYPTION_ERROR)
1078 ieee->ieee_stats.rx_discards_undecryptable++;
1079 else if (status->frame_status & ZD_RX_CRC32_ERROR) {
1080 ieee->stats.rx_crc_errors++;
1081 ieee->ieee_stats.rx_fcs_errors++;
1083 else if (status->frame_status & ZD_RX_CRC16_ERROR)
1084 ieee->stats.rx_crc_errors++;
1088 memset(stats, 0, sizeof(struct ieee80211_rx_stats));
1089 stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
1090 + sizeof(struct rx_status));
1091 /* FIXME: 802.11a */
1092 stats->freq = IEEE80211_24GHZ_BAND;
1093 stats->received_channel = _zd_chip_get_channel(&mac->chip);
1094 stats->rssi = zd_rx_strength_percent(status->signal_strength);
1095 stats->signal = zd_rx_qual_percent(buffer,
1096 length - sizeof(struct rx_status),
1098 stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
1099 stats->rate = zd_rx_rate(buffer, status);
1101 stats->mask |= IEEE80211_STATMASK_RATE;
1106 static void zd_mac_rx(struct zd_mac *mac, struct sk_buff *skb)
1109 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1110 struct ieee80211_rx_stats stats;
1111 const struct rx_status *status;
1113 if (skb->len < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
1114 IEEE80211_FCS_LEN + sizeof(struct rx_status))
1116 ieee->stats.rx_errors++;
1117 ieee->stats.rx_length_errors++;
1121 r = fill_rx_stats(&stats, &status, mac, skb->data, skb->len);
1123 /* Only packets with rx errors are included here.
1124 * The error stats have already been set in fill_rx_stats.
1129 __skb_pull(skb, ZD_PLCP_HEADER_SIZE);
1130 __skb_trim(skb, skb->len -
1131 (IEEE80211_FCS_LEN + sizeof(struct rx_status)));
1133 ZD_ASSERT(IS_ALIGNED((unsigned long)skb->data, 4));
1135 update_qual_rssi(mac, skb->data, skb->len, stats.signal,
1136 status->signal_strength);
1138 r = filter_rx(ieee, skb->data, skb->len, &stats);
1141 ieee->stats.rx_errors++;
1142 dev_dbg_f(zd_mac_dev(mac), "Error in packet.\n");
1147 if (ieee->iw_mode == IW_MODE_MONITOR)
1148 fill_rt_header(skb_push(skb, sizeof(struct zd_rt_hdr)), mac,
1151 r = ieee80211_rx(ieee, skb, &stats);
1155 /* We are always in a soft irq. */
1159 static void do_rx(unsigned long mac_ptr)
1161 struct zd_mac *mac = (struct zd_mac *)mac_ptr;
1162 struct sk_buff *skb;
1164 while ((skb = skb_dequeue(&mac->rx_queue)) != NULL)
1165 zd_mac_rx(mac, skb);
1168 int zd_mac_rx_irq(struct zd_mac *mac, const u8 *buffer, unsigned int length)
1170 struct sk_buff *skb;
1171 unsigned int reserved =
1172 ALIGN(max_t(unsigned int,
1173 sizeof(struct zd_rt_hdr), ZD_PLCP_HEADER_SIZE), 4) -
1174 ZD_PLCP_HEADER_SIZE;
1176 skb = dev_alloc_skb(reserved + length);
1178 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1179 dev_warn(zd_mac_dev(mac), "Could not allocate skb.\n");
1180 ieee->stats.rx_dropped++;
1183 skb_reserve(skb, reserved);
1184 memcpy(__skb_put(skb, length), buffer, length);
1185 skb_queue_tail(&mac->rx_queue, skb);
1186 tasklet_schedule(&mac->rx_tasklet);
1190 static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
1193 return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
1196 static void set_security(struct net_device *netdev,
1197 struct ieee80211_security *sec)
1199 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
1200 struct ieee80211_security *secinfo = &ieee->sec;
1203 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
1205 for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
1206 if (sec->flags & (1<<keyidx)) {
1207 secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
1208 secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
1209 memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
1213 if (sec->flags & SEC_ACTIVE_KEY) {
1214 secinfo->active_key = sec->active_key;
1215 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1216 " .active_key = %d\n", sec->active_key);
1218 if (sec->flags & SEC_UNICAST_GROUP) {
1219 secinfo->unicast_uses_group = sec->unicast_uses_group;
1220 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1221 " .unicast_uses_group = %d\n",
1222 sec->unicast_uses_group);
1224 if (sec->flags & SEC_LEVEL) {
1225 secinfo->level = sec->level;
1226 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1227 " .level = %d\n", sec->level);
1229 if (sec->flags & SEC_ENABLED) {
1230 secinfo->enabled = sec->enabled;
1231 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1232 " .enabled = %d\n", sec->enabled);
1234 if (sec->flags & SEC_ENCRYPT) {
1235 secinfo->encrypt = sec->encrypt;
1236 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1237 " .encrypt = %d\n", sec->encrypt);
1239 if (sec->flags & SEC_AUTH_MODE) {
1240 secinfo->auth_mode = sec->auth_mode;
1241 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1242 " .auth_mode = %d\n", sec->auth_mode);
1246 static void ieee_init(struct ieee80211_device *ieee)
1248 ieee->mode = IEEE_B | IEEE_G;
1249 ieee->freq_band = IEEE80211_24GHZ_BAND;
1250 ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
1251 ieee->tx_headroom = sizeof(struct zd_ctrlset);
1252 ieee->set_security = set_security;
1253 ieee->hard_start_xmit = netdev_tx;
1255 /* Software encryption/decryption for now */
1256 ieee->host_build_iv = 0;
1257 ieee->host_encrypt = 1;
1258 ieee->host_decrypt = 1;
1260 /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
1261 * correctly support AUTO */
1262 ieee->iw_mode = IW_MODE_INFRA;
1265 static void softmac_init(struct ieee80211softmac_device *sm)
1267 sm->set_channel = set_channel;
1268 sm->bssinfo_change = bssinfo_change;
1271 struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
1273 struct zd_mac *mac = zd_netdev_mac(ndev);
1274 struct iw_statistics *iw_stats = &mac->iw_stats;
1275 unsigned int i, count, qual_total, rssi_total;
1277 memset(iw_stats, 0, sizeof(struct iw_statistics));
1278 /* We are not setting the status, because ieee->state is not updated
1279 * at all and this driver doesn't track authentication state.
1281 spin_lock_irq(&mac->lock);
1282 count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
1283 mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
1284 qual_total = rssi_total = 0;
1285 for (i = 0; i < count; i++) {
1286 qual_total += mac->qual_buffer[i];
1287 rssi_total += mac->rssi_buffer[i];
1289 spin_unlock_irq(&mac->lock);
1290 iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
1292 iw_stats->qual.qual = qual_total / count;
1293 iw_stats->qual.level = rssi_total / count;
1294 iw_stats->qual.updated |=
1295 IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
1297 iw_stats->qual.updated |=
1298 IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
1300 /* TODO: update counter */
1304 #define LINK_LED_WORK_DELAY HZ
1306 static void link_led_handler(struct work_struct *work)
1308 struct zd_mac *mac =
1309 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
1310 struct zd_chip *chip = &mac->chip;
1311 struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
1315 spin_lock_irq(&mac->lock);
1316 is_associated = sm->associnfo.associated != 0;
1317 spin_unlock_irq(&mac->lock);
1319 r = zd_chip_control_leds(chip,
1320 is_associated ? LED_ASSOCIATED : LED_SCANNING);
1322 dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1324 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1325 LINK_LED_WORK_DELAY);
1328 static void housekeeping_init(struct zd_mac *mac)
1330 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
1333 static void housekeeping_enable(struct zd_mac *mac)
1335 dev_dbg_f(zd_mac_dev(mac), "\n");
1336 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1340 static void housekeeping_disable(struct zd_mac *mac)
1342 dev_dbg_f(zd_mac_dev(mac), "\n");
1343 cancel_rearming_delayed_workqueue(zd_workqueue,
1344 &mac->housekeeping.link_led_work);
1345 zd_chip_control_leds(&mac->chip, LED_OFF);