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);
36 int zd_mac_init(struct zd_mac *mac,
37 struct net_device *netdev,
38 struct usb_interface *intf)
40 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
42 memset(mac, 0, sizeof(*mac));
43 spin_lock_init(&mac->lock);
47 softmac_init(ieee80211_priv(netdev));
48 zd_chip_init(&mac->chip, netdev, intf);
52 static int reset_channel(struct zd_mac *mac)
56 const struct channel_range *range;
58 spin_lock_irqsave(&mac->lock, flags);
59 range = zd_channel_range(mac->regdomain);
64 mac->requested_channel = range->start;
67 spin_unlock_irqrestore(&mac->lock, flags);
71 int zd_mac_init_hw(struct zd_mac *mac, u8 device_type)
74 struct zd_chip *chip = &mac->chip;
78 r = zd_chip_enable_int(chip);
81 r = zd_chip_init_hw(chip, device_type);
85 zd_get_e2p_mac_addr(chip, addr);
86 r = zd_write_mac_addr(chip, addr);
89 ZD_ASSERT(!irqs_disabled());
90 spin_lock_irq(&mac->lock);
91 memcpy(mac->netdev->dev_addr, addr, ETH_ALEN);
92 spin_unlock_irq(&mac->lock);
94 r = zd_read_regdomain(chip, &default_regdomain);
97 if (!zd_regdomain_supported(default_regdomain)) {
98 dev_dbg_f(zd_mac_dev(mac),
99 "Regulatory Domain %#04x is not supported.\n",
104 spin_lock_irq(&mac->lock);
105 mac->regdomain = mac->default_regdomain = default_regdomain;
106 spin_unlock_irq(&mac->lock);
107 r = reset_channel(mac);
111 r = zd_set_encryption_type(chip, NO_WEP);
115 r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
121 zd_chip_disable_int(chip);
126 void zd_mac_clear(struct zd_mac *mac)
128 /* Aquire the lock. */
129 spin_lock(&mac->lock);
130 spin_unlock(&mac->lock);
131 zd_chip_clear(&mac->chip);
132 memset(mac, 0, sizeof(*mac));
135 static int reset_mode(struct zd_mac *mac)
137 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
138 struct zd_ioreq32 ioreqs[3] = {
139 { CR_RX_FILTER, RX_FILTER_BEACON|RX_FILTER_PROBE_RESPONSE|
140 RX_FILTER_AUTH|RX_FILTER_ASSOC_RESPONSE },
141 { CR_SNIFFER_ON, 0U },
142 { CR_ENCRYPTION_TYPE, NO_WEP },
145 if (ieee->iw_mode == IW_MODE_MONITOR) {
146 ioreqs[0].value = 0xffffffff;
147 ioreqs[1].value = 0x1;
148 ioreqs[2].value = ENC_SNIFFER;
151 return zd_iowrite32a(&mac->chip, ioreqs, 3);
154 int zd_mac_open(struct net_device *netdev)
156 struct zd_mac *mac = zd_netdev_mac(netdev);
157 struct zd_chip *chip = &mac->chip;
160 r = zd_chip_enable_int(chip);
164 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
170 r = zd_chip_switch_radio_on(chip);
173 r = zd_chip_set_channel(chip, mac->requested_channel);
176 r = zd_chip_enable_rx(chip);
179 r = zd_chip_enable_hwint(chip);
183 ieee80211softmac_start(netdev);
186 zd_chip_disable_rx(chip);
188 zd_chip_switch_radio_off(chip);
190 zd_chip_disable_int(chip);
195 int zd_mac_stop(struct net_device *netdev)
197 struct zd_mac *mac = zd_netdev_mac(netdev);
198 struct zd_chip *chip = &mac->chip;
200 netif_stop_queue(netdev);
203 * The order here deliberately is a little different from the open()
204 * method, since we need to make sure there is no opportunity for RX
205 * frames to be processed by softmac after we have stopped it.
208 zd_chip_disable_rx(chip);
209 ieee80211softmac_stop(netdev);
211 zd_chip_disable_hwint(chip);
212 zd_chip_switch_radio_off(chip);
213 zd_chip_disable_int(chip);
218 int zd_mac_set_mac_address(struct net_device *netdev, void *p)
222 struct sockaddr *addr = p;
223 struct zd_mac *mac = zd_netdev_mac(netdev);
224 struct zd_chip *chip = &mac->chip;
226 if (!is_valid_ether_addr(addr->sa_data))
227 return -EADDRNOTAVAIL;
229 dev_dbg_f(zd_mac_dev(mac),
230 "Setting MAC to " MAC_FMT "\n", MAC_ARG(addr->sa_data));
232 r = zd_write_mac_addr(chip, addr->sa_data);
236 spin_lock_irqsave(&mac->lock, flags);
237 memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
238 spin_unlock_irqrestore(&mac->lock, flags);
243 int zd_mac_set_regdomain(struct zd_mac *mac, u8 regdomain)
248 ZD_ASSERT(!irqs_disabled());
249 spin_lock_irq(&mac->lock);
250 if (regdomain == 0) {
251 regdomain = mac->default_regdomain;
253 if (!zd_regdomain_supported(regdomain)) {
254 spin_unlock_irq(&mac->lock);
257 mac->regdomain = regdomain;
258 channel = mac->requested_channel;
259 spin_unlock_irq(&mac->lock);
261 r = zd_geo_init(zd_mac_to_ieee80211(mac), regdomain);
264 if (!zd_regdomain_supports_channel(regdomain, channel)) {
265 r = reset_channel(mac);
273 u8 zd_mac_get_regdomain(struct zd_mac *mac)
278 spin_lock_irqsave(&mac->lock, flags);
279 regdomain = mac->regdomain;
280 spin_unlock_irqrestore(&mac->lock, flags);
284 static void set_channel(struct net_device *netdev, u8 channel)
286 struct zd_mac *mac = zd_netdev_mac(netdev);
288 dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
290 zd_chip_set_channel(&mac->chip, channel);
293 /* TODO: Should not work in Managed mode. */
294 int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
296 unsigned long lock_flags;
297 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
299 if (ieee->iw_mode == IW_MODE_INFRA)
302 spin_lock_irqsave(&mac->lock, lock_flags);
303 if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
304 spin_unlock_irqrestore(&mac->lock, lock_flags);
307 mac->requested_channel = channel;
308 spin_unlock_irqrestore(&mac->lock, lock_flags);
309 if (netif_running(mac->netdev))
310 return zd_chip_set_channel(&mac->chip, channel);
315 int zd_mac_get_channel(struct zd_mac *mac, u8 *channel, u8 *flags)
317 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
319 *channel = zd_chip_get_channel(&mac->chip);
320 if (ieee->iw_mode != IW_MODE_INFRA) {
321 spin_lock_irq(&mac->lock);
322 *flags = *channel == mac->requested_channel ?
323 MAC_FIXED_CHANNEL : 0;
324 spin_unlock(&mac->lock);
328 dev_dbg_f(zd_mac_dev(mac), "channel %u flags %u\n", *channel, *flags);
332 /* If wrong rate is given, we are falling back to the slowest rate: 1MBit/s */
333 static u8 cs_typed_rate(u8 cs_rate)
335 static const u8 typed_rates[16] = {
336 [ZD_CS_CCK_RATE_1M] = ZD_CS_CCK|ZD_CS_CCK_RATE_1M,
337 [ZD_CS_CCK_RATE_2M] = ZD_CS_CCK|ZD_CS_CCK_RATE_2M,
338 [ZD_CS_CCK_RATE_5_5M] = ZD_CS_CCK|ZD_CS_CCK_RATE_5_5M,
339 [ZD_CS_CCK_RATE_11M] = ZD_CS_CCK|ZD_CS_CCK_RATE_11M,
340 [ZD_OFDM_RATE_6M] = ZD_CS_OFDM|ZD_OFDM_RATE_6M,
341 [ZD_OFDM_RATE_9M] = ZD_CS_OFDM|ZD_OFDM_RATE_9M,
342 [ZD_OFDM_RATE_12M] = ZD_CS_OFDM|ZD_OFDM_RATE_12M,
343 [ZD_OFDM_RATE_18M] = ZD_CS_OFDM|ZD_OFDM_RATE_18M,
344 [ZD_OFDM_RATE_24M] = ZD_CS_OFDM|ZD_OFDM_RATE_24M,
345 [ZD_OFDM_RATE_36M] = ZD_CS_OFDM|ZD_OFDM_RATE_36M,
346 [ZD_OFDM_RATE_48M] = ZD_CS_OFDM|ZD_OFDM_RATE_48M,
347 [ZD_OFDM_RATE_54M] = ZD_CS_OFDM|ZD_OFDM_RATE_54M,
350 ZD_ASSERT(ZD_CS_RATE_MASK == 0x0f);
351 return typed_rates[cs_rate & ZD_CS_RATE_MASK];
354 /* Fallback to lowest rate, if rate is unknown. */
355 static u8 rate_to_cs_rate(u8 rate)
358 case IEEE80211_CCK_RATE_2MB:
359 return ZD_CS_CCK_RATE_2M;
360 case IEEE80211_CCK_RATE_5MB:
361 return ZD_CS_CCK_RATE_5_5M;
362 case IEEE80211_CCK_RATE_11MB:
363 return ZD_CS_CCK_RATE_11M;
364 case IEEE80211_OFDM_RATE_6MB:
365 return ZD_OFDM_RATE_6M;
366 case IEEE80211_OFDM_RATE_9MB:
367 return ZD_OFDM_RATE_9M;
368 case IEEE80211_OFDM_RATE_12MB:
369 return ZD_OFDM_RATE_12M;
370 case IEEE80211_OFDM_RATE_18MB:
371 return ZD_OFDM_RATE_18M;
372 case IEEE80211_OFDM_RATE_24MB:
373 return ZD_OFDM_RATE_24M;
374 case IEEE80211_OFDM_RATE_36MB:
375 return ZD_OFDM_RATE_36M;
376 case IEEE80211_OFDM_RATE_48MB:
377 return ZD_OFDM_RATE_48M;
378 case IEEE80211_OFDM_RATE_54MB:
379 return ZD_OFDM_RATE_54M;
381 return ZD_CS_CCK_RATE_1M;
384 int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
386 struct ieee80211_device *ieee;
392 mac->netdev->type = ARPHRD_ETHER;
394 case IW_MODE_MONITOR:
395 mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
398 dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
402 ieee = zd_mac_to_ieee80211(mac);
403 ZD_ASSERT(!irqs_disabled());
404 spin_lock_irq(&ieee->lock);
405 ieee->iw_mode = mode;
406 spin_unlock_irq(&ieee->lock);
408 if (netif_running(mac->netdev))
409 return reset_mode(mac);
414 int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
417 struct ieee80211_device *ieee;
419 ieee = zd_mac_to_ieee80211(mac);
420 spin_lock_irqsave(&ieee->lock, flags);
421 *mode = ieee->iw_mode;
422 spin_unlock_irqrestore(&ieee->lock, flags);
426 int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
429 const struct channel_range *channel_range;
432 memset(range, 0, sizeof(*range));
434 /* FIXME: Not so important and depends on the mode. For 802.11g
435 * usually this value is used. It seems to be that Bit/s number is
438 range->throughput = 27 * 1000 * 1000;
440 range->max_qual.qual = 100;
441 range->max_qual.level = 100;
443 /* FIXME: Needs still to be tuned. */
444 range->avg_qual.qual = 71;
445 range->avg_qual.level = 80;
447 /* FIXME: depends on standard? */
448 range->min_rts = 256;
449 range->max_rts = 2346;
451 range->min_frag = MIN_FRAG_THRESHOLD;
452 range->max_frag = MAX_FRAG_THRESHOLD;
454 range->max_encoding_tokens = WEP_KEYS;
455 range->num_encoding_sizes = 2;
456 range->encoding_size[0] = 5;
457 range->encoding_size[1] = WEP_KEY_LEN;
459 range->we_version_compiled = WIRELESS_EXT;
460 range->we_version_source = 20;
462 ZD_ASSERT(!irqs_disabled());
463 spin_lock_irq(&mac->lock);
464 regdomain = mac->regdomain;
465 spin_unlock_irq(&mac->lock);
466 channel_range = zd_channel_range(regdomain);
468 range->num_channels = channel_range->end - channel_range->start;
469 range->old_num_channels = range->num_channels;
470 range->num_frequency = range->num_channels;
471 range->old_num_frequency = range->num_frequency;
473 for (i = 0; i < range->num_frequency; i++) {
474 struct iw_freq *freq = &range->freq[i];
475 freq->i = channel_range->start + i;
476 zd_channel_to_freq(freq, freq->i);
482 static int zd_calc_tx_length_us(u8 *service, u8 cs_rate, u16 tx_length)
484 static const u8 rate_divisor[] = {
485 [ZD_CS_CCK_RATE_1M] = 1,
486 [ZD_CS_CCK_RATE_2M] = 2,
487 [ZD_CS_CCK_RATE_5_5M] = 11, /* bits must be doubled */
488 [ZD_CS_CCK_RATE_11M] = 11,
489 [ZD_OFDM_RATE_6M] = 6,
490 [ZD_OFDM_RATE_9M] = 9,
491 [ZD_OFDM_RATE_12M] = 12,
492 [ZD_OFDM_RATE_18M] = 18,
493 [ZD_OFDM_RATE_24M] = 24,
494 [ZD_OFDM_RATE_36M] = 36,
495 [ZD_OFDM_RATE_48M] = 48,
496 [ZD_OFDM_RATE_54M] = 54,
499 u32 bits = (u32)tx_length * 8;
502 divisor = rate_divisor[cs_rate];
507 case ZD_CS_CCK_RATE_5_5M:
508 bits = (2*bits) + 10; /* round up to the next integer */
510 case ZD_CS_CCK_RATE_11M:
513 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
514 if (0 < t && t <= 3) {
515 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
518 bits += 10; /* round up to the next integer */
526 R2M_SHORT_PREAMBLE = 0x01,
530 static u8 cs_rate_to_modulation(u8 cs_rate, int flags)
534 modulation = cs_typed_rate(cs_rate);
535 if (flags & R2M_SHORT_PREAMBLE) {
536 switch (ZD_CS_RATE(modulation)) {
537 case ZD_CS_CCK_RATE_2M:
538 case ZD_CS_CCK_RATE_5_5M:
539 case ZD_CS_CCK_RATE_11M:
540 modulation |= ZD_CS_CCK_PREA_SHORT;
544 if (flags & R2M_11A) {
545 if (ZD_CS_TYPE(modulation) == ZD_CS_OFDM)
546 modulation |= ZD_CS_OFDM_MODE_11A;
551 static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
552 struct ieee80211_hdr_4addr *hdr)
554 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
555 u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
557 int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
559 /* FIXME: 802.11a? short preamble? */
560 rate = ieee80211softmac_suggest_txrate(softmac,
561 is_multicast_ether_addr(hdr->addr1), is_mgt);
563 cs_rate = rate_to_cs_rate(rate);
564 cs->modulation = cs_rate_to_modulation(cs_rate, 0);
567 static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
568 struct ieee80211_hdr_4addr *header)
570 unsigned int tx_length = le16_to_cpu(cs->tx_length);
571 u16 fctl = le16_to_cpu(header->frame_ctl);
572 u16 ftype = WLAN_FC_GET_TYPE(fctl);
573 u16 stype = WLAN_FC_GET_STYPE(fctl);
578 * - if fragment 0, enable bit 0
579 * - if backoff needed, enable bit 0
580 * - if burst (backoff not needed) disable bit 0
581 * - if multicast, enable bit 1
582 * - if PS-POLL frame, enable bit 2
583 * - if in INDEPENDENT_BSS mode and zd1205_DestPowerSave, then enable
585 * - if frag_len > RTS threshold, set bit 5 as long if it isnt
587 * - if bit 5 is set, and we are in OFDM mode, unset bit 5 and set bit
594 if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
595 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
598 if (is_multicast_ether_addr(header->addr1))
599 cs->control |= ZD_CS_MULTICAST;
602 if (stype == IEEE80211_STYPE_PSPOLL)
603 cs->control |= ZD_CS_PS_POLL_FRAME;
605 if (!is_multicast_ether_addr(header->addr1) &&
606 ftype != IEEE80211_FTYPE_MGMT &&
607 tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
609 /* FIXME: check the logic */
610 if (ZD_CS_TYPE(cs->modulation) == ZD_CS_OFDM) {
612 cs->control |= ZD_CS_SELF_CTS;
613 } else { /* 802.11b */
614 cs->control |= ZD_CS_RTS;
618 /* FIXME: Management frame? */
621 static int fill_ctrlset(struct zd_mac *mac,
622 struct ieee80211_txb *txb,
626 struct sk_buff *skb = txb->fragments[frag_num];
627 struct ieee80211_hdr_4addr *hdr =
628 (struct ieee80211_hdr_4addr *) skb->data;
629 unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
630 unsigned int next_frag_len;
631 unsigned int packet_length;
632 struct zd_ctrlset *cs = (struct zd_ctrlset *)
633 skb_push(skb, sizeof(struct zd_ctrlset));
635 if (frag_num+1 < txb->nr_frags) {
636 next_frag_len = txb->fragments[frag_num+1]->len +
641 ZD_ASSERT(frag_len <= 0xffff);
642 ZD_ASSERT(next_frag_len <= 0xffff);
644 cs_set_modulation(mac, cs, hdr);
646 cs->tx_length = cpu_to_le16(frag_len);
648 cs_set_control(mac, cs, hdr);
650 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
651 ZD_ASSERT(packet_length <= 0xffff);
652 /* ZD1211B: Computing the length difference this way, gives us
653 * flexibility to compute the packet length.
655 cs->packet_length = cpu_to_le16(mac->chip.is_zd1211b ?
656 packet_length - frag_len : packet_length);
660 * - transmit frame length in microseconds
661 * - seems to be derived from frame length
662 * - see Cal_Us_Service() in zdinlinef.h
663 * - if macp->bTxBurstEnable is enabled, then multiply by 4
664 * - bTxBurstEnable is never set in the vendor driver
667 * - "for PLCP configuration"
668 * - always 0 except in some situations at 802.11b 11M
669 * - see line 53 of zdinlinef.h
672 r = zd_calc_tx_length_us(&cs->service, ZD_CS_RATE(cs->modulation),
673 le16_to_cpu(cs->tx_length));
676 cs->current_length = cpu_to_le16(r);
678 if (next_frag_len == 0) {
679 cs->next_frame_length = 0;
681 r = zd_calc_tx_length_us(NULL, ZD_CS_RATE(cs->modulation),
685 cs->next_frame_length = cpu_to_le16(r);
691 static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
695 for (i = 0; i < txb->nr_frags; i++) {
696 struct sk_buff *skb = txb->fragments[i];
698 r = fill_ctrlset(mac, txb, i);
701 r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
706 /* FIXME: shouldn't this be handled by the upper layers? */
707 mac->netdev->trans_start = jiffies;
709 ieee80211_txb_free(txb);
714 struct ieee80211_radiotap_header rt_hdr;
721 static void fill_rt_header(void *buffer, struct zd_mac *mac,
722 const struct ieee80211_rx_stats *stats,
723 const struct rx_status *status)
725 struct zd_rt_hdr *hdr = buffer;
727 hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
728 hdr->rt_hdr.it_pad = 0;
729 hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
730 hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
731 (1 << IEEE80211_RADIOTAP_CHANNEL) |
732 (1 << IEEE80211_RADIOTAP_RATE));
735 if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
736 hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
739 hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
740 _zd_chip_get_channel(&mac->chip)));
741 hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
742 ((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
743 ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
745 hdr->rt_rate = stats->rate / 5;
748 /* Returns 1 if the data packet is for us and 0 otherwise. */
749 static int is_data_packet_for_us(struct ieee80211_device *ieee,
750 struct ieee80211_hdr_4addr *hdr)
752 struct net_device *netdev = ieee->dev;
753 u16 fc = le16_to_cpu(hdr->frame_ctl);
755 ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
757 switch (ieee->iw_mode) {
759 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
760 memcmp(hdr->addr3, ieee->bssid, ETH_ALEN) != 0)
765 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
766 IEEE80211_FCTL_FROMDS ||
767 memcmp(hdr->addr2, ieee->bssid, ETH_ALEN) != 0)
771 ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
775 return memcmp(hdr->addr1, netdev->dev_addr, ETH_ALEN) == 0 ||
776 is_multicast_ether_addr(hdr->addr1) ||
777 (netdev->flags & IFF_PROMISC);
780 /* Filters receiving packets. If it returns 1 send it to ieee80211_rx, if 0
781 * return. If an error is detected -EINVAL is returned. ieee80211_rx_mgt() is
784 * It has been based on ieee80211_rx_any.
786 static int filter_rx(struct ieee80211_device *ieee,
787 const u8 *buffer, unsigned int length,
788 struct ieee80211_rx_stats *stats)
790 struct ieee80211_hdr_4addr *hdr;
793 if (ieee->iw_mode == IW_MODE_MONITOR)
796 hdr = (struct ieee80211_hdr_4addr *)buffer;
797 fc = le16_to_cpu(hdr->frame_ctl);
798 if ((fc & IEEE80211_FCTL_VERS) != 0)
801 switch (WLAN_FC_GET_TYPE(fc)) {
802 case IEEE80211_FTYPE_MGMT:
803 if (length < sizeof(struct ieee80211_hdr_3addr))
805 ieee80211_rx_mgt(ieee, hdr, stats);
807 case IEEE80211_FTYPE_CTL:
808 /* Ignore invalid short buffers */
810 case IEEE80211_FTYPE_DATA:
811 if (length < sizeof(struct ieee80211_hdr_3addr))
813 return is_data_packet_for_us(ieee, hdr);
819 static void update_qual_rssi(struct zd_mac *mac, u8 qual_percent, u8 rssi)
823 spin_lock_irqsave(&mac->lock, flags);
824 mac->qual_average = (7 * mac->qual_average + qual_percent) / 8;
825 mac->rssi_average = (7 * mac->rssi_average + rssi) / 8;
826 spin_unlock_irqrestore(&mac->lock, flags);
829 static int fill_rx_stats(struct ieee80211_rx_stats *stats,
830 const struct rx_status **pstatus,
832 const u8 *buffer, unsigned int length)
834 const struct rx_status *status;
836 *pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
837 if (status->frame_status & ZD_RX_ERROR) {
841 memset(stats, 0, sizeof(struct ieee80211_rx_stats));
842 stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
843 + sizeof(struct rx_status));
845 stats->freq = IEEE80211_24GHZ_BAND;
846 stats->received_channel = _zd_chip_get_channel(&mac->chip);
847 stats->rssi = zd_rx_strength_percent(status->signal_strength);
848 stats->signal = zd_rx_qual_percent(buffer,
849 length - sizeof(struct rx_status),
851 stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
852 stats->rate = zd_rx_rate(buffer, status);
854 stats->mask |= IEEE80211_STATMASK_RATE;
856 update_qual_rssi(mac, stats->signal, stats->rssi);
860 int zd_mac_rx(struct zd_mac *mac, const u8 *buffer, unsigned int length)
863 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
864 struct ieee80211_rx_stats stats;
865 const struct rx_status *status;
868 if (length < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
869 IEEE80211_FCS_LEN + sizeof(struct rx_status))
872 r = fill_rx_stats(&stats, &status, mac, buffer, length);
876 length -= ZD_PLCP_HEADER_SIZE+IEEE80211_FCS_LEN+
877 sizeof(struct rx_status);
878 buffer += ZD_PLCP_HEADER_SIZE;
880 r = filter_rx(ieee, buffer, length, &stats);
884 skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
887 if (ieee->iw_mode == IW_MODE_MONITOR)
888 fill_rt_header(skb_put(skb, sizeof(struct zd_rt_hdr)), mac,
890 memcpy(skb_put(skb, length), buffer, length);
892 r = ieee80211_rx(ieee, skb, &stats);
895 dev_kfree_skb_irq(skb);
900 static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
903 return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
906 static void set_security(struct net_device *netdev,
907 struct ieee80211_security *sec)
909 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
910 struct ieee80211_security *secinfo = &ieee->sec;
913 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
915 for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
916 if (sec->flags & (1<<keyidx)) {
917 secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
918 secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
919 memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
923 if (sec->flags & SEC_ACTIVE_KEY) {
924 secinfo->active_key = sec->active_key;
925 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
926 " .active_key = %d\n", sec->active_key);
928 if (sec->flags & SEC_UNICAST_GROUP) {
929 secinfo->unicast_uses_group = sec->unicast_uses_group;
930 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
931 " .unicast_uses_group = %d\n",
932 sec->unicast_uses_group);
934 if (sec->flags & SEC_LEVEL) {
935 secinfo->level = sec->level;
936 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
937 " .level = %d\n", sec->level);
939 if (sec->flags & SEC_ENABLED) {
940 secinfo->enabled = sec->enabled;
941 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
942 " .enabled = %d\n", sec->enabled);
944 if (sec->flags & SEC_ENCRYPT) {
945 secinfo->encrypt = sec->encrypt;
946 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
947 " .encrypt = %d\n", sec->encrypt);
949 if (sec->flags & SEC_AUTH_MODE) {
950 secinfo->auth_mode = sec->auth_mode;
951 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
952 " .auth_mode = %d\n", sec->auth_mode);
956 static void ieee_init(struct ieee80211_device *ieee)
958 ieee->mode = IEEE_B | IEEE_G;
959 ieee->freq_band = IEEE80211_24GHZ_BAND;
960 ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
961 ieee->tx_headroom = sizeof(struct zd_ctrlset);
962 ieee->set_security = set_security;
963 ieee->hard_start_xmit = netdev_tx;
965 /* Software encryption/decryption for now */
966 ieee->host_build_iv = 0;
967 ieee->host_encrypt = 1;
968 ieee->host_decrypt = 1;
970 /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
971 * correctly support AUTO */
972 ieee->iw_mode = IW_MODE_INFRA;
975 static void softmac_init(struct ieee80211softmac_device *sm)
977 sm->set_channel = set_channel;
980 struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
982 struct zd_mac *mac = zd_netdev_mac(ndev);
983 struct iw_statistics *iw_stats = &mac->iw_stats;
985 memset(iw_stats, 0, sizeof(struct iw_statistics));
986 /* We are not setting the status, because ieee->state is not updated
987 * at all and this driver doesn't track authentication state.
989 spin_lock_irq(&mac->lock);
990 iw_stats->qual.qual = mac->qual_average;
991 iw_stats->qual.level = mac->rssi_average;
992 iw_stats->qual.updated = IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED|
993 IW_QUAL_NOISE_INVALID;
994 spin_unlock_irq(&mac->lock);
995 /* TODO: update counter */
1000 static const char* decryption_types[] = {
1001 [ZD_RX_NO_WEP] = "none",
1002 [ZD_RX_WEP64] = "WEP64",
1003 [ZD_RX_TKIP] = "TKIP",
1004 [ZD_RX_AES] = "AES",
1005 [ZD_RX_WEP128] = "WEP128",
1006 [ZD_RX_WEP256] = "WEP256",
1009 static const char *decryption_type_string(u8 type)
1013 if (type < ARRAY_SIZE(decryption_types)) {
1014 s = decryption_types[type];
1018 return s ? s : "unknown";
1021 static int is_ofdm(u8 frame_status)
1023 return (frame_status & ZD_RX_OFDM);
1026 void zd_dump_rx_status(const struct rx_status *status)
1028 const char* modulation;
1031 if (is_ofdm(status->frame_status)) {
1032 modulation = "ofdm";
1033 quality = status->signal_quality_ofdm;
1036 quality = status->signal_quality_cck;
1038 pr_debug("rx status %s strength %#04x qual %#04x decryption %s\n",
1039 modulation, status->signal_strength, quality,
1040 decryption_type_string(status->decryption_type));
1041 if (status->frame_status & ZD_RX_ERROR) {
1042 pr_debug("rx error %s%s%s%s%s%s\n",
1043 (status->frame_status & ZD_RX_TIMEOUT_ERROR) ?
1045 (status->frame_status & ZD_RX_FIFO_OVERRUN_ERROR) ?
1047 (status->frame_status & ZD_RX_DECRYPTION_ERROR) ?
1049 (status->frame_status & ZD_RX_CRC32_ERROR) ?
1051 (status->frame_status & ZD_RX_NO_ADDR1_MATCH_ERROR) ?
1053 (status->frame_status & ZD_RX_CRC16_ERROR) ?