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(void *d);
36 static void set_basic_rates_work(void *d);
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(void *mac_ptr);
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_WORK(&mac->set_rts_cts_work, set_rts_cts_work, mac);
54 INIT_WORK(&mac->set_basic_rates_work, set_basic_rates_work, mac);
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,
65 static int reset_channel(struct zd_mac *mac)
69 const struct channel_range *range;
71 spin_lock_irqsave(&mac->lock, flags);
72 range = zd_channel_range(mac->regdomain);
77 mac->requested_channel = range->start;
80 spin_unlock_irqrestore(&mac->lock, flags);
84 int zd_mac_init_hw(struct zd_mac *mac, u8 device_type)
87 struct zd_chip *chip = &mac->chip;
91 r = zd_chip_enable_int(chip);
94 r = zd_chip_init_hw(chip, device_type);
98 zd_get_e2p_mac_addr(chip, addr);
99 r = zd_write_mac_addr(chip, addr);
102 ZD_ASSERT(!irqs_disabled());
103 spin_lock_irq(&mac->lock);
104 memcpy(mac->netdev->dev_addr, addr, ETH_ALEN);
105 spin_unlock_irq(&mac->lock);
107 r = zd_read_regdomain(chip, &default_regdomain);
110 if (!zd_regdomain_supported(default_regdomain)) {
111 dev_dbg_f(zd_mac_dev(mac),
112 "Regulatory Domain %#04x is not supported.\n",
117 spin_lock_irq(&mac->lock);
118 mac->regdomain = mac->default_regdomain = default_regdomain;
119 spin_unlock_irq(&mac->lock);
120 r = reset_channel(mac);
124 /* We must inform the device that we are doing encryption/decryption in
125 * software at the moment. */
126 r = zd_set_encryption_type(chip, ENC_SNIFFER);
130 r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
136 zd_chip_disable_int(chip);
141 void zd_mac_clear(struct zd_mac *mac)
143 flush_workqueue(zd_workqueue);
144 zd_chip_clear(&mac->chip);
145 ZD_ASSERT(!spin_is_locked(&mac->lock));
146 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
149 static int reset_mode(struct zd_mac *mac)
151 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
152 struct zd_ioreq32 ioreqs[3] = {
153 { CR_RX_FILTER, STA_RX_FILTER },
154 { CR_SNIFFER_ON, 0U },
157 if (ieee->iw_mode == IW_MODE_MONITOR) {
158 ioreqs[0].value = 0xffffffff;
159 ioreqs[1].value = 0x1;
160 ioreqs[2].value = ENC_SNIFFER;
163 return zd_iowrite32a(&mac->chip, ioreqs, 3);
166 int zd_mac_open(struct net_device *netdev)
168 struct zd_mac *mac = zd_netdev_mac(netdev);
169 struct zd_chip *chip = &mac->chip;
172 r = zd_chip_enable_int(chip);
176 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
182 r = zd_chip_switch_radio_on(chip);
185 r = zd_chip_set_channel(chip, mac->requested_channel);
188 r = zd_chip_enable_rx(chip);
191 r = zd_chip_enable_hwint(chip);
195 housekeeping_enable(mac);
196 ieee80211softmac_start(netdev);
199 zd_chip_disable_rx(chip);
201 zd_chip_switch_radio_off(chip);
203 zd_chip_disable_int(chip);
208 int zd_mac_stop(struct net_device *netdev)
210 struct zd_mac *mac = zd_netdev_mac(netdev);
211 struct zd_chip *chip = &mac->chip;
213 netif_stop_queue(netdev);
216 * The order here deliberately is a little different from the open()
217 * method, since we need to make sure there is no opportunity for RX
218 * frames to be processed by softmac after we have stopped it.
221 zd_chip_disable_rx(chip);
222 housekeeping_disable(mac);
223 ieee80211softmac_stop(netdev);
225 /* Ensure no work items are running or queued from this point */
226 cancel_delayed_work(&mac->set_rts_cts_work);
227 cancel_delayed_work(&mac->set_basic_rates_work);
228 flush_workqueue(zd_workqueue);
229 mac->updating_rts_rate = 0;
230 mac->updating_basic_rates = 0;
232 zd_chip_disable_hwint(chip);
233 zd_chip_switch_radio_off(chip);
234 zd_chip_disable_int(chip);
239 int zd_mac_set_mac_address(struct net_device *netdev, void *p)
243 struct sockaddr *addr = p;
244 struct zd_mac *mac = zd_netdev_mac(netdev);
245 struct zd_chip *chip = &mac->chip;
247 if (!is_valid_ether_addr(addr->sa_data))
248 return -EADDRNOTAVAIL;
250 dev_dbg_f(zd_mac_dev(mac),
251 "Setting MAC to " MAC_FMT "\n", MAC_ARG(addr->sa_data));
253 r = zd_write_mac_addr(chip, addr->sa_data);
257 spin_lock_irqsave(&mac->lock, flags);
258 memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
259 spin_unlock_irqrestore(&mac->lock, flags);
264 static void set_multicast_hash_handler(void *mac_ptr)
266 struct zd_mac *mac = mac_ptr;
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(void *d)
412 struct zd_mac *mac = d;
415 unsigned int short_preamble;
417 mutex_lock(&mac->chip.mutex);
419 spin_lock_irqsave(&mac->lock, flags);
420 mac->updating_rts_rate = 0;
421 rts_rate = mac->rts_rate;
422 short_preamble = mac->short_preamble;
423 spin_unlock_irqrestore(&mac->lock, flags);
425 zd_chip_set_rts_cts_rate_locked(&mac->chip, rts_rate, short_preamble);
426 mutex_unlock(&mac->chip.mutex);
431 static void set_basic_rates_work(void *d)
433 struct zd_mac *mac = d;
437 mutex_lock(&mac->chip.mutex);
439 spin_lock_irqsave(&mac->lock, flags);
440 mac->updating_basic_rates = 0;
441 basic_rates = mac->basic_rates;
442 spin_unlock_irqrestore(&mac->lock, flags);
444 zd_chip_set_basic_rates_locked(&mac->chip, basic_rates);
445 mutex_unlock(&mac->chip.mutex);
450 static void bssinfo_change(struct net_device *netdev, u32 changes)
452 struct zd_mac *mac = zd_netdev_mac(netdev);
453 struct ieee80211softmac_device *softmac = ieee80211_priv(netdev);
454 struct ieee80211softmac_bss_info *bssinfo = &softmac->bssinfo;
455 int need_set_rts_cts = 0;
456 int need_set_rates = 0;
460 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
462 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_SHORT_PREAMBLE) {
463 spin_lock_irqsave(&mac->lock, flags);
464 mac->short_preamble = bssinfo->short_preamble;
465 spin_unlock_irqrestore(&mac->lock, flags);
466 need_set_rts_cts = 1;
469 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_RATES) {
470 /* Set RTS rate to highest available basic rate */
471 u8 rate = ieee80211softmac_highest_supported_rate(softmac,
472 &bssinfo->supported_rates, 1);
473 rate = rate_to_zd_rate(rate);
475 spin_lock_irqsave(&mac->lock, flags);
476 if (rate != mac->rts_rate) {
477 mac->rts_rate = rate;
478 need_set_rts_cts = 1;
480 spin_unlock_irqrestore(&mac->lock, flags);
482 /* Set basic rates */
484 if (bssinfo->supported_rates.count == 0) {
485 /* Allow the device to be flexible */
486 basic_rates = CR_RATES_80211B | CR_RATES_80211G;
491 for (i = 0; i < bssinfo->supported_rates.count; i++) {
492 u16 rate = bssinfo->supported_rates.rates[i];
493 if ((rate & IEEE80211_BASIC_RATE_MASK) == 0)
496 rate &= ~IEEE80211_BASIC_RATE_MASK;
497 basic_rates |= rate_to_cr_rate(rate);
500 spin_lock_irqsave(&mac->lock, flags);
501 mac->basic_rates = basic_rates;
502 spin_unlock_irqrestore(&mac->lock, flags);
505 /* Schedule any changes we made above */
507 spin_lock_irqsave(&mac->lock, flags);
508 if (need_set_rts_cts && !mac->updating_rts_rate) {
509 mac->updating_rts_rate = 1;
510 netif_stop_queue(mac->netdev);
511 queue_work(zd_workqueue, &mac->set_rts_cts_work);
513 if (need_set_rates && !mac->updating_basic_rates) {
514 mac->updating_basic_rates = 1;
515 netif_stop_queue(mac->netdev);
516 queue_work(zd_workqueue, &mac->set_basic_rates_work);
518 spin_unlock_irqrestore(&mac->lock, flags);
521 static void set_channel(struct net_device *netdev, u8 channel)
523 struct zd_mac *mac = zd_netdev_mac(netdev);
525 dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
527 zd_chip_set_channel(&mac->chip, channel);
530 int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
532 unsigned long lock_flags;
533 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
535 if (ieee->iw_mode == IW_MODE_INFRA)
538 spin_lock_irqsave(&mac->lock, lock_flags);
539 if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
540 spin_unlock_irqrestore(&mac->lock, lock_flags);
543 mac->requested_channel = channel;
544 spin_unlock_irqrestore(&mac->lock, lock_flags);
545 if (netif_running(mac->netdev))
546 return zd_chip_set_channel(&mac->chip, channel);
551 u8 zd_mac_get_channel(struct zd_mac *mac)
553 u8 channel = zd_chip_get_channel(&mac->chip);
555 dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
559 /* If wrong rate is given, we are falling back to the slowest rate: 1MBit/s */
560 static u8 zd_rate_typed(u8 zd_rate)
562 static const u8 typed_rates[16] = {
563 [ZD_CCK_RATE_1M] = ZD_CS_CCK|ZD_CCK_RATE_1M,
564 [ZD_CCK_RATE_2M] = ZD_CS_CCK|ZD_CCK_RATE_2M,
565 [ZD_CCK_RATE_5_5M] = ZD_CS_CCK|ZD_CCK_RATE_5_5M,
566 [ZD_CCK_RATE_11M] = ZD_CS_CCK|ZD_CCK_RATE_11M,
567 [ZD_OFDM_RATE_6M] = ZD_CS_OFDM|ZD_OFDM_RATE_6M,
568 [ZD_OFDM_RATE_9M] = ZD_CS_OFDM|ZD_OFDM_RATE_9M,
569 [ZD_OFDM_RATE_12M] = ZD_CS_OFDM|ZD_OFDM_RATE_12M,
570 [ZD_OFDM_RATE_18M] = ZD_CS_OFDM|ZD_OFDM_RATE_18M,
571 [ZD_OFDM_RATE_24M] = ZD_CS_OFDM|ZD_OFDM_RATE_24M,
572 [ZD_OFDM_RATE_36M] = ZD_CS_OFDM|ZD_OFDM_RATE_36M,
573 [ZD_OFDM_RATE_48M] = ZD_CS_OFDM|ZD_OFDM_RATE_48M,
574 [ZD_OFDM_RATE_54M] = ZD_CS_OFDM|ZD_OFDM_RATE_54M,
577 ZD_ASSERT(ZD_CS_RATE_MASK == 0x0f);
578 return typed_rates[zd_rate & ZD_CS_RATE_MASK];
581 int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
583 struct ieee80211_device *ieee;
589 mac->netdev->type = ARPHRD_ETHER;
591 case IW_MODE_MONITOR:
592 mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
595 dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
599 ieee = zd_mac_to_ieee80211(mac);
600 ZD_ASSERT(!irqs_disabled());
601 spin_lock_irq(&ieee->lock);
602 ieee->iw_mode = mode;
603 spin_unlock_irq(&ieee->lock);
605 if (netif_running(mac->netdev))
606 return reset_mode(mac);
611 int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
614 struct ieee80211_device *ieee;
616 ieee = zd_mac_to_ieee80211(mac);
617 spin_lock_irqsave(&ieee->lock, flags);
618 *mode = ieee->iw_mode;
619 spin_unlock_irqrestore(&ieee->lock, flags);
623 int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
626 const struct channel_range *channel_range;
629 memset(range, 0, sizeof(*range));
631 /* FIXME: Not so important and depends on the mode. For 802.11g
632 * usually this value is used. It seems to be that Bit/s number is
635 range->throughput = 27 * 1000 * 1000;
637 range->max_qual.qual = 100;
638 range->max_qual.level = 100;
640 /* FIXME: Needs still to be tuned. */
641 range->avg_qual.qual = 71;
642 range->avg_qual.level = 80;
644 /* FIXME: depends on standard? */
645 range->min_rts = 256;
646 range->max_rts = 2346;
648 range->min_frag = MIN_FRAG_THRESHOLD;
649 range->max_frag = MAX_FRAG_THRESHOLD;
651 range->max_encoding_tokens = WEP_KEYS;
652 range->num_encoding_sizes = 2;
653 range->encoding_size[0] = 5;
654 range->encoding_size[1] = WEP_KEY_LEN;
656 range->we_version_compiled = WIRELESS_EXT;
657 range->we_version_source = 20;
659 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
660 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
662 ZD_ASSERT(!irqs_disabled());
663 spin_lock_irq(&mac->lock);
664 regdomain = mac->regdomain;
665 spin_unlock_irq(&mac->lock);
666 channel_range = zd_channel_range(regdomain);
668 range->num_channels = channel_range->end - channel_range->start;
669 range->old_num_channels = range->num_channels;
670 range->num_frequency = range->num_channels;
671 range->old_num_frequency = range->num_frequency;
673 for (i = 0; i < range->num_frequency; i++) {
674 struct iw_freq *freq = &range->freq[i];
675 freq->i = channel_range->start + i;
676 zd_channel_to_freq(freq, freq->i);
682 static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
684 static const u8 rate_divisor[] = {
685 [ZD_CCK_RATE_1M] = 1,
686 [ZD_CCK_RATE_2M] = 2,
687 [ZD_CCK_RATE_5_5M] = 11, /* bits must be doubled */
688 [ZD_CCK_RATE_11M] = 11,
689 [ZD_OFDM_RATE_6M] = 6,
690 [ZD_OFDM_RATE_9M] = 9,
691 [ZD_OFDM_RATE_12M] = 12,
692 [ZD_OFDM_RATE_18M] = 18,
693 [ZD_OFDM_RATE_24M] = 24,
694 [ZD_OFDM_RATE_36M] = 36,
695 [ZD_OFDM_RATE_48M] = 48,
696 [ZD_OFDM_RATE_54M] = 54,
699 u32 bits = (u32)tx_length * 8;
702 divisor = rate_divisor[zd_rate];
707 case ZD_CCK_RATE_5_5M:
708 bits = (2*bits) + 10; /* round up to the next integer */
710 case ZD_CCK_RATE_11M:
713 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
714 if (0 < t && t <= 3) {
715 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
718 bits += 10; /* round up to the next integer */
726 R2M_SHORT_PREAMBLE = 0x01,
730 static u8 zd_rate_to_modulation(u8 zd_rate, int flags)
734 modulation = zd_rate_typed(zd_rate);
735 if (flags & R2M_SHORT_PREAMBLE) {
736 switch (ZD_CS_RATE(modulation)) {
738 case ZD_CCK_RATE_5_5M:
739 case ZD_CCK_RATE_11M:
740 modulation |= ZD_CS_CCK_PREA_SHORT;
744 if (flags & R2M_11A) {
745 if (ZD_CS_TYPE(modulation) == ZD_CS_OFDM)
746 modulation |= ZD_CS_OFDM_MODE_11A;
751 static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
752 struct ieee80211_hdr_4addr *hdr)
754 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
755 u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
757 int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
758 int is_multicast = is_multicast_ether_addr(hdr->addr1);
759 int short_preamble = ieee80211softmac_short_preamble_ok(softmac,
760 is_multicast, is_mgt);
763 /* FIXME: 802.11a? */
764 rate = ieee80211softmac_suggest_txrate(softmac, is_multicast, is_mgt);
767 flags |= R2M_SHORT_PREAMBLE;
769 zd_rate = rate_to_zd_rate(rate);
770 cs->modulation = zd_rate_to_modulation(zd_rate, flags);
773 static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
774 struct ieee80211_hdr_4addr *header)
776 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
777 unsigned int tx_length = le16_to_cpu(cs->tx_length);
778 u16 fctl = le16_to_cpu(header->frame_ctl);
779 u16 ftype = WLAN_FC_GET_TYPE(fctl);
780 u16 stype = WLAN_FC_GET_STYPE(fctl);
784 * - if backoff needed, enable bit 0
785 * - if burst (backoff not needed) disable bit 0
791 if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
792 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
795 if (is_multicast_ether_addr(header->addr1))
796 cs->control |= ZD_CS_MULTICAST;
799 if (stype == IEEE80211_STYPE_PSPOLL)
800 cs->control |= ZD_CS_PS_POLL_FRAME;
802 /* Unicast data frames over the threshold should have RTS */
803 if (!is_multicast_ether_addr(header->addr1) &&
804 ftype != IEEE80211_FTYPE_MGMT &&
805 tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
806 cs->control |= ZD_CS_RTS;
808 /* Use CTS-to-self protection if required */
809 if (ZD_CS_TYPE(cs->modulation) == ZD_CS_OFDM &&
810 ieee80211softmac_protection_needed(softmac)) {
811 /* FIXME: avoid sending RTS *and* self-CTS, is that correct? */
812 cs->control &= ~ZD_CS_RTS;
813 cs->control |= ZD_CS_SELF_CTS;
816 /* FIXME: Management frame? */
819 static int fill_ctrlset(struct zd_mac *mac,
820 struct ieee80211_txb *txb,
824 struct sk_buff *skb = txb->fragments[frag_num];
825 struct ieee80211_hdr_4addr *hdr =
826 (struct ieee80211_hdr_4addr *) skb->data;
827 unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
828 unsigned int next_frag_len;
829 unsigned int packet_length;
830 struct zd_ctrlset *cs = (struct zd_ctrlset *)
831 skb_push(skb, sizeof(struct zd_ctrlset));
833 if (frag_num+1 < txb->nr_frags) {
834 next_frag_len = txb->fragments[frag_num+1]->len +
839 ZD_ASSERT(frag_len <= 0xffff);
840 ZD_ASSERT(next_frag_len <= 0xffff);
842 cs_set_modulation(mac, cs, hdr);
844 cs->tx_length = cpu_to_le16(frag_len);
846 cs_set_control(mac, cs, hdr);
848 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
849 ZD_ASSERT(packet_length <= 0xffff);
850 /* ZD1211B: Computing the length difference this way, gives us
851 * flexibility to compute the packet length.
853 cs->packet_length = cpu_to_le16(mac->chip.is_zd1211b ?
854 packet_length - frag_len : packet_length);
858 * - transmit frame length in microseconds
859 * - seems to be derived from frame length
860 * - see Cal_Us_Service() in zdinlinef.h
861 * - if macp->bTxBurstEnable is enabled, then multiply by 4
862 * - bTxBurstEnable is never set in the vendor driver
865 * - "for PLCP configuration"
866 * - always 0 except in some situations at 802.11b 11M
867 * - see line 53 of zdinlinef.h
870 r = zd_calc_tx_length_us(&cs->service, ZD_CS_RATE(cs->modulation),
871 le16_to_cpu(cs->tx_length));
874 cs->current_length = cpu_to_le16(r);
876 if (next_frag_len == 0) {
877 cs->next_frame_length = 0;
879 r = zd_calc_tx_length_us(NULL, ZD_CS_RATE(cs->modulation),
883 cs->next_frame_length = cpu_to_le16(r);
889 static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
893 for (i = 0; i < txb->nr_frags; i++) {
894 struct sk_buff *skb = txb->fragments[i];
896 r = fill_ctrlset(mac, txb, i);
899 r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
904 /* FIXME: shouldn't this be handled by the upper layers? */
905 mac->netdev->trans_start = jiffies;
907 ieee80211_txb_free(txb);
912 struct ieee80211_radiotap_header rt_hdr;
917 } __attribute__((packed));
919 static void fill_rt_header(void *buffer, struct zd_mac *mac,
920 const struct ieee80211_rx_stats *stats,
921 const struct rx_status *status)
923 struct zd_rt_hdr *hdr = buffer;
925 hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
926 hdr->rt_hdr.it_pad = 0;
927 hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
928 hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
929 (1 << IEEE80211_RADIOTAP_CHANNEL) |
930 (1 << IEEE80211_RADIOTAP_RATE));
933 if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
934 hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
936 hdr->rt_rate = stats->rate / 5;
939 hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
940 _zd_chip_get_channel(&mac->chip)));
941 hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
942 ((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
943 ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
946 /* Returns 1 if the data packet is for us and 0 otherwise. */
947 static int is_data_packet_for_us(struct ieee80211_device *ieee,
948 struct ieee80211_hdr_4addr *hdr)
950 struct net_device *netdev = ieee->dev;
951 u16 fc = le16_to_cpu(hdr->frame_ctl);
953 ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
955 switch (ieee->iw_mode) {
957 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
958 memcmp(hdr->addr3, ieee->bssid, ETH_ALEN) != 0)
963 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
964 IEEE80211_FCTL_FROMDS ||
965 memcmp(hdr->addr2, ieee->bssid, ETH_ALEN) != 0)
969 ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
973 return memcmp(hdr->addr1, netdev->dev_addr, ETH_ALEN) == 0 ||
974 (is_multicast_ether_addr(hdr->addr1) &&
975 memcmp(hdr->addr3, netdev->dev_addr, ETH_ALEN) != 0) ||
976 (netdev->flags & IFF_PROMISC);
979 /* Filters received packets. The function returns 1 if the packet should be
980 * forwarded to ieee80211_rx(). If the packet should be ignored the function
981 * returns 0. If an invalid packet is found the function returns -EINVAL.
983 * The function calls ieee80211_rx_mgt() directly.
985 * It has been based on ieee80211_rx_any.
987 static int filter_rx(struct ieee80211_device *ieee,
988 const u8 *buffer, unsigned int length,
989 struct ieee80211_rx_stats *stats)
991 struct ieee80211_hdr_4addr *hdr;
994 if (ieee->iw_mode == IW_MODE_MONITOR)
997 hdr = (struct ieee80211_hdr_4addr *)buffer;
998 fc = le16_to_cpu(hdr->frame_ctl);
999 if ((fc & IEEE80211_FCTL_VERS) != 0)
1002 switch (WLAN_FC_GET_TYPE(fc)) {
1003 case IEEE80211_FTYPE_MGMT:
1004 if (length < sizeof(struct ieee80211_hdr_3addr))
1006 ieee80211_rx_mgt(ieee, hdr, stats);
1008 case IEEE80211_FTYPE_CTL:
1010 case IEEE80211_FTYPE_DATA:
1011 /* Ignore invalid short buffers */
1012 if (length < sizeof(struct ieee80211_hdr_3addr))
1014 return is_data_packet_for_us(ieee, hdr);
1020 static void update_qual_rssi(struct zd_mac *mac,
1021 const u8 *buffer, unsigned int length,
1022 u8 qual_percent, u8 rssi_percent)
1024 unsigned long flags;
1025 struct ieee80211_hdr_3addr *hdr;
1028 hdr = (struct ieee80211_hdr_3addr *)buffer;
1029 if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
1031 if (memcmp(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid, ETH_ALEN) != 0)
1034 spin_lock_irqsave(&mac->lock, flags);
1035 i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
1036 mac->qual_buffer[i] = qual_percent;
1037 mac->rssi_buffer[i] = rssi_percent;
1039 spin_unlock_irqrestore(&mac->lock, flags);
1042 static int fill_rx_stats(struct ieee80211_rx_stats *stats,
1043 const struct rx_status **pstatus,
1045 const u8 *buffer, unsigned int length)
1047 const struct rx_status *status;
1049 *pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
1050 if (status->frame_status & ZD_RX_ERROR) {
1051 /* FIXME: update? */
1054 memset(stats, 0, sizeof(struct ieee80211_rx_stats));
1055 stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
1056 + sizeof(struct rx_status));
1057 /* FIXME: 802.11a */
1058 stats->freq = IEEE80211_24GHZ_BAND;
1059 stats->received_channel = _zd_chip_get_channel(&mac->chip);
1060 stats->rssi = zd_rx_strength_percent(status->signal_strength);
1061 stats->signal = zd_rx_qual_percent(buffer,
1062 length - sizeof(struct rx_status),
1064 stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
1065 stats->rate = zd_rx_rate(buffer, status);
1067 stats->mask |= IEEE80211_STATMASK_RATE;
1072 int zd_mac_rx(struct zd_mac *mac, const u8 *buffer, unsigned int length)
1075 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1076 struct ieee80211_rx_stats stats;
1077 const struct rx_status *status;
1078 struct sk_buff *skb;
1080 if (length < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
1081 IEEE80211_FCS_LEN + sizeof(struct rx_status))
1084 r = fill_rx_stats(&stats, &status, mac, buffer, length);
1088 length -= ZD_PLCP_HEADER_SIZE+IEEE80211_FCS_LEN+
1089 sizeof(struct rx_status);
1090 buffer += ZD_PLCP_HEADER_SIZE;
1092 update_qual_rssi(mac, buffer, length, stats.signal, stats.rssi);
1094 r = filter_rx(ieee, buffer, length, &stats);
1098 skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
1101 if (ieee->iw_mode == IW_MODE_MONITOR)
1102 fill_rt_header(skb_put(skb, sizeof(struct zd_rt_hdr)), mac,
1104 memcpy(skb_put(skb, length), buffer, length);
1106 r = ieee80211_rx(ieee, skb, &stats);
1108 dev_kfree_skb_any(skb);
1112 static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
1115 return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
1118 static void set_security(struct net_device *netdev,
1119 struct ieee80211_security *sec)
1121 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
1122 struct ieee80211_security *secinfo = &ieee->sec;
1125 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
1127 for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
1128 if (sec->flags & (1<<keyidx)) {
1129 secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
1130 secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
1131 memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
1135 if (sec->flags & SEC_ACTIVE_KEY) {
1136 secinfo->active_key = sec->active_key;
1137 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1138 " .active_key = %d\n", sec->active_key);
1140 if (sec->flags & SEC_UNICAST_GROUP) {
1141 secinfo->unicast_uses_group = sec->unicast_uses_group;
1142 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1143 " .unicast_uses_group = %d\n",
1144 sec->unicast_uses_group);
1146 if (sec->flags & SEC_LEVEL) {
1147 secinfo->level = sec->level;
1148 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1149 " .level = %d\n", sec->level);
1151 if (sec->flags & SEC_ENABLED) {
1152 secinfo->enabled = sec->enabled;
1153 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1154 " .enabled = %d\n", sec->enabled);
1156 if (sec->flags & SEC_ENCRYPT) {
1157 secinfo->encrypt = sec->encrypt;
1158 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1159 " .encrypt = %d\n", sec->encrypt);
1161 if (sec->flags & SEC_AUTH_MODE) {
1162 secinfo->auth_mode = sec->auth_mode;
1163 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1164 " .auth_mode = %d\n", sec->auth_mode);
1168 static void ieee_init(struct ieee80211_device *ieee)
1170 ieee->mode = IEEE_B | IEEE_G;
1171 ieee->freq_band = IEEE80211_24GHZ_BAND;
1172 ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
1173 ieee->tx_headroom = sizeof(struct zd_ctrlset);
1174 ieee->set_security = set_security;
1175 ieee->hard_start_xmit = netdev_tx;
1177 /* Software encryption/decryption for now */
1178 ieee->host_build_iv = 0;
1179 ieee->host_encrypt = 1;
1180 ieee->host_decrypt = 1;
1182 /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
1183 * correctly support AUTO */
1184 ieee->iw_mode = IW_MODE_INFRA;
1187 static void softmac_init(struct ieee80211softmac_device *sm)
1189 sm->set_channel = set_channel;
1190 sm->bssinfo_change = bssinfo_change;
1193 struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
1195 struct zd_mac *mac = zd_netdev_mac(ndev);
1196 struct iw_statistics *iw_stats = &mac->iw_stats;
1197 unsigned int i, count, qual_total, rssi_total;
1199 memset(iw_stats, 0, sizeof(struct iw_statistics));
1200 /* We are not setting the status, because ieee->state is not updated
1201 * at all and this driver doesn't track authentication state.
1203 spin_lock_irq(&mac->lock);
1204 count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
1205 mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
1206 qual_total = rssi_total = 0;
1207 for (i = 0; i < count; i++) {
1208 qual_total += mac->qual_buffer[i];
1209 rssi_total += mac->rssi_buffer[i];
1211 spin_unlock_irq(&mac->lock);
1212 iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
1214 iw_stats->qual.qual = qual_total / count;
1215 iw_stats->qual.level = rssi_total / count;
1216 iw_stats->qual.updated |=
1217 IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
1219 iw_stats->qual.updated |=
1220 IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
1222 /* TODO: update counter */
1226 #define LINK_LED_WORK_DELAY HZ
1228 static void link_led_handler(void *p)
1230 struct zd_mac *mac = p;
1231 struct zd_chip *chip = &mac->chip;
1232 struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
1236 spin_lock_irq(&mac->lock);
1237 is_associated = sm->associnfo.associated != 0;
1238 spin_unlock_irq(&mac->lock);
1240 r = zd_chip_control_leds(chip,
1241 is_associated ? LED_ASSOCIATED : LED_SCANNING);
1243 dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1245 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1246 LINK_LED_WORK_DELAY);
1249 static void housekeeping_init(struct zd_mac *mac)
1251 INIT_WORK(&mac->housekeeping.link_led_work, link_led_handler, mac);
1254 static void housekeeping_enable(struct zd_mac *mac)
1256 dev_dbg_f(zd_mac_dev(mac), "\n");
1257 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1261 static void housekeeping_disable(struct zd_mac *mac)
1263 dev_dbg_f(zd_mac_dev(mac), "\n");
1264 cancel_rearming_delayed_workqueue(zd_workqueue,
1265 &mac->housekeeping.link_led_work);
1266 zd_chip_control_leds(&mac->chip, LED_OFF);