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 int zd_mac_init(struct zd_mac *mac,
45 struct net_device *netdev,
46 struct usb_interface *intf)
48 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
50 memset(mac, 0, sizeof(*mac));
51 spin_lock_init(&mac->lock);
53 INIT_DELAYED_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
54 INIT_DELAYED_WORK(&mac->set_basic_rates_work, set_basic_rates_work);
57 softmac_init(ieee80211_priv(netdev));
58 zd_chip_init(&mac->chip, netdev, intf);
59 housekeeping_init(mac);
60 INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
64 static int reset_channel(struct zd_mac *mac)
68 const struct channel_range *range;
70 spin_lock_irqsave(&mac->lock, flags);
71 range = zd_channel_range(mac->regdomain);
76 mac->requested_channel = range->start;
79 spin_unlock_irqrestore(&mac->lock, flags);
83 int zd_mac_init_hw(struct zd_mac *mac, u8 device_type)
86 struct zd_chip *chip = &mac->chip;
90 r = zd_chip_enable_int(chip);
93 r = zd_chip_init_hw(chip, device_type);
97 zd_get_e2p_mac_addr(chip, addr);
98 r = zd_write_mac_addr(chip, addr);
101 ZD_ASSERT(!irqs_disabled());
102 spin_lock_irq(&mac->lock);
103 memcpy(mac->netdev->dev_addr, addr, ETH_ALEN);
104 spin_unlock_irq(&mac->lock);
106 r = zd_read_regdomain(chip, &default_regdomain);
109 if (!zd_regdomain_supported(default_regdomain)) {
110 dev_dbg_f(zd_mac_dev(mac),
111 "Regulatory Domain %#04x is not supported.\n",
116 spin_lock_irq(&mac->lock);
117 mac->regdomain = mac->default_regdomain = default_regdomain;
118 spin_unlock_irq(&mac->lock);
119 r = reset_channel(mac);
123 /* We must inform the device that we are doing encryption/decryption in
124 * software at the moment. */
125 r = zd_set_encryption_type(chip, ENC_SNIFFER);
129 r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
135 zd_chip_disable_int(chip);
140 void zd_mac_clear(struct zd_mac *mac)
142 flush_workqueue(zd_workqueue);
143 zd_chip_clear(&mac->chip);
144 ZD_ASSERT(!spin_is_locked(&mac->lock));
145 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
148 static int reset_mode(struct zd_mac *mac)
150 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
151 struct zd_ioreq32 ioreqs[3] = {
152 { CR_RX_FILTER, STA_RX_FILTER },
153 { CR_SNIFFER_ON, 0U },
156 if (ieee->iw_mode == IW_MODE_MONITOR) {
157 ioreqs[0].value = 0xffffffff;
158 ioreqs[1].value = 0x1;
159 ioreqs[2].value = ENC_SNIFFER;
162 return zd_iowrite32a(&mac->chip, ioreqs, 3);
165 int zd_mac_open(struct net_device *netdev)
167 struct zd_mac *mac = zd_netdev_mac(netdev);
168 struct zd_chip *chip = &mac->chip;
171 r = zd_chip_enable_int(chip);
175 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
181 r = zd_chip_switch_radio_on(chip);
184 r = zd_chip_set_channel(chip, mac->requested_channel);
187 r = zd_chip_enable_rx(chip);
190 r = zd_chip_enable_hwint(chip);
194 housekeeping_enable(mac);
195 ieee80211softmac_start(netdev);
198 zd_chip_disable_rx(chip);
200 zd_chip_switch_radio_off(chip);
202 zd_chip_disable_int(chip);
207 int zd_mac_stop(struct net_device *netdev)
209 struct zd_mac *mac = zd_netdev_mac(netdev);
210 struct zd_chip *chip = &mac->chip;
212 netif_stop_queue(netdev);
215 * The order here deliberately is a little different from the open()
216 * method, since we need to make sure there is no opportunity for RX
217 * frames to be processed by softmac after we have stopped it.
220 zd_chip_disable_rx(chip);
221 housekeeping_disable(mac);
222 ieee80211softmac_stop(netdev);
224 /* Ensure no work items are running or queued from this point */
225 cancel_delayed_work(&mac->set_rts_cts_work);
226 cancel_delayed_work(&mac->set_basic_rates_work);
227 flush_workqueue(zd_workqueue);
228 mac->updating_rts_rate = 0;
229 mac->updating_basic_rates = 0;
231 zd_chip_disable_hwint(chip);
232 zd_chip_switch_radio_off(chip);
233 zd_chip_disable_int(chip);
238 int zd_mac_set_mac_address(struct net_device *netdev, void *p)
242 struct sockaddr *addr = p;
243 struct zd_mac *mac = zd_netdev_mac(netdev);
244 struct zd_chip *chip = &mac->chip;
246 if (!is_valid_ether_addr(addr->sa_data))
247 return -EADDRNOTAVAIL;
249 dev_dbg_f(zd_mac_dev(mac),
250 "Setting MAC to " MAC_FMT "\n", MAC_ARG(addr->sa_data));
252 r = zd_write_mac_addr(chip, addr->sa_data);
256 spin_lock_irqsave(&mac->lock, flags);
257 memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
258 spin_unlock_irqrestore(&mac->lock, flags);
263 static void set_multicast_hash_handler(struct work_struct *work)
265 struct zd_mac *mac = container_of(work, struct zd_mac,
266 set_multicast_hash_work);
267 struct zd_mc_hash hash;
269 spin_lock_irq(&mac->lock);
270 hash = mac->multicast_hash;
271 spin_unlock_irq(&mac->lock);
273 zd_chip_set_multicast_hash(&mac->chip, &hash);
276 void zd_mac_set_multicast_list(struct net_device *dev)
278 struct zd_mc_hash hash;
279 struct zd_mac *mac = zd_netdev_mac(dev);
280 struct dev_mc_list *mc;
283 if (dev->flags & (IFF_PROMISC|IFF_ALLMULTI)) {
284 zd_mc_add_all(&hash);
287 for (mc = dev->mc_list; mc; mc = mc->next) {
288 dev_dbg_f(zd_mac_dev(mac), "mc addr " MAC_FMT "\n",
289 MAC_ARG(mc->dmi_addr));
290 zd_mc_add_addr(&hash, mc->dmi_addr);
294 spin_lock_irqsave(&mac->lock, flags);
295 mac->multicast_hash = hash;
296 spin_unlock_irqrestore(&mac->lock, flags);
297 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
300 int zd_mac_set_regdomain(struct zd_mac *mac, u8 regdomain)
305 ZD_ASSERT(!irqs_disabled());
306 spin_lock_irq(&mac->lock);
307 if (regdomain == 0) {
308 regdomain = mac->default_regdomain;
310 if (!zd_regdomain_supported(regdomain)) {
311 spin_unlock_irq(&mac->lock);
314 mac->regdomain = regdomain;
315 channel = mac->requested_channel;
316 spin_unlock_irq(&mac->lock);
318 r = zd_geo_init(zd_mac_to_ieee80211(mac), regdomain);
321 if (!zd_regdomain_supports_channel(regdomain, channel)) {
322 r = reset_channel(mac);
330 u8 zd_mac_get_regdomain(struct zd_mac *mac)
335 spin_lock_irqsave(&mac->lock, flags);
336 regdomain = mac->regdomain;
337 spin_unlock_irqrestore(&mac->lock, flags);
341 /* Fallback to lowest rate, if rate is unknown. */
342 static u8 rate_to_zd_rate(u8 rate)
345 case IEEE80211_CCK_RATE_2MB:
346 return ZD_CCK_RATE_2M;
347 case IEEE80211_CCK_RATE_5MB:
348 return ZD_CCK_RATE_5_5M;
349 case IEEE80211_CCK_RATE_11MB:
350 return ZD_CCK_RATE_11M;
351 case IEEE80211_OFDM_RATE_6MB:
352 return ZD_OFDM_RATE_6M;
353 case IEEE80211_OFDM_RATE_9MB:
354 return ZD_OFDM_RATE_9M;
355 case IEEE80211_OFDM_RATE_12MB:
356 return ZD_OFDM_RATE_12M;
357 case IEEE80211_OFDM_RATE_18MB:
358 return ZD_OFDM_RATE_18M;
359 case IEEE80211_OFDM_RATE_24MB:
360 return ZD_OFDM_RATE_24M;
361 case IEEE80211_OFDM_RATE_36MB:
362 return ZD_OFDM_RATE_36M;
363 case IEEE80211_OFDM_RATE_48MB:
364 return ZD_OFDM_RATE_48M;
365 case IEEE80211_OFDM_RATE_54MB:
366 return ZD_OFDM_RATE_54M;
368 return ZD_CCK_RATE_1M;
371 static u16 rate_to_cr_rate(u8 rate)
374 case IEEE80211_CCK_RATE_2MB:
376 case IEEE80211_CCK_RATE_5MB:
378 case IEEE80211_CCK_RATE_11MB:
380 case IEEE80211_OFDM_RATE_6MB:
382 case IEEE80211_OFDM_RATE_9MB:
384 case IEEE80211_OFDM_RATE_12MB:
386 case IEEE80211_OFDM_RATE_18MB:
388 case IEEE80211_OFDM_RATE_24MB:
390 case IEEE80211_OFDM_RATE_36MB:
392 case IEEE80211_OFDM_RATE_48MB:
394 case IEEE80211_OFDM_RATE_54MB:
400 static void try_enable_tx(struct zd_mac *mac)
404 spin_lock_irqsave(&mac->lock, flags);
405 if (mac->updating_rts_rate == 0 && mac->updating_basic_rates == 0)
406 netif_wake_queue(mac->netdev);
407 spin_unlock_irqrestore(&mac->lock, flags);
410 static void set_rts_cts_work(struct work_struct *work)
413 container_of(work, struct zd_mac, set_rts_cts_work.work);
416 unsigned int short_preamble;
418 mutex_lock(&mac->chip.mutex);
420 spin_lock_irqsave(&mac->lock, flags);
421 mac->updating_rts_rate = 0;
422 rts_rate = mac->rts_rate;
423 short_preamble = mac->short_preamble;
424 spin_unlock_irqrestore(&mac->lock, flags);
426 zd_chip_set_rts_cts_rate_locked(&mac->chip, rts_rate, short_preamble);
427 mutex_unlock(&mac->chip.mutex);
432 static void set_basic_rates_work(struct work_struct *work)
435 container_of(work, struct zd_mac, set_basic_rates_work.work);
439 mutex_lock(&mac->chip.mutex);
441 spin_lock_irqsave(&mac->lock, flags);
442 mac->updating_basic_rates = 0;
443 basic_rates = mac->basic_rates;
444 spin_unlock_irqrestore(&mac->lock, flags);
446 zd_chip_set_basic_rates_locked(&mac->chip, basic_rates);
447 mutex_unlock(&mac->chip.mutex);
452 static void bssinfo_change(struct net_device *netdev, u32 changes)
454 struct zd_mac *mac = zd_netdev_mac(netdev);
455 struct ieee80211softmac_device *softmac = ieee80211_priv(netdev);
456 struct ieee80211softmac_bss_info *bssinfo = &softmac->bssinfo;
457 int need_set_rts_cts = 0;
458 int need_set_rates = 0;
462 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
464 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_SHORT_PREAMBLE) {
465 spin_lock_irqsave(&mac->lock, flags);
466 mac->short_preamble = bssinfo->short_preamble;
467 spin_unlock_irqrestore(&mac->lock, flags);
468 need_set_rts_cts = 1;
471 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_RATES) {
472 /* Set RTS rate to highest available basic rate */
473 u8 rate = ieee80211softmac_highest_supported_rate(softmac,
474 &bssinfo->supported_rates, 1);
475 rate = rate_to_zd_rate(rate);
477 spin_lock_irqsave(&mac->lock, flags);
478 if (rate != mac->rts_rate) {
479 mac->rts_rate = rate;
480 need_set_rts_cts = 1;
482 spin_unlock_irqrestore(&mac->lock, flags);
484 /* Set basic rates */
486 if (bssinfo->supported_rates.count == 0) {
487 /* Allow the device to be flexible */
488 basic_rates = CR_RATES_80211B | CR_RATES_80211G;
493 for (i = 0; i < bssinfo->supported_rates.count; i++) {
494 u16 rate = bssinfo->supported_rates.rates[i];
495 if ((rate & IEEE80211_BASIC_RATE_MASK) == 0)
498 rate &= ~IEEE80211_BASIC_RATE_MASK;
499 basic_rates |= rate_to_cr_rate(rate);
502 spin_lock_irqsave(&mac->lock, flags);
503 mac->basic_rates = basic_rates;
504 spin_unlock_irqrestore(&mac->lock, flags);
507 /* Schedule any changes we made above */
509 spin_lock_irqsave(&mac->lock, flags);
510 if (need_set_rts_cts && !mac->updating_rts_rate) {
511 mac->updating_rts_rate = 1;
512 netif_stop_queue(mac->netdev);
513 queue_delayed_work(zd_workqueue, &mac->set_rts_cts_work, 0);
515 if (need_set_rates && !mac->updating_basic_rates) {
516 mac->updating_basic_rates = 1;
517 netif_stop_queue(mac->netdev);
518 queue_delayed_work(zd_workqueue, &mac->set_basic_rates_work,
521 spin_unlock_irqrestore(&mac->lock, flags);
524 static void set_channel(struct net_device *netdev, u8 channel)
526 struct zd_mac *mac = zd_netdev_mac(netdev);
528 dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
530 zd_chip_set_channel(&mac->chip, channel);
533 int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
535 unsigned long lock_flags;
536 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
538 if (ieee->iw_mode == IW_MODE_INFRA)
541 spin_lock_irqsave(&mac->lock, lock_flags);
542 if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
543 spin_unlock_irqrestore(&mac->lock, lock_flags);
546 mac->requested_channel = channel;
547 spin_unlock_irqrestore(&mac->lock, lock_flags);
548 if (netif_running(mac->netdev))
549 return zd_chip_set_channel(&mac->chip, channel);
554 u8 zd_mac_get_channel(struct zd_mac *mac)
556 u8 channel = zd_chip_get_channel(&mac->chip);
558 dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
562 /* If wrong rate is given, we are falling back to the slowest rate: 1MBit/s */
563 static u8 zd_rate_typed(u8 zd_rate)
565 static const u8 typed_rates[16] = {
566 [ZD_CCK_RATE_1M] = ZD_CS_CCK|ZD_CCK_RATE_1M,
567 [ZD_CCK_RATE_2M] = ZD_CS_CCK|ZD_CCK_RATE_2M,
568 [ZD_CCK_RATE_5_5M] = ZD_CS_CCK|ZD_CCK_RATE_5_5M,
569 [ZD_CCK_RATE_11M] = ZD_CS_CCK|ZD_CCK_RATE_11M,
570 [ZD_OFDM_RATE_6M] = ZD_CS_OFDM|ZD_OFDM_RATE_6M,
571 [ZD_OFDM_RATE_9M] = ZD_CS_OFDM|ZD_OFDM_RATE_9M,
572 [ZD_OFDM_RATE_12M] = ZD_CS_OFDM|ZD_OFDM_RATE_12M,
573 [ZD_OFDM_RATE_18M] = ZD_CS_OFDM|ZD_OFDM_RATE_18M,
574 [ZD_OFDM_RATE_24M] = ZD_CS_OFDM|ZD_OFDM_RATE_24M,
575 [ZD_OFDM_RATE_36M] = ZD_CS_OFDM|ZD_OFDM_RATE_36M,
576 [ZD_OFDM_RATE_48M] = ZD_CS_OFDM|ZD_OFDM_RATE_48M,
577 [ZD_OFDM_RATE_54M] = ZD_CS_OFDM|ZD_OFDM_RATE_54M,
580 ZD_ASSERT(ZD_CS_RATE_MASK == 0x0f);
581 return typed_rates[zd_rate & ZD_CS_RATE_MASK];
584 int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
586 struct ieee80211_device *ieee;
592 mac->netdev->type = ARPHRD_ETHER;
594 case IW_MODE_MONITOR:
595 mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
598 dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
602 ieee = zd_mac_to_ieee80211(mac);
603 ZD_ASSERT(!irqs_disabled());
604 spin_lock_irq(&ieee->lock);
605 ieee->iw_mode = mode;
606 spin_unlock_irq(&ieee->lock);
608 if (netif_running(mac->netdev))
609 return reset_mode(mac);
614 int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
617 struct ieee80211_device *ieee;
619 ieee = zd_mac_to_ieee80211(mac);
620 spin_lock_irqsave(&ieee->lock, flags);
621 *mode = ieee->iw_mode;
622 spin_unlock_irqrestore(&ieee->lock, flags);
626 int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
629 const struct channel_range *channel_range;
632 memset(range, 0, sizeof(*range));
634 /* FIXME: Not so important and depends on the mode. For 802.11g
635 * usually this value is used. It seems to be that Bit/s number is
638 range->throughput = 27 * 1000 * 1000;
640 range->max_qual.qual = 100;
641 range->max_qual.level = 100;
643 /* FIXME: Needs still to be tuned. */
644 range->avg_qual.qual = 71;
645 range->avg_qual.level = 80;
647 /* FIXME: depends on standard? */
648 range->min_rts = 256;
649 range->max_rts = 2346;
651 range->min_frag = MIN_FRAG_THRESHOLD;
652 range->max_frag = MAX_FRAG_THRESHOLD;
654 range->max_encoding_tokens = WEP_KEYS;
655 range->num_encoding_sizes = 2;
656 range->encoding_size[0] = 5;
657 range->encoding_size[1] = WEP_KEY_LEN;
659 range->we_version_compiled = WIRELESS_EXT;
660 range->we_version_source = 20;
662 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
663 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
665 ZD_ASSERT(!irqs_disabled());
666 spin_lock_irq(&mac->lock);
667 regdomain = mac->regdomain;
668 spin_unlock_irq(&mac->lock);
669 channel_range = zd_channel_range(regdomain);
671 range->num_channels = channel_range->end - channel_range->start;
672 range->old_num_channels = range->num_channels;
673 range->num_frequency = range->num_channels;
674 range->old_num_frequency = range->num_frequency;
676 for (i = 0; i < range->num_frequency; i++) {
677 struct iw_freq *freq = &range->freq[i];
678 freq->i = channel_range->start + i;
679 zd_channel_to_freq(freq, freq->i);
685 static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
687 static const u8 rate_divisor[] = {
688 [ZD_CCK_RATE_1M] = 1,
689 [ZD_CCK_RATE_2M] = 2,
690 [ZD_CCK_RATE_5_5M] = 11, /* bits must be doubled */
691 [ZD_CCK_RATE_11M] = 11,
692 [ZD_OFDM_RATE_6M] = 6,
693 [ZD_OFDM_RATE_9M] = 9,
694 [ZD_OFDM_RATE_12M] = 12,
695 [ZD_OFDM_RATE_18M] = 18,
696 [ZD_OFDM_RATE_24M] = 24,
697 [ZD_OFDM_RATE_36M] = 36,
698 [ZD_OFDM_RATE_48M] = 48,
699 [ZD_OFDM_RATE_54M] = 54,
702 u32 bits = (u32)tx_length * 8;
705 divisor = rate_divisor[zd_rate];
710 case ZD_CCK_RATE_5_5M:
711 bits = (2*bits) + 10; /* round up to the next integer */
713 case ZD_CCK_RATE_11M:
716 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
717 if (0 < t && t <= 3) {
718 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
721 bits += 10; /* round up to the next integer */
729 R2M_SHORT_PREAMBLE = 0x01,
733 static u8 zd_rate_to_modulation(u8 zd_rate, int flags)
737 modulation = zd_rate_typed(zd_rate);
738 if (flags & R2M_SHORT_PREAMBLE) {
739 switch (ZD_CS_RATE(modulation)) {
741 case ZD_CCK_RATE_5_5M:
742 case ZD_CCK_RATE_11M:
743 modulation |= ZD_CS_CCK_PREA_SHORT;
747 if (flags & R2M_11A) {
748 if (ZD_CS_TYPE(modulation) == ZD_CS_OFDM)
749 modulation |= ZD_CS_OFDM_MODE_11A;
754 static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
755 struct ieee80211_hdr_4addr *hdr)
757 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
758 u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
760 int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
761 int is_multicast = is_multicast_ether_addr(hdr->addr1);
762 int short_preamble = ieee80211softmac_short_preamble_ok(softmac,
763 is_multicast, is_mgt);
766 /* FIXME: 802.11a? */
767 rate = ieee80211softmac_suggest_txrate(softmac, is_multicast, is_mgt);
770 flags |= R2M_SHORT_PREAMBLE;
772 zd_rate = rate_to_zd_rate(rate);
773 cs->modulation = zd_rate_to_modulation(zd_rate, flags);
776 static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
777 struct ieee80211_hdr_4addr *header)
779 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
780 unsigned int tx_length = le16_to_cpu(cs->tx_length);
781 u16 fctl = le16_to_cpu(header->frame_ctl);
782 u16 ftype = WLAN_FC_GET_TYPE(fctl);
783 u16 stype = WLAN_FC_GET_STYPE(fctl);
787 * - if backoff needed, enable bit 0
788 * - if burst (backoff not needed) disable bit 0
794 if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
795 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
798 if (is_multicast_ether_addr(header->addr1))
799 cs->control |= ZD_CS_MULTICAST;
802 if (stype == IEEE80211_STYPE_PSPOLL)
803 cs->control |= ZD_CS_PS_POLL_FRAME;
805 /* Unicast data frames over the threshold should have RTS */
806 if (!is_multicast_ether_addr(header->addr1) &&
807 ftype != IEEE80211_FTYPE_MGMT &&
808 tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
809 cs->control |= ZD_CS_RTS;
811 /* Use CTS-to-self protection if required */
812 if (ZD_CS_TYPE(cs->modulation) == ZD_CS_OFDM &&
813 ieee80211softmac_protection_needed(softmac)) {
814 /* FIXME: avoid sending RTS *and* self-CTS, is that correct? */
815 cs->control &= ~ZD_CS_RTS;
816 cs->control |= ZD_CS_SELF_CTS;
819 /* FIXME: Management frame? */
822 static int fill_ctrlset(struct zd_mac *mac,
823 struct ieee80211_txb *txb,
827 struct sk_buff *skb = txb->fragments[frag_num];
828 struct ieee80211_hdr_4addr *hdr =
829 (struct ieee80211_hdr_4addr *) skb->data;
830 unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
831 unsigned int next_frag_len;
832 unsigned int packet_length;
833 struct zd_ctrlset *cs = (struct zd_ctrlset *)
834 skb_push(skb, sizeof(struct zd_ctrlset));
836 if (frag_num+1 < txb->nr_frags) {
837 next_frag_len = txb->fragments[frag_num+1]->len +
842 ZD_ASSERT(frag_len <= 0xffff);
843 ZD_ASSERT(next_frag_len <= 0xffff);
845 cs_set_modulation(mac, cs, hdr);
847 cs->tx_length = cpu_to_le16(frag_len);
849 cs_set_control(mac, cs, hdr);
851 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
852 ZD_ASSERT(packet_length <= 0xffff);
853 /* ZD1211B: Computing the length difference this way, gives us
854 * flexibility to compute the packet length.
856 cs->packet_length = cpu_to_le16(mac->chip.is_zd1211b ?
857 packet_length - frag_len : packet_length);
861 * - transmit frame length in microseconds
862 * - seems to be derived from frame length
863 * - see Cal_Us_Service() in zdinlinef.h
864 * - if macp->bTxBurstEnable is enabled, then multiply by 4
865 * - bTxBurstEnable is never set in the vendor driver
868 * - "for PLCP configuration"
869 * - always 0 except in some situations at 802.11b 11M
870 * - see line 53 of zdinlinef.h
873 r = zd_calc_tx_length_us(&cs->service, ZD_CS_RATE(cs->modulation),
874 le16_to_cpu(cs->tx_length));
877 cs->current_length = cpu_to_le16(r);
879 if (next_frag_len == 0) {
880 cs->next_frame_length = 0;
882 r = zd_calc_tx_length_us(NULL, ZD_CS_RATE(cs->modulation),
886 cs->next_frame_length = cpu_to_le16(r);
892 static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
896 for (i = 0; i < txb->nr_frags; i++) {
897 struct sk_buff *skb = txb->fragments[i];
899 r = fill_ctrlset(mac, txb, i);
902 r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
907 /* FIXME: shouldn't this be handled by the upper layers? */
908 mac->netdev->trans_start = jiffies;
910 ieee80211_txb_free(txb);
915 struct ieee80211_radiotap_header rt_hdr;
920 } __attribute__((packed));
922 static void fill_rt_header(void *buffer, struct zd_mac *mac,
923 const struct ieee80211_rx_stats *stats,
924 const struct rx_status *status)
926 struct zd_rt_hdr *hdr = buffer;
928 hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
929 hdr->rt_hdr.it_pad = 0;
930 hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
931 hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
932 (1 << IEEE80211_RADIOTAP_CHANNEL) |
933 (1 << IEEE80211_RADIOTAP_RATE));
936 if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
937 hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
939 hdr->rt_rate = stats->rate / 5;
942 hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
943 _zd_chip_get_channel(&mac->chip)));
944 hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
945 ((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
946 ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
949 /* Returns 1 if the data packet is for us and 0 otherwise. */
950 static int is_data_packet_for_us(struct ieee80211_device *ieee,
951 struct ieee80211_hdr_4addr *hdr)
953 struct net_device *netdev = ieee->dev;
954 u16 fc = le16_to_cpu(hdr->frame_ctl);
956 ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
958 switch (ieee->iw_mode) {
960 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
961 memcmp(hdr->addr3, ieee->bssid, ETH_ALEN) != 0)
966 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
967 IEEE80211_FCTL_FROMDS ||
968 memcmp(hdr->addr2, ieee->bssid, ETH_ALEN) != 0)
972 ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
976 return memcmp(hdr->addr1, netdev->dev_addr, ETH_ALEN) == 0 ||
977 (is_multicast_ether_addr(hdr->addr1) &&
978 memcmp(hdr->addr3, netdev->dev_addr, ETH_ALEN) != 0) ||
979 (netdev->flags & IFF_PROMISC);
982 /* Filters received packets. The function returns 1 if the packet should be
983 * forwarded to ieee80211_rx(). If the packet should be ignored the function
984 * returns 0. If an invalid packet is found the function returns -EINVAL.
986 * The function calls ieee80211_rx_mgt() directly.
988 * It has been based on ieee80211_rx_any.
990 static int filter_rx(struct ieee80211_device *ieee,
991 const u8 *buffer, unsigned int length,
992 struct ieee80211_rx_stats *stats)
994 struct ieee80211_hdr_4addr *hdr;
997 if (ieee->iw_mode == IW_MODE_MONITOR)
1000 hdr = (struct ieee80211_hdr_4addr *)buffer;
1001 fc = le16_to_cpu(hdr->frame_ctl);
1002 if ((fc & IEEE80211_FCTL_VERS) != 0)
1005 switch (WLAN_FC_GET_TYPE(fc)) {
1006 case IEEE80211_FTYPE_MGMT:
1007 if (length < sizeof(struct ieee80211_hdr_3addr))
1009 ieee80211_rx_mgt(ieee, hdr, stats);
1011 case IEEE80211_FTYPE_CTL:
1013 case IEEE80211_FTYPE_DATA:
1014 /* Ignore invalid short buffers */
1015 if (length < sizeof(struct ieee80211_hdr_3addr))
1017 return is_data_packet_for_us(ieee, hdr);
1023 static void update_qual_rssi(struct zd_mac *mac,
1024 const u8 *buffer, unsigned int length,
1025 u8 qual_percent, u8 rssi_percent)
1027 unsigned long flags;
1028 struct ieee80211_hdr_3addr *hdr;
1031 hdr = (struct ieee80211_hdr_3addr *)buffer;
1032 if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
1034 if (memcmp(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid, ETH_ALEN) != 0)
1037 spin_lock_irqsave(&mac->lock, flags);
1038 i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
1039 mac->qual_buffer[i] = qual_percent;
1040 mac->rssi_buffer[i] = rssi_percent;
1042 spin_unlock_irqrestore(&mac->lock, flags);
1045 static int fill_rx_stats(struct ieee80211_rx_stats *stats,
1046 const struct rx_status **pstatus,
1048 const u8 *buffer, unsigned int length)
1050 const struct rx_status *status;
1052 *pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
1053 if (status->frame_status & ZD_RX_ERROR) {
1054 /* FIXME: update? */
1057 memset(stats, 0, sizeof(struct ieee80211_rx_stats));
1058 stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
1059 + sizeof(struct rx_status));
1060 /* FIXME: 802.11a */
1061 stats->freq = IEEE80211_24GHZ_BAND;
1062 stats->received_channel = _zd_chip_get_channel(&mac->chip);
1063 stats->rssi = zd_rx_strength_percent(status->signal_strength);
1064 stats->signal = zd_rx_qual_percent(buffer,
1065 length - sizeof(struct rx_status),
1067 stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
1068 stats->rate = zd_rx_rate(buffer, status);
1070 stats->mask |= IEEE80211_STATMASK_RATE;
1075 int zd_mac_rx(struct zd_mac *mac, const u8 *buffer, unsigned int length)
1078 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1079 struct ieee80211_rx_stats stats;
1080 const struct rx_status *status;
1081 struct sk_buff *skb;
1083 if (length < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
1084 IEEE80211_FCS_LEN + sizeof(struct rx_status))
1087 r = fill_rx_stats(&stats, &status, mac, buffer, length);
1091 length -= ZD_PLCP_HEADER_SIZE+IEEE80211_FCS_LEN+
1092 sizeof(struct rx_status);
1093 buffer += ZD_PLCP_HEADER_SIZE;
1095 update_qual_rssi(mac, buffer, length, stats.signal, stats.rssi);
1097 r = filter_rx(ieee, buffer, length, &stats);
1101 skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
1104 if (ieee->iw_mode == IW_MODE_MONITOR)
1105 fill_rt_header(skb_put(skb, sizeof(struct zd_rt_hdr)), mac,
1107 memcpy(skb_put(skb, length), buffer, length);
1109 r = ieee80211_rx(ieee, skb, &stats);
1111 dev_kfree_skb_any(skb);
1115 static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
1118 return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
1121 static void set_security(struct net_device *netdev,
1122 struct ieee80211_security *sec)
1124 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
1125 struct ieee80211_security *secinfo = &ieee->sec;
1128 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
1130 for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
1131 if (sec->flags & (1<<keyidx)) {
1132 secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
1133 secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
1134 memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
1138 if (sec->flags & SEC_ACTIVE_KEY) {
1139 secinfo->active_key = sec->active_key;
1140 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1141 " .active_key = %d\n", sec->active_key);
1143 if (sec->flags & SEC_UNICAST_GROUP) {
1144 secinfo->unicast_uses_group = sec->unicast_uses_group;
1145 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1146 " .unicast_uses_group = %d\n",
1147 sec->unicast_uses_group);
1149 if (sec->flags & SEC_LEVEL) {
1150 secinfo->level = sec->level;
1151 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1152 " .level = %d\n", sec->level);
1154 if (sec->flags & SEC_ENABLED) {
1155 secinfo->enabled = sec->enabled;
1156 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1157 " .enabled = %d\n", sec->enabled);
1159 if (sec->flags & SEC_ENCRYPT) {
1160 secinfo->encrypt = sec->encrypt;
1161 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1162 " .encrypt = %d\n", sec->encrypt);
1164 if (sec->flags & SEC_AUTH_MODE) {
1165 secinfo->auth_mode = sec->auth_mode;
1166 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1167 " .auth_mode = %d\n", sec->auth_mode);
1171 static void ieee_init(struct ieee80211_device *ieee)
1173 ieee->mode = IEEE_B | IEEE_G;
1174 ieee->freq_band = IEEE80211_24GHZ_BAND;
1175 ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
1176 ieee->tx_headroom = sizeof(struct zd_ctrlset);
1177 ieee->set_security = set_security;
1178 ieee->hard_start_xmit = netdev_tx;
1180 /* Software encryption/decryption for now */
1181 ieee->host_build_iv = 0;
1182 ieee->host_encrypt = 1;
1183 ieee->host_decrypt = 1;
1185 /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
1186 * correctly support AUTO */
1187 ieee->iw_mode = IW_MODE_INFRA;
1190 static void softmac_init(struct ieee80211softmac_device *sm)
1192 sm->set_channel = set_channel;
1193 sm->bssinfo_change = bssinfo_change;
1196 struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
1198 struct zd_mac *mac = zd_netdev_mac(ndev);
1199 struct iw_statistics *iw_stats = &mac->iw_stats;
1200 unsigned int i, count, qual_total, rssi_total;
1202 memset(iw_stats, 0, sizeof(struct iw_statistics));
1203 /* We are not setting the status, because ieee->state is not updated
1204 * at all and this driver doesn't track authentication state.
1206 spin_lock_irq(&mac->lock);
1207 count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
1208 mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
1209 qual_total = rssi_total = 0;
1210 for (i = 0; i < count; i++) {
1211 qual_total += mac->qual_buffer[i];
1212 rssi_total += mac->rssi_buffer[i];
1214 spin_unlock_irq(&mac->lock);
1215 iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
1217 iw_stats->qual.qual = qual_total / count;
1218 iw_stats->qual.level = rssi_total / count;
1219 iw_stats->qual.updated |=
1220 IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
1222 iw_stats->qual.updated |=
1223 IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
1225 /* TODO: update counter */
1229 #define LINK_LED_WORK_DELAY HZ
1231 static void link_led_handler(struct work_struct *work)
1233 struct zd_mac *mac =
1234 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
1235 struct zd_chip *chip = &mac->chip;
1236 struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
1240 spin_lock_irq(&mac->lock);
1241 is_associated = sm->associnfo.associated != 0;
1242 spin_unlock_irq(&mac->lock);
1244 r = zd_chip_control_leds(chip,
1245 is_associated ? LED_ASSOCIATED : LED_SCANNING);
1247 dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1249 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1250 LINK_LED_WORK_DELAY);
1253 static void housekeeping_init(struct zd_mac *mac)
1255 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
1258 static void housekeeping_enable(struct zd_mac *mac)
1260 dev_dbg_f(zd_mac_dev(mac), "\n");
1261 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1265 static void housekeeping_disable(struct zd_mac *mac)
1267 dev_dbg_f(zd_mac_dev(mac), "\n");
1268 cancel_rearming_delayed_workqueue(zd_workqueue,
1269 &mac->housekeeping.link_led_work);
1270 zd_chip_control_leds(&mac->chip, LED_OFF);