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 set_rx_filter(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 static int set_sniffer(struct zd_mac *mac)
173 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
174 return zd_iowrite32(&mac->chip, CR_SNIFFER_ON,
175 ieee->iw_mode == IW_MODE_MONITOR ? 1 : 0);
179 static int set_mc_hash(struct zd_mac *mac)
181 struct zd_mc_hash hash;
182 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
185 if (ieee->iw_mode == IW_MODE_MONITOR)
186 zd_mc_add_all(&hash);
188 return zd_chip_set_multicast_hash(&mac->chip, &hash);
191 int zd_mac_open(struct net_device *netdev)
193 struct zd_mac *mac = zd_netdev_mac(netdev);
194 struct zd_chip *chip = &mac->chip;
195 struct zd_usb *usb = &chip->usb;
198 if (!usb->initialized) {
199 r = zd_usb_init_hw(usb);
204 tasklet_enable(&mac->rx_tasklet);
206 r = zd_chip_enable_int(chip);
210 r = zd_write_mac_addr(chip, netdev->dev_addr);
214 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
217 r = set_rx_filter(mac);
220 r = set_sniffer(mac);
223 r = set_mc_hash(mac);
226 r = zd_chip_switch_radio_on(chip);
229 r = zd_chip_set_channel(chip, mac->requested_channel);
232 r = zd_chip_enable_rx(chip);
235 r = zd_chip_enable_hwint(chip);
239 housekeeping_enable(mac);
240 ieee80211softmac_start(netdev);
243 zd_chip_disable_rx(chip);
245 zd_chip_switch_radio_off(chip);
247 zd_chip_disable_int(chip);
252 int zd_mac_stop(struct net_device *netdev)
254 struct zd_mac *mac = zd_netdev_mac(netdev);
255 struct zd_chip *chip = &mac->chip;
257 netif_stop_queue(netdev);
260 * The order here deliberately is a little different from the open()
261 * method, since we need to make sure there is no opportunity for RX
262 * frames to be processed by softmac after we have stopped it.
265 zd_chip_disable_rx(chip);
266 skb_queue_purge(&mac->rx_queue);
267 tasklet_disable(&mac->rx_tasklet);
268 housekeeping_disable(mac);
269 ieee80211softmac_stop(netdev);
271 /* Ensure no work items are running or queued from this point */
272 cancel_delayed_work(&mac->set_rts_cts_work);
273 cancel_delayed_work(&mac->set_basic_rates_work);
274 flush_workqueue(zd_workqueue);
275 mac->updating_rts_rate = 0;
276 mac->updating_basic_rates = 0;
278 zd_chip_disable_hwint(chip);
279 zd_chip_switch_radio_off(chip);
280 zd_chip_disable_int(chip);
285 int zd_mac_set_mac_address(struct net_device *netdev, void *p)
289 struct sockaddr *addr = p;
290 struct zd_mac *mac = zd_netdev_mac(netdev);
291 struct zd_chip *chip = &mac->chip;
293 if (!is_valid_ether_addr(addr->sa_data))
294 return -EADDRNOTAVAIL;
296 dev_dbg_f(zd_mac_dev(mac),
297 "Setting MAC to " MAC_FMT "\n", MAC_ARG(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;
333 if (dev->flags & (IFF_PROMISC|IFF_ALLMULTI) ||
334 ieee->iw_mode == IW_MODE_MONITOR) {
335 zd_mc_add_all(&hash);
338 for (mc = dev->mc_list; mc; mc = mc->next) {
339 dev_dbg_f(zd_mac_dev(mac), "mc addr " MAC_FMT "\n",
340 MAC_ARG(mc->dmi_addr));
341 zd_mc_add_addr(&hash, mc->dmi_addr);
345 spin_lock_irqsave(&mac->lock, flags);
346 mac->multicast_hash = hash;
347 spin_unlock_irqrestore(&mac->lock, flags);
348 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
351 int zd_mac_set_regdomain(struct zd_mac *mac, u8 regdomain)
356 ZD_ASSERT(!irqs_disabled());
357 spin_lock_irq(&mac->lock);
358 if (regdomain == 0) {
359 regdomain = mac->default_regdomain;
361 if (!zd_regdomain_supported(regdomain)) {
362 spin_unlock_irq(&mac->lock);
365 mac->regdomain = regdomain;
366 channel = mac->requested_channel;
367 spin_unlock_irq(&mac->lock);
369 r = zd_geo_init(zd_mac_to_ieee80211(mac), regdomain);
372 if (!zd_regdomain_supports_channel(regdomain, channel)) {
373 r = reset_channel(mac);
381 u8 zd_mac_get_regdomain(struct zd_mac *mac)
386 spin_lock_irqsave(&mac->lock, flags);
387 regdomain = mac->regdomain;
388 spin_unlock_irqrestore(&mac->lock, flags);
392 /* Fallback to lowest rate, if rate is unknown. */
393 static u8 rate_to_zd_rate(u8 rate)
396 case IEEE80211_CCK_RATE_2MB:
397 return ZD_CCK_RATE_2M;
398 case IEEE80211_CCK_RATE_5MB:
399 return ZD_CCK_RATE_5_5M;
400 case IEEE80211_CCK_RATE_11MB:
401 return ZD_CCK_RATE_11M;
402 case IEEE80211_OFDM_RATE_6MB:
403 return ZD_OFDM_RATE_6M;
404 case IEEE80211_OFDM_RATE_9MB:
405 return ZD_OFDM_RATE_9M;
406 case IEEE80211_OFDM_RATE_12MB:
407 return ZD_OFDM_RATE_12M;
408 case IEEE80211_OFDM_RATE_18MB:
409 return ZD_OFDM_RATE_18M;
410 case IEEE80211_OFDM_RATE_24MB:
411 return ZD_OFDM_RATE_24M;
412 case IEEE80211_OFDM_RATE_36MB:
413 return ZD_OFDM_RATE_36M;
414 case IEEE80211_OFDM_RATE_48MB:
415 return ZD_OFDM_RATE_48M;
416 case IEEE80211_OFDM_RATE_54MB:
417 return ZD_OFDM_RATE_54M;
419 return ZD_CCK_RATE_1M;
422 static u16 rate_to_cr_rate(u8 rate)
425 case IEEE80211_CCK_RATE_2MB:
427 case IEEE80211_CCK_RATE_5MB:
429 case IEEE80211_CCK_RATE_11MB:
431 case IEEE80211_OFDM_RATE_6MB:
433 case IEEE80211_OFDM_RATE_9MB:
435 case IEEE80211_OFDM_RATE_12MB:
437 case IEEE80211_OFDM_RATE_18MB:
439 case IEEE80211_OFDM_RATE_24MB:
441 case IEEE80211_OFDM_RATE_36MB:
443 case IEEE80211_OFDM_RATE_48MB:
445 case IEEE80211_OFDM_RATE_54MB:
451 static void try_enable_tx(struct zd_mac *mac)
455 spin_lock_irqsave(&mac->lock, flags);
456 if (mac->updating_rts_rate == 0 && mac->updating_basic_rates == 0)
457 netif_wake_queue(mac->netdev);
458 spin_unlock_irqrestore(&mac->lock, flags);
461 static void set_rts_cts_work(struct work_struct *work)
464 container_of(work, struct zd_mac, set_rts_cts_work.work);
467 unsigned int short_preamble;
469 mutex_lock(&mac->chip.mutex);
471 spin_lock_irqsave(&mac->lock, flags);
472 mac->updating_rts_rate = 0;
473 rts_rate = mac->rts_rate;
474 short_preamble = mac->short_preamble;
475 spin_unlock_irqrestore(&mac->lock, flags);
477 zd_chip_set_rts_cts_rate_locked(&mac->chip, rts_rate, short_preamble);
478 mutex_unlock(&mac->chip.mutex);
483 static void set_basic_rates_work(struct work_struct *work)
486 container_of(work, struct zd_mac, set_basic_rates_work.work);
490 mutex_lock(&mac->chip.mutex);
492 spin_lock_irqsave(&mac->lock, flags);
493 mac->updating_basic_rates = 0;
494 basic_rates = mac->basic_rates;
495 spin_unlock_irqrestore(&mac->lock, flags);
497 zd_chip_set_basic_rates_locked(&mac->chip, basic_rates);
498 mutex_unlock(&mac->chip.mutex);
503 static void bssinfo_change(struct net_device *netdev, u32 changes)
505 struct zd_mac *mac = zd_netdev_mac(netdev);
506 struct ieee80211softmac_device *softmac = ieee80211_priv(netdev);
507 struct ieee80211softmac_bss_info *bssinfo = &softmac->bssinfo;
508 int need_set_rts_cts = 0;
509 int need_set_rates = 0;
513 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
515 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_SHORT_PREAMBLE) {
516 spin_lock_irqsave(&mac->lock, flags);
517 mac->short_preamble = bssinfo->short_preamble;
518 spin_unlock_irqrestore(&mac->lock, flags);
519 need_set_rts_cts = 1;
522 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_RATES) {
523 /* Set RTS rate to highest available basic rate */
524 u8 hi_rate = ieee80211softmac_highest_supported_rate(softmac,
525 &bssinfo->supported_rates, 1);
526 hi_rate = rate_to_zd_rate(hi_rate);
528 spin_lock_irqsave(&mac->lock, flags);
529 if (hi_rate != mac->rts_rate) {
530 mac->rts_rate = hi_rate;
531 need_set_rts_cts = 1;
533 spin_unlock_irqrestore(&mac->lock, flags);
535 /* Set basic rates */
537 if (bssinfo->supported_rates.count == 0) {
538 /* Allow the device to be flexible */
539 basic_rates = CR_RATES_80211B | CR_RATES_80211G;
544 for (i = 0; i < bssinfo->supported_rates.count; i++) {
545 u16 rate = bssinfo->supported_rates.rates[i];
546 if ((rate & IEEE80211_BASIC_RATE_MASK) == 0)
549 rate &= ~IEEE80211_BASIC_RATE_MASK;
550 basic_rates |= rate_to_cr_rate(rate);
553 spin_lock_irqsave(&mac->lock, flags);
554 mac->basic_rates = basic_rates;
555 spin_unlock_irqrestore(&mac->lock, flags);
558 /* Schedule any changes we made above */
560 spin_lock_irqsave(&mac->lock, flags);
561 if (need_set_rts_cts && !mac->updating_rts_rate) {
562 mac->updating_rts_rate = 1;
563 netif_stop_queue(mac->netdev);
564 queue_delayed_work(zd_workqueue, &mac->set_rts_cts_work, 0);
566 if (need_set_rates && !mac->updating_basic_rates) {
567 mac->updating_basic_rates = 1;
568 netif_stop_queue(mac->netdev);
569 queue_delayed_work(zd_workqueue, &mac->set_basic_rates_work,
572 spin_unlock_irqrestore(&mac->lock, flags);
575 static void set_channel(struct net_device *netdev, u8 channel)
577 struct zd_mac *mac = zd_netdev_mac(netdev);
579 dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
581 zd_chip_set_channel(&mac->chip, channel);
584 int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
586 unsigned long lock_flags;
587 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
589 if (ieee->iw_mode == IW_MODE_INFRA)
592 spin_lock_irqsave(&mac->lock, lock_flags);
593 if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
594 spin_unlock_irqrestore(&mac->lock, lock_flags);
597 mac->requested_channel = channel;
598 spin_unlock_irqrestore(&mac->lock, lock_flags);
599 if (netif_running(mac->netdev))
600 return zd_chip_set_channel(&mac->chip, channel);
605 u8 zd_mac_get_channel(struct zd_mac *mac)
607 u8 channel = zd_chip_get_channel(&mac->chip);
609 dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
613 /* If wrong rate is given, we are falling back to the slowest rate: 1MBit/s */
614 static u8 zd_rate_typed(u8 zd_rate)
616 static const u8 typed_rates[16] = {
617 [ZD_CCK_RATE_1M] = ZD_CS_CCK|ZD_CCK_RATE_1M,
618 [ZD_CCK_RATE_2M] = ZD_CS_CCK|ZD_CCK_RATE_2M,
619 [ZD_CCK_RATE_5_5M] = ZD_CS_CCK|ZD_CCK_RATE_5_5M,
620 [ZD_CCK_RATE_11M] = ZD_CS_CCK|ZD_CCK_RATE_11M,
621 [ZD_OFDM_RATE_6M] = ZD_CS_OFDM|ZD_OFDM_RATE_6M,
622 [ZD_OFDM_RATE_9M] = ZD_CS_OFDM|ZD_OFDM_RATE_9M,
623 [ZD_OFDM_RATE_12M] = ZD_CS_OFDM|ZD_OFDM_RATE_12M,
624 [ZD_OFDM_RATE_18M] = ZD_CS_OFDM|ZD_OFDM_RATE_18M,
625 [ZD_OFDM_RATE_24M] = ZD_CS_OFDM|ZD_OFDM_RATE_24M,
626 [ZD_OFDM_RATE_36M] = ZD_CS_OFDM|ZD_OFDM_RATE_36M,
627 [ZD_OFDM_RATE_48M] = ZD_CS_OFDM|ZD_OFDM_RATE_48M,
628 [ZD_OFDM_RATE_54M] = ZD_CS_OFDM|ZD_OFDM_RATE_54M,
631 ZD_ASSERT(ZD_CS_RATE_MASK == 0x0f);
632 return typed_rates[zd_rate & ZD_CS_RATE_MASK];
635 int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
637 struct ieee80211_device *ieee;
643 mac->netdev->type = ARPHRD_ETHER;
645 case IW_MODE_MONITOR:
646 mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
649 dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
653 ieee = zd_mac_to_ieee80211(mac);
654 ZD_ASSERT(!irqs_disabled());
655 spin_lock_irq(&ieee->lock);
656 ieee->iw_mode = mode;
657 spin_unlock_irq(&ieee->lock);
659 if (netif_running(mac->netdev)) {
660 int r = set_rx_filter(mac);
663 return set_sniffer(mac);
669 int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
672 struct ieee80211_device *ieee;
674 ieee = zd_mac_to_ieee80211(mac);
675 spin_lock_irqsave(&ieee->lock, flags);
676 *mode = ieee->iw_mode;
677 spin_unlock_irqrestore(&ieee->lock, flags);
681 int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
684 const struct channel_range *channel_range;
687 memset(range, 0, sizeof(*range));
689 /* FIXME: Not so important and depends on the mode. For 802.11g
690 * usually this value is used. It seems to be that Bit/s number is
693 range->throughput = 27 * 1000 * 1000;
695 range->max_qual.qual = 100;
696 range->max_qual.level = 100;
698 /* FIXME: Needs still to be tuned. */
699 range->avg_qual.qual = 71;
700 range->avg_qual.level = 80;
702 /* FIXME: depends on standard? */
703 range->min_rts = 256;
704 range->max_rts = 2346;
706 range->min_frag = MIN_FRAG_THRESHOLD;
707 range->max_frag = MAX_FRAG_THRESHOLD;
709 range->max_encoding_tokens = WEP_KEYS;
710 range->num_encoding_sizes = 2;
711 range->encoding_size[0] = 5;
712 range->encoding_size[1] = WEP_KEY_LEN;
714 range->we_version_compiled = WIRELESS_EXT;
715 range->we_version_source = 20;
717 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
718 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
720 ZD_ASSERT(!irqs_disabled());
721 spin_lock_irq(&mac->lock);
722 regdomain = mac->regdomain;
723 spin_unlock_irq(&mac->lock);
724 channel_range = zd_channel_range(regdomain);
726 range->num_channels = channel_range->end - channel_range->start;
727 range->old_num_channels = range->num_channels;
728 range->num_frequency = range->num_channels;
729 range->old_num_frequency = range->num_frequency;
731 for (i = 0; i < range->num_frequency; i++) {
732 struct iw_freq *freq = &range->freq[i];
733 freq->i = channel_range->start + i;
734 zd_channel_to_freq(freq, freq->i);
740 static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
742 static const u8 rate_divisor[] = {
743 [ZD_CCK_RATE_1M] = 1,
744 [ZD_CCK_RATE_2M] = 2,
745 [ZD_CCK_RATE_5_5M] = 11, /* bits must be doubled */
746 [ZD_CCK_RATE_11M] = 11,
747 [ZD_OFDM_RATE_6M] = 6,
748 [ZD_OFDM_RATE_9M] = 9,
749 [ZD_OFDM_RATE_12M] = 12,
750 [ZD_OFDM_RATE_18M] = 18,
751 [ZD_OFDM_RATE_24M] = 24,
752 [ZD_OFDM_RATE_36M] = 36,
753 [ZD_OFDM_RATE_48M] = 48,
754 [ZD_OFDM_RATE_54M] = 54,
757 u32 bits = (u32)tx_length * 8;
760 divisor = rate_divisor[zd_rate];
765 case ZD_CCK_RATE_5_5M:
766 bits = (2*bits) + 10; /* round up to the next integer */
768 case ZD_CCK_RATE_11M:
771 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
772 if (0 < t && t <= 3) {
773 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
776 bits += 10; /* round up to the next integer */
784 R2M_SHORT_PREAMBLE = 0x01,
788 static u8 zd_rate_to_modulation(u8 zd_rate, int flags)
792 modulation = zd_rate_typed(zd_rate);
793 if (flags & R2M_SHORT_PREAMBLE) {
794 switch (ZD_CS_RATE(modulation)) {
796 case ZD_CCK_RATE_5_5M:
797 case ZD_CCK_RATE_11M:
798 modulation |= ZD_CS_CCK_PREA_SHORT;
802 if (flags & R2M_11A) {
803 if (ZD_CS_TYPE(modulation) == ZD_CS_OFDM)
804 modulation |= ZD_CS_OFDM_MODE_11A;
809 static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
810 struct ieee80211_hdr_4addr *hdr)
812 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
813 u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
815 int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
816 int is_multicast = is_multicast_ether_addr(hdr->addr1);
817 int short_preamble = ieee80211softmac_short_preamble_ok(softmac,
818 is_multicast, is_mgt);
821 /* FIXME: 802.11a? */
822 rate = ieee80211softmac_suggest_txrate(softmac, is_multicast, is_mgt);
825 flags |= R2M_SHORT_PREAMBLE;
827 zd_rate = rate_to_zd_rate(rate);
828 cs->modulation = zd_rate_to_modulation(zd_rate, flags);
831 static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
832 struct ieee80211_hdr_4addr *header)
834 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
835 unsigned int tx_length = le16_to_cpu(cs->tx_length);
836 u16 fctl = le16_to_cpu(header->frame_ctl);
837 u16 ftype = WLAN_FC_GET_TYPE(fctl);
838 u16 stype = WLAN_FC_GET_STYPE(fctl);
842 * - if backoff needed, enable bit 0
843 * - if burst (backoff not needed) disable bit 0
849 if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
850 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
853 if (is_multicast_ether_addr(header->addr1))
854 cs->control |= ZD_CS_MULTICAST;
857 if (ftype == IEEE80211_FTYPE_CTL && stype == IEEE80211_STYPE_PSPOLL)
858 cs->control |= ZD_CS_PS_POLL_FRAME;
860 /* Unicast data frames over the threshold should have RTS */
861 if (!is_multicast_ether_addr(header->addr1) &&
862 ftype != IEEE80211_FTYPE_MGMT &&
863 tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
864 cs->control |= ZD_CS_RTS;
866 /* Use CTS-to-self protection if required */
867 if (ZD_CS_TYPE(cs->modulation) == ZD_CS_OFDM &&
868 ieee80211softmac_protection_needed(softmac)) {
869 /* FIXME: avoid sending RTS *and* self-CTS, is that correct? */
870 cs->control &= ~ZD_CS_RTS;
871 cs->control |= ZD_CS_SELF_CTS;
874 /* FIXME: Management frame? */
877 static int fill_ctrlset(struct zd_mac *mac,
878 struct ieee80211_txb *txb,
882 struct sk_buff *skb = txb->fragments[frag_num];
883 struct ieee80211_hdr_4addr *hdr =
884 (struct ieee80211_hdr_4addr *) skb->data;
885 unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
886 unsigned int next_frag_len;
887 unsigned int packet_length;
888 struct zd_ctrlset *cs = (struct zd_ctrlset *)
889 skb_push(skb, sizeof(struct zd_ctrlset));
891 if (frag_num+1 < txb->nr_frags) {
892 next_frag_len = txb->fragments[frag_num+1]->len +
897 ZD_ASSERT(frag_len <= 0xffff);
898 ZD_ASSERT(next_frag_len <= 0xffff);
900 cs_set_modulation(mac, cs, hdr);
902 cs->tx_length = cpu_to_le16(frag_len);
904 cs_set_control(mac, cs, hdr);
906 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
907 ZD_ASSERT(packet_length <= 0xffff);
908 /* ZD1211B: Computing the length difference this way, gives us
909 * flexibility to compute the packet length.
911 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
912 packet_length - frag_len : packet_length);
916 * - transmit frame length in microseconds
917 * - seems to be derived from frame length
918 * - see Cal_Us_Service() in zdinlinef.h
919 * - if macp->bTxBurstEnable is enabled, then multiply by 4
920 * - bTxBurstEnable is never set in the vendor driver
923 * - "for PLCP configuration"
924 * - always 0 except in some situations at 802.11b 11M
925 * - see line 53 of zdinlinef.h
928 r = zd_calc_tx_length_us(&cs->service, ZD_CS_RATE(cs->modulation),
929 le16_to_cpu(cs->tx_length));
932 cs->current_length = cpu_to_le16(r);
934 if (next_frag_len == 0) {
935 cs->next_frame_length = 0;
937 r = zd_calc_tx_length_us(NULL, ZD_CS_RATE(cs->modulation),
941 cs->next_frame_length = cpu_to_le16(r);
947 static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
950 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
952 for (i = 0; i < txb->nr_frags; i++) {
953 struct sk_buff *skb = txb->fragments[i];
955 r = fill_ctrlset(mac, txb, i);
957 ieee->stats.tx_dropped++;
960 r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
962 ieee->stats.tx_dropped++;
967 /* FIXME: shouldn't this be handled by the upper layers? */
968 mac->netdev->trans_start = jiffies;
970 ieee80211_txb_free(txb);
975 struct ieee80211_radiotap_header rt_hdr;
980 } __attribute__((packed));
982 static void fill_rt_header(void *buffer, struct zd_mac *mac,
983 const struct ieee80211_rx_stats *stats,
984 const struct rx_status *status)
986 struct zd_rt_hdr *hdr = buffer;
988 hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
989 hdr->rt_hdr.it_pad = 0;
990 hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
991 hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
992 (1 << IEEE80211_RADIOTAP_CHANNEL) |
993 (1 << IEEE80211_RADIOTAP_RATE));
996 if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
997 hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
999 hdr->rt_rate = stats->rate / 5;
1001 /* FIXME: 802.11a */
1002 hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
1003 _zd_chip_get_channel(&mac->chip)));
1004 hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
1005 ((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
1006 ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
1009 /* Returns 1 if the data packet is for us and 0 otherwise. */
1010 static int is_data_packet_for_us(struct ieee80211_device *ieee,
1011 struct ieee80211_hdr_4addr *hdr)
1013 struct net_device *netdev = ieee->dev;
1014 u16 fc = le16_to_cpu(hdr->frame_ctl);
1016 ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
1018 switch (ieee->iw_mode) {
1020 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
1021 compare_ether_addr(hdr->addr3, ieee->bssid) != 0)
1026 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
1027 IEEE80211_FCTL_FROMDS ||
1028 compare_ether_addr(hdr->addr2, ieee->bssid) != 0)
1032 ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
1036 return compare_ether_addr(hdr->addr1, netdev->dev_addr) == 0 ||
1037 (is_multicast_ether_addr(hdr->addr1) &&
1038 compare_ether_addr(hdr->addr3, netdev->dev_addr) != 0) ||
1039 (netdev->flags & IFF_PROMISC);
1042 /* Filters received packets. The function returns 1 if the packet should be
1043 * forwarded to ieee80211_rx(). If the packet should be ignored the function
1044 * returns 0. If an invalid packet is found the function returns -EINVAL.
1046 * The function calls ieee80211_rx_mgt() directly.
1048 * It has been based on ieee80211_rx_any.
1050 static int filter_rx(struct ieee80211_device *ieee,
1051 const u8 *buffer, unsigned int length,
1052 struct ieee80211_rx_stats *stats)
1054 struct ieee80211_hdr_4addr *hdr;
1057 if (ieee->iw_mode == IW_MODE_MONITOR)
1060 hdr = (struct ieee80211_hdr_4addr *)buffer;
1061 fc = le16_to_cpu(hdr->frame_ctl);
1062 if ((fc & IEEE80211_FCTL_VERS) != 0)
1065 switch (WLAN_FC_GET_TYPE(fc)) {
1066 case IEEE80211_FTYPE_MGMT:
1067 if (length < sizeof(struct ieee80211_hdr_3addr))
1069 ieee80211_rx_mgt(ieee, hdr, stats);
1071 case IEEE80211_FTYPE_CTL:
1073 case IEEE80211_FTYPE_DATA:
1074 /* Ignore invalid short buffers */
1075 if (length < sizeof(struct ieee80211_hdr_3addr))
1077 return is_data_packet_for_us(ieee, hdr);
1083 static void update_qual_rssi(struct zd_mac *mac,
1084 const u8 *buffer, unsigned int length,
1085 u8 qual_percent, u8 rssi_percent)
1087 unsigned long flags;
1088 struct ieee80211_hdr_3addr *hdr;
1091 hdr = (struct ieee80211_hdr_3addr *)buffer;
1092 if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
1094 if (compare_ether_addr(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid) != 0)
1097 spin_lock_irqsave(&mac->lock, flags);
1098 i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
1099 mac->qual_buffer[i] = qual_percent;
1100 mac->rssi_buffer[i] = rssi_percent;
1102 spin_unlock_irqrestore(&mac->lock, flags);
1105 static int fill_rx_stats(struct ieee80211_rx_stats *stats,
1106 const struct rx_status **pstatus,
1108 const u8 *buffer, unsigned int length)
1110 const struct rx_status *status;
1112 *pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
1113 if (status->frame_status & ZD_RX_ERROR) {
1114 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1115 ieee->stats.rx_errors++;
1116 if (status->frame_status & ZD_RX_TIMEOUT_ERROR)
1117 ieee->stats.rx_missed_errors++;
1118 else if (status->frame_status & ZD_RX_FIFO_OVERRUN_ERROR)
1119 ieee->stats.rx_fifo_errors++;
1120 else if (status->frame_status & ZD_RX_DECRYPTION_ERROR)
1121 ieee->ieee_stats.rx_discards_undecryptable++;
1122 else if (status->frame_status & ZD_RX_CRC32_ERROR) {
1123 ieee->stats.rx_crc_errors++;
1124 ieee->ieee_stats.rx_fcs_errors++;
1126 else if (status->frame_status & ZD_RX_CRC16_ERROR)
1127 ieee->stats.rx_crc_errors++;
1131 memset(stats, 0, sizeof(struct ieee80211_rx_stats));
1132 stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
1133 + sizeof(struct rx_status));
1134 /* FIXME: 802.11a */
1135 stats->freq = IEEE80211_24GHZ_BAND;
1136 stats->received_channel = _zd_chip_get_channel(&mac->chip);
1137 stats->rssi = zd_rx_strength_percent(status->signal_strength);
1138 stats->signal = zd_rx_qual_percent(buffer,
1139 length - sizeof(struct rx_status),
1141 stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
1142 stats->rate = zd_rx_rate(buffer, status);
1144 stats->mask |= IEEE80211_STATMASK_RATE;
1149 static void zd_mac_rx(struct zd_mac *mac, struct sk_buff *skb)
1152 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1153 struct ieee80211_rx_stats stats;
1154 const struct rx_status *status;
1156 if (skb->len < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
1157 IEEE80211_FCS_LEN + sizeof(struct rx_status))
1159 ieee->stats.rx_errors++;
1160 ieee->stats.rx_length_errors++;
1164 r = fill_rx_stats(&stats, &status, mac, skb->data, skb->len);
1166 /* Only packets with rx errors are included here.
1167 * The error stats have already been set in fill_rx_stats.
1172 __skb_pull(skb, ZD_PLCP_HEADER_SIZE);
1173 __skb_trim(skb, skb->len -
1174 (IEEE80211_FCS_LEN + sizeof(struct rx_status)));
1176 update_qual_rssi(mac, skb->data, skb->len, stats.signal,
1177 status->signal_strength);
1179 r = filter_rx(ieee, skb->data, skb->len, &stats);
1182 ieee->stats.rx_errors++;
1183 dev_dbg_f(zd_mac_dev(mac), "Error in packet.\n");
1188 if (ieee->iw_mode == IW_MODE_MONITOR)
1189 fill_rt_header(skb_push(skb, sizeof(struct zd_rt_hdr)), mac,
1192 r = ieee80211_rx(ieee, skb, &stats);
1196 /* We are always in a soft irq. */
1200 static void do_rx(unsigned long mac_ptr)
1202 struct zd_mac *mac = (struct zd_mac *)mac_ptr;
1203 struct sk_buff *skb;
1205 while ((skb = skb_dequeue(&mac->rx_queue)) != NULL)
1206 zd_mac_rx(mac, skb);
1209 int zd_mac_rx_irq(struct zd_mac *mac, const u8 *buffer, unsigned int length)
1211 struct sk_buff *skb;
1213 skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
1215 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1216 dev_warn(zd_mac_dev(mac), "Could not allocate skb.\n");
1217 ieee->stats.rx_dropped++;
1220 skb_reserve(skb, sizeof(struct zd_rt_hdr));
1221 memcpy(__skb_put(skb, length), buffer, length);
1222 skb_queue_tail(&mac->rx_queue, skb);
1223 tasklet_schedule(&mac->rx_tasklet);
1227 static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
1230 return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
1233 static void set_security(struct net_device *netdev,
1234 struct ieee80211_security *sec)
1236 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
1237 struct ieee80211_security *secinfo = &ieee->sec;
1240 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
1242 for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
1243 if (sec->flags & (1<<keyidx)) {
1244 secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
1245 secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
1246 memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
1250 if (sec->flags & SEC_ACTIVE_KEY) {
1251 secinfo->active_key = sec->active_key;
1252 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1253 " .active_key = %d\n", sec->active_key);
1255 if (sec->flags & SEC_UNICAST_GROUP) {
1256 secinfo->unicast_uses_group = sec->unicast_uses_group;
1257 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1258 " .unicast_uses_group = %d\n",
1259 sec->unicast_uses_group);
1261 if (sec->flags & SEC_LEVEL) {
1262 secinfo->level = sec->level;
1263 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1264 " .level = %d\n", sec->level);
1266 if (sec->flags & SEC_ENABLED) {
1267 secinfo->enabled = sec->enabled;
1268 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1269 " .enabled = %d\n", sec->enabled);
1271 if (sec->flags & SEC_ENCRYPT) {
1272 secinfo->encrypt = sec->encrypt;
1273 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1274 " .encrypt = %d\n", sec->encrypt);
1276 if (sec->flags & SEC_AUTH_MODE) {
1277 secinfo->auth_mode = sec->auth_mode;
1278 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1279 " .auth_mode = %d\n", sec->auth_mode);
1283 static void ieee_init(struct ieee80211_device *ieee)
1285 ieee->mode = IEEE_B | IEEE_G;
1286 ieee->freq_band = IEEE80211_24GHZ_BAND;
1287 ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
1288 ieee->tx_headroom = sizeof(struct zd_ctrlset);
1289 ieee->set_security = set_security;
1290 ieee->hard_start_xmit = netdev_tx;
1292 /* Software encryption/decryption for now */
1293 ieee->host_build_iv = 0;
1294 ieee->host_encrypt = 1;
1295 ieee->host_decrypt = 1;
1297 /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
1298 * correctly support AUTO */
1299 ieee->iw_mode = IW_MODE_INFRA;
1302 static void softmac_init(struct ieee80211softmac_device *sm)
1304 sm->set_channel = set_channel;
1305 sm->bssinfo_change = bssinfo_change;
1308 struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
1310 struct zd_mac *mac = zd_netdev_mac(ndev);
1311 struct iw_statistics *iw_stats = &mac->iw_stats;
1312 unsigned int i, count, qual_total, rssi_total;
1314 memset(iw_stats, 0, sizeof(struct iw_statistics));
1315 /* We are not setting the status, because ieee->state is not updated
1316 * at all and this driver doesn't track authentication state.
1318 spin_lock_irq(&mac->lock);
1319 count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
1320 mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
1321 qual_total = rssi_total = 0;
1322 for (i = 0; i < count; i++) {
1323 qual_total += mac->qual_buffer[i];
1324 rssi_total += mac->rssi_buffer[i];
1326 spin_unlock_irq(&mac->lock);
1327 iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
1329 iw_stats->qual.qual = qual_total / count;
1330 iw_stats->qual.level = rssi_total / count;
1331 iw_stats->qual.updated |=
1332 IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
1334 iw_stats->qual.updated |=
1335 IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
1337 /* TODO: update counter */
1341 #define LINK_LED_WORK_DELAY HZ
1343 static void link_led_handler(struct work_struct *work)
1345 struct zd_mac *mac =
1346 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
1347 struct zd_chip *chip = &mac->chip;
1348 struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
1352 spin_lock_irq(&mac->lock);
1353 is_associated = sm->associnfo.associated != 0;
1354 spin_unlock_irq(&mac->lock);
1356 r = zd_chip_control_leds(chip,
1357 is_associated ? LED_ASSOCIATED : LED_SCANNING);
1359 dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1361 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1362 LINK_LED_WORK_DELAY);
1365 static void housekeeping_init(struct zd_mac *mac)
1367 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
1370 static void housekeeping_enable(struct zd_mac *mac)
1372 dev_dbg_f(zd_mac_dev(mac), "\n");
1373 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1377 static void housekeeping_disable(struct zd_mac *mac)
1379 dev_dbg_f(zd_mac_dev(mac), "\n");
1380 cancel_rearming_delayed_workqueue(zd_workqueue,
1381 &mac->housekeeping.link_led_work);
1382 zd_chip_control_leds(&mac->chip, LED_OFF);