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_init_hw(struct zd_mac *mac, u8 device_type)
92 struct zd_chip *chip = &mac->chip;
96 r = zd_chip_enable_int(chip);
99 r = zd_chip_init_hw(chip, device_type);
103 zd_get_e2p_mac_addr(chip, addr);
104 r = zd_write_mac_addr(chip, addr);
107 ZD_ASSERT(!irqs_disabled());
108 spin_lock_irq(&mac->lock);
109 memcpy(mac->netdev->dev_addr, addr, ETH_ALEN);
110 spin_unlock_irq(&mac->lock);
112 r = zd_read_regdomain(chip, &default_regdomain);
115 if (!zd_regdomain_supported(default_regdomain)) {
116 dev_dbg_f(zd_mac_dev(mac),
117 "Regulatory Domain %#04x is not supported.\n",
122 spin_lock_irq(&mac->lock);
123 mac->regdomain = mac->default_regdomain = default_regdomain;
124 spin_unlock_irq(&mac->lock);
125 r = reset_channel(mac);
129 /* We must inform the device that we are doing encryption/decryption in
130 * software at the moment. */
131 r = zd_set_encryption_type(chip, ENC_SNIFFER);
135 r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
141 zd_chip_disable_int(chip);
146 void zd_mac_clear(struct zd_mac *mac)
148 flush_workqueue(zd_workqueue);
149 skb_queue_purge(&mac->rx_queue);
150 tasklet_kill(&mac->rx_tasklet);
151 zd_chip_clear(&mac->chip);
152 ZD_ASSERT(!spin_is_locked(&mac->lock));
153 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
156 static int reset_mode(struct zd_mac *mac)
158 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
159 struct zd_ioreq32 ioreqs[3] = {
160 { CR_RX_FILTER, STA_RX_FILTER },
161 { CR_SNIFFER_ON, 0U },
164 if (ieee->iw_mode == IW_MODE_MONITOR) {
165 ioreqs[0].value = 0xffffffff;
166 ioreqs[1].value = 0x1;
167 ioreqs[2].value = ENC_SNIFFER;
170 return zd_iowrite32a(&mac->chip, ioreqs, 3);
173 int zd_mac_open(struct net_device *netdev)
175 struct zd_mac *mac = zd_netdev_mac(netdev);
176 struct zd_chip *chip = &mac->chip;
179 tasklet_enable(&mac->rx_tasklet);
181 r = zd_chip_enable_int(chip);
185 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
191 r = zd_chip_switch_radio_on(chip);
194 r = zd_chip_set_channel(chip, mac->requested_channel);
197 r = zd_chip_enable_rx(chip);
200 r = zd_chip_enable_hwint(chip);
204 housekeeping_enable(mac);
205 ieee80211softmac_start(netdev);
208 zd_chip_disable_rx(chip);
210 zd_chip_switch_radio_off(chip);
212 zd_chip_disable_int(chip);
217 int zd_mac_stop(struct net_device *netdev)
219 struct zd_mac *mac = zd_netdev_mac(netdev);
220 struct zd_chip *chip = &mac->chip;
222 netif_stop_queue(netdev);
225 * The order here deliberately is a little different from the open()
226 * method, since we need to make sure there is no opportunity for RX
227 * frames to be processed by softmac after we have stopped it.
230 zd_chip_disable_rx(chip);
231 skb_queue_purge(&mac->rx_queue);
232 tasklet_disable(&mac->rx_tasklet);
233 housekeeping_disable(mac);
234 ieee80211softmac_stop(netdev);
236 /* Ensure no work items are running or queued from this point */
237 cancel_delayed_work(&mac->set_rts_cts_work);
238 cancel_delayed_work(&mac->set_basic_rates_work);
239 flush_workqueue(zd_workqueue);
240 mac->updating_rts_rate = 0;
241 mac->updating_basic_rates = 0;
243 zd_chip_disable_hwint(chip);
244 zd_chip_switch_radio_off(chip);
245 zd_chip_disable_int(chip);
250 int zd_mac_set_mac_address(struct net_device *netdev, void *p)
254 struct sockaddr *addr = p;
255 struct zd_mac *mac = zd_netdev_mac(netdev);
256 struct zd_chip *chip = &mac->chip;
258 if (!is_valid_ether_addr(addr->sa_data))
259 return -EADDRNOTAVAIL;
261 dev_dbg_f(zd_mac_dev(mac),
262 "Setting MAC to " MAC_FMT "\n", MAC_ARG(addr->sa_data));
264 r = zd_write_mac_addr(chip, addr->sa_data);
268 spin_lock_irqsave(&mac->lock, flags);
269 memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
270 spin_unlock_irqrestore(&mac->lock, flags);
275 static void set_multicast_hash_handler(struct work_struct *work)
277 struct zd_mac *mac = container_of(work, struct zd_mac,
278 set_multicast_hash_work);
279 struct zd_mc_hash hash;
281 spin_lock_irq(&mac->lock);
282 hash = mac->multicast_hash;
283 spin_unlock_irq(&mac->lock);
285 zd_chip_set_multicast_hash(&mac->chip, &hash);
288 void zd_mac_set_multicast_list(struct net_device *dev)
290 struct zd_mc_hash hash;
291 struct zd_mac *mac = zd_netdev_mac(dev);
292 struct dev_mc_list *mc;
295 if (dev->flags & (IFF_PROMISC|IFF_ALLMULTI)) {
296 zd_mc_add_all(&hash);
299 for (mc = dev->mc_list; mc; mc = mc->next) {
300 dev_dbg_f(zd_mac_dev(mac), "mc addr " MAC_FMT "\n",
301 MAC_ARG(mc->dmi_addr));
302 zd_mc_add_addr(&hash, mc->dmi_addr);
306 spin_lock_irqsave(&mac->lock, flags);
307 mac->multicast_hash = hash;
308 spin_unlock_irqrestore(&mac->lock, flags);
309 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
312 int zd_mac_set_regdomain(struct zd_mac *mac, u8 regdomain)
317 ZD_ASSERT(!irqs_disabled());
318 spin_lock_irq(&mac->lock);
319 if (regdomain == 0) {
320 regdomain = mac->default_regdomain;
322 if (!zd_regdomain_supported(regdomain)) {
323 spin_unlock_irq(&mac->lock);
326 mac->regdomain = regdomain;
327 channel = mac->requested_channel;
328 spin_unlock_irq(&mac->lock);
330 r = zd_geo_init(zd_mac_to_ieee80211(mac), regdomain);
333 if (!zd_regdomain_supports_channel(regdomain, channel)) {
334 r = reset_channel(mac);
342 u8 zd_mac_get_regdomain(struct zd_mac *mac)
347 spin_lock_irqsave(&mac->lock, flags);
348 regdomain = mac->regdomain;
349 spin_unlock_irqrestore(&mac->lock, flags);
353 /* Fallback to lowest rate, if rate is unknown. */
354 static u8 rate_to_zd_rate(u8 rate)
357 case IEEE80211_CCK_RATE_2MB:
358 return ZD_CCK_RATE_2M;
359 case IEEE80211_CCK_RATE_5MB:
360 return ZD_CCK_RATE_5_5M;
361 case IEEE80211_CCK_RATE_11MB:
362 return ZD_CCK_RATE_11M;
363 case IEEE80211_OFDM_RATE_6MB:
364 return ZD_OFDM_RATE_6M;
365 case IEEE80211_OFDM_RATE_9MB:
366 return ZD_OFDM_RATE_9M;
367 case IEEE80211_OFDM_RATE_12MB:
368 return ZD_OFDM_RATE_12M;
369 case IEEE80211_OFDM_RATE_18MB:
370 return ZD_OFDM_RATE_18M;
371 case IEEE80211_OFDM_RATE_24MB:
372 return ZD_OFDM_RATE_24M;
373 case IEEE80211_OFDM_RATE_36MB:
374 return ZD_OFDM_RATE_36M;
375 case IEEE80211_OFDM_RATE_48MB:
376 return ZD_OFDM_RATE_48M;
377 case IEEE80211_OFDM_RATE_54MB:
378 return ZD_OFDM_RATE_54M;
380 return ZD_CCK_RATE_1M;
383 static u16 rate_to_cr_rate(u8 rate)
386 case IEEE80211_CCK_RATE_2MB:
388 case IEEE80211_CCK_RATE_5MB:
390 case IEEE80211_CCK_RATE_11MB:
392 case IEEE80211_OFDM_RATE_6MB:
394 case IEEE80211_OFDM_RATE_9MB:
396 case IEEE80211_OFDM_RATE_12MB:
398 case IEEE80211_OFDM_RATE_18MB:
400 case IEEE80211_OFDM_RATE_24MB:
402 case IEEE80211_OFDM_RATE_36MB:
404 case IEEE80211_OFDM_RATE_48MB:
406 case IEEE80211_OFDM_RATE_54MB:
412 static void try_enable_tx(struct zd_mac *mac)
416 spin_lock_irqsave(&mac->lock, flags);
417 if (mac->updating_rts_rate == 0 && mac->updating_basic_rates == 0)
418 netif_wake_queue(mac->netdev);
419 spin_unlock_irqrestore(&mac->lock, flags);
422 static void set_rts_cts_work(struct work_struct *work)
425 container_of(work, struct zd_mac, set_rts_cts_work.work);
428 unsigned int short_preamble;
430 mutex_lock(&mac->chip.mutex);
432 spin_lock_irqsave(&mac->lock, flags);
433 mac->updating_rts_rate = 0;
434 rts_rate = mac->rts_rate;
435 short_preamble = mac->short_preamble;
436 spin_unlock_irqrestore(&mac->lock, flags);
438 zd_chip_set_rts_cts_rate_locked(&mac->chip, rts_rate, short_preamble);
439 mutex_unlock(&mac->chip.mutex);
444 static void set_basic_rates_work(struct work_struct *work)
447 container_of(work, struct zd_mac, set_basic_rates_work.work);
451 mutex_lock(&mac->chip.mutex);
453 spin_lock_irqsave(&mac->lock, flags);
454 mac->updating_basic_rates = 0;
455 basic_rates = mac->basic_rates;
456 spin_unlock_irqrestore(&mac->lock, flags);
458 zd_chip_set_basic_rates_locked(&mac->chip, basic_rates);
459 mutex_unlock(&mac->chip.mutex);
464 static void bssinfo_change(struct net_device *netdev, u32 changes)
466 struct zd_mac *mac = zd_netdev_mac(netdev);
467 struct ieee80211softmac_device *softmac = ieee80211_priv(netdev);
468 struct ieee80211softmac_bss_info *bssinfo = &softmac->bssinfo;
469 int need_set_rts_cts = 0;
470 int need_set_rates = 0;
474 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
476 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_SHORT_PREAMBLE) {
477 spin_lock_irqsave(&mac->lock, flags);
478 mac->short_preamble = bssinfo->short_preamble;
479 spin_unlock_irqrestore(&mac->lock, flags);
480 need_set_rts_cts = 1;
483 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_RATES) {
484 /* Set RTS rate to highest available basic rate */
485 u8 hi_rate = ieee80211softmac_highest_supported_rate(softmac,
486 &bssinfo->supported_rates, 1);
487 hi_rate = rate_to_zd_rate(hi_rate);
489 spin_lock_irqsave(&mac->lock, flags);
490 if (hi_rate != mac->rts_rate) {
491 mac->rts_rate = hi_rate;
492 need_set_rts_cts = 1;
494 spin_unlock_irqrestore(&mac->lock, flags);
496 /* Set basic rates */
498 if (bssinfo->supported_rates.count == 0) {
499 /* Allow the device to be flexible */
500 basic_rates = CR_RATES_80211B | CR_RATES_80211G;
505 for (i = 0; i < bssinfo->supported_rates.count; i++) {
506 u16 rate = bssinfo->supported_rates.rates[i];
507 if ((rate & IEEE80211_BASIC_RATE_MASK) == 0)
510 rate &= ~IEEE80211_BASIC_RATE_MASK;
511 basic_rates |= rate_to_cr_rate(rate);
514 spin_lock_irqsave(&mac->lock, flags);
515 mac->basic_rates = basic_rates;
516 spin_unlock_irqrestore(&mac->lock, flags);
519 /* Schedule any changes we made above */
521 spin_lock_irqsave(&mac->lock, flags);
522 if (need_set_rts_cts && !mac->updating_rts_rate) {
523 mac->updating_rts_rate = 1;
524 netif_stop_queue(mac->netdev);
525 queue_delayed_work(zd_workqueue, &mac->set_rts_cts_work, 0);
527 if (need_set_rates && !mac->updating_basic_rates) {
528 mac->updating_basic_rates = 1;
529 netif_stop_queue(mac->netdev);
530 queue_delayed_work(zd_workqueue, &mac->set_basic_rates_work,
533 spin_unlock_irqrestore(&mac->lock, flags);
536 static void set_channel(struct net_device *netdev, u8 channel)
538 struct zd_mac *mac = zd_netdev_mac(netdev);
540 dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
542 zd_chip_set_channel(&mac->chip, channel);
545 int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
547 unsigned long lock_flags;
548 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
550 if (ieee->iw_mode == IW_MODE_INFRA)
553 spin_lock_irqsave(&mac->lock, lock_flags);
554 if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
555 spin_unlock_irqrestore(&mac->lock, lock_flags);
558 mac->requested_channel = channel;
559 spin_unlock_irqrestore(&mac->lock, lock_flags);
560 if (netif_running(mac->netdev))
561 return zd_chip_set_channel(&mac->chip, channel);
566 u8 zd_mac_get_channel(struct zd_mac *mac)
568 u8 channel = zd_chip_get_channel(&mac->chip);
570 dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
574 /* If wrong rate is given, we are falling back to the slowest rate: 1MBit/s */
575 static u8 zd_rate_typed(u8 zd_rate)
577 static const u8 typed_rates[16] = {
578 [ZD_CCK_RATE_1M] = ZD_CS_CCK|ZD_CCK_RATE_1M,
579 [ZD_CCK_RATE_2M] = ZD_CS_CCK|ZD_CCK_RATE_2M,
580 [ZD_CCK_RATE_5_5M] = ZD_CS_CCK|ZD_CCK_RATE_5_5M,
581 [ZD_CCK_RATE_11M] = ZD_CS_CCK|ZD_CCK_RATE_11M,
582 [ZD_OFDM_RATE_6M] = ZD_CS_OFDM|ZD_OFDM_RATE_6M,
583 [ZD_OFDM_RATE_9M] = ZD_CS_OFDM|ZD_OFDM_RATE_9M,
584 [ZD_OFDM_RATE_12M] = ZD_CS_OFDM|ZD_OFDM_RATE_12M,
585 [ZD_OFDM_RATE_18M] = ZD_CS_OFDM|ZD_OFDM_RATE_18M,
586 [ZD_OFDM_RATE_24M] = ZD_CS_OFDM|ZD_OFDM_RATE_24M,
587 [ZD_OFDM_RATE_36M] = ZD_CS_OFDM|ZD_OFDM_RATE_36M,
588 [ZD_OFDM_RATE_48M] = ZD_CS_OFDM|ZD_OFDM_RATE_48M,
589 [ZD_OFDM_RATE_54M] = ZD_CS_OFDM|ZD_OFDM_RATE_54M,
592 ZD_ASSERT(ZD_CS_RATE_MASK == 0x0f);
593 return typed_rates[zd_rate & ZD_CS_RATE_MASK];
596 int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
598 struct ieee80211_device *ieee;
604 mac->netdev->type = ARPHRD_ETHER;
606 case IW_MODE_MONITOR:
607 mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
610 dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
614 ieee = zd_mac_to_ieee80211(mac);
615 ZD_ASSERT(!irqs_disabled());
616 spin_lock_irq(&ieee->lock);
617 ieee->iw_mode = mode;
618 spin_unlock_irq(&ieee->lock);
620 if (netif_running(mac->netdev))
621 return reset_mode(mac);
626 int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
629 struct ieee80211_device *ieee;
631 ieee = zd_mac_to_ieee80211(mac);
632 spin_lock_irqsave(&ieee->lock, flags);
633 *mode = ieee->iw_mode;
634 spin_unlock_irqrestore(&ieee->lock, flags);
638 int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
641 const struct channel_range *channel_range;
644 memset(range, 0, sizeof(*range));
646 /* FIXME: Not so important and depends on the mode. For 802.11g
647 * usually this value is used. It seems to be that Bit/s number is
650 range->throughput = 27 * 1000 * 1000;
652 range->max_qual.qual = 100;
653 range->max_qual.level = 100;
655 /* FIXME: Needs still to be tuned. */
656 range->avg_qual.qual = 71;
657 range->avg_qual.level = 80;
659 /* FIXME: depends on standard? */
660 range->min_rts = 256;
661 range->max_rts = 2346;
663 range->min_frag = MIN_FRAG_THRESHOLD;
664 range->max_frag = MAX_FRAG_THRESHOLD;
666 range->max_encoding_tokens = WEP_KEYS;
667 range->num_encoding_sizes = 2;
668 range->encoding_size[0] = 5;
669 range->encoding_size[1] = WEP_KEY_LEN;
671 range->we_version_compiled = WIRELESS_EXT;
672 range->we_version_source = 20;
674 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
675 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
677 ZD_ASSERT(!irqs_disabled());
678 spin_lock_irq(&mac->lock);
679 regdomain = mac->regdomain;
680 spin_unlock_irq(&mac->lock);
681 channel_range = zd_channel_range(regdomain);
683 range->num_channels = channel_range->end - channel_range->start;
684 range->old_num_channels = range->num_channels;
685 range->num_frequency = range->num_channels;
686 range->old_num_frequency = range->num_frequency;
688 for (i = 0; i < range->num_frequency; i++) {
689 struct iw_freq *freq = &range->freq[i];
690 freq->i = channel_range->start + i;
691 zd_channel_to_freq(freq, freq->i);
697 static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
699 static const u8 rate_divisor[] = {
700 [ZD_CCK_RATE_1M] = 1,
701 [ZD_CCK_RATE_2M] = 2,
702 [ZD_CCK_RATE_5_5M] = 11, /* bits must be doubled */
703 [ZD_CCK_RATE_11M] = 11,
704 [ZD_OFDM_RATE_6M] = 6,
705 [ZD_OFDM_RATE_9M] = 9,
706 [ZD_OFDM_RATE_12M] = 12,
707 [ZD_OFDM_RATE_18M] = 18,
708 [ZD_OFDM_RATE_24M] = 24,
709 [ZD_OFDM_RATE_36M] = 36,
710 [ZD_OFDM_RATE_48M] = 48,
711 [ZD_OFDM_RATE_54M] = 54,
714 u32 bits = (u32)tx_length * 8;
717 divisor = rate_divisor[zd_rate];
722 case ZD_CCK_RATE_5_5M:
723 bits = (2*bits) + 10; /* round up to the next integer */
725 case ZD_CCK_RATE_11M:
728 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
729 if (0 < t && t <= 3) {
730 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
733 bits += 10; /* round up to the next integer */
741 R2M_SHORT_PREAMBLE = 0x01,
745 static u8 zd_rate_to_modulation(u8 zd_rate, int flags)
749 modulation = zd_rate_typed(zd_rate);
750 if (flags & R2M_SHORT_PREAMBLE) {
751 switch (ZD_CS_RATE(modulation)) {
753 case ZD_CCK_RATE_5_5M:
754 case ZD_CCK_RATE_11M:
755 modulation |= ZD_CS_CCK_PREA_SHORT;
759 if (flags & R2M_11A) {
760 if (ZD_CS_TYPE(modulation) == ZD_CS_OFDM)
761 modulation |= ZD_CS_OFDM_MODE_11A;
766 static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
767 struct ieee80211_hdr_4addr *hdr)
769 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
770 u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
772 int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
773 int is_multicast = is_multicast_ether_addr(hdr->addr1);
774 int short_preamble = ieee80211softmac_short_preamble_ok(softmac,
775 is_multicast, is_mgt);
778 /* FIXME: 802.11a? */
779 rate = ieee80211softmac_suggest_txrate(softmac, is_multicast, is_mgt);
782 flags |= R2M_SHORT_PREAMBLE;
784 zd_rate = rate_to_zd_rate(rate);
785 cs->modulation = zd_rate_to_modulation(zd_rate, flags);
788 static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
789 struct ieee80211_hdr_4addr *header)
791 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
792 unsigned int tx_length = le16_to_cpu(cs->tx_length);
793 u16 fctl = le16_to_cpu(header->frame_ctl);
794 u16 ftype = WLAN_FC_GET_TYPE(fctl);
795 u16 stype = WLAN_FC_GET_STYPE(fctl);
799 * - if backoff needed, enable bit 0
800 * - if burst (backoff not needed) disable bit 0
806 if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
807 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
810 if (is_multicast_ether_addr(header->addr1))
811 cs->control |= ZD_CS_MULTICAST;
814 if (stype == IEEE80211_STYPE_PSPOLL)
815 cs->control |= ZD_CS_PS_POLL_FRAME;
817 /* Unicast data frames over the threshold should have RTS */
818 if (!is_multicast_ether_addr(header->addr1) &&
819 ftype != IEEE80211_FTYPE_MGMT &&
820 tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
821 cs->control |= ZD_CS_RTS;
823 /* Use CTS-to-self protection if required */
824 if (ZD_CS_TYPE(cs->modulation) == ZD_CS_OFDM &&
825 ieee80211softmac_protection_needed(softmac)) {
826 /* FIXME: avoid sending RTS *and* self-CTS, is that correct? */
827 cs->control &= ~ZD_CS_RTS;
828 cs->control |= ZD_CS_SELF_CTS;
831 /* FIXME: Management frame? */
834 static int fill_ctrlset(struct zd_mac *mac,
835 struct ieee80211_txb *txb,
839 struct sk_buff *skb = txb->fragments[frag_num];
840 struct ieee80211_hdr_4addr *hdr =
841 (struct ieee80211_hdr_4addr *) skb->data;
842 unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
843 unsigned int next_frag_len;
844 unsigned int packet_length;
845 struct zd_ctrlset *cs = (struct zd_ctrlset *)
846 skb_push(skb, sizeof(struct zd_ctrlset));
848 if (frag_num+1 < txb->nr_frags) {
849 next_frag_len = txb->fragments[frag_num+1]->len +
854 ZD_ASSERT(frag_len <= 0xffff);
855 ZD_ASSERT(next_frag_len <= 0xffff);
857 cs_set_modulation(mac, cs, hdr);
859 cs->tx_length = cpu_to_le16(frag_len);
861 cs_set_control(mac, cs, hdr);
863 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
864 ZD_ASSERT(packet_length <= 0xffff);
865 /* ZD1211B: Computing the length difference this way, gives us
866 * flexibility to compute the packet length.
868 cs->packet_length = cpu_to_le16(mac->chip.is_zd1211b ?
869 packet_length - frag_len : packet_length);
873 * - transmit frame length in microseconds
874 * - seems to be derived from frame length
875 * - see Cal_Us_Service() in zdinlinef.h
876 * - if macp->bTxBurstEnable is enabled, then multiply by 4
877 * - bTxBurstEnable is never set in the vendor driver
880 * - "for PLCP configuration"
881 * - always 0 except in some situations at 802.11b 11M
882 * - see line 53 of zdinlinef.h
885 r = zd_calc_tx_length_us(&cs->service, ZD_CS_RATE(cs->modulation),
886 le16_to_cpu(cs->tx_length));
889 cs->current_length = cpu_to_le16(r);
891 if (next_frag_len == 0) {
892 cs->next_frame_length = 0;
894 r = zd_calc_tx_length_us(NULL, ZD_CS_RATE(cs->modulation),
898 cs->next_frame_length = cpu_to_le16(r);
904 static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
908 for (i = 0; i < txb->nr_frags; i++) {
909 struct sk_buff *skb = txb->fragments[i];
911 r = fill_ctrlset(mac, txb, i);
914 r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
919 /* FIXME: shouldn't this be handled by the upper layers? */
920 mac->netdev->trans_start = jiffies;
922 ieee80211_txb_free(txb);
927 struct ieee80211_radiotap_header rt_hdr;
932 } __attribute__((packed));
934 static void fill_rt_header(void *buffer, struct zd_mac *mac,
935 const struct ieee80211_rx_stats *stats,
936 const struct rx_status *status)
938 struct zd_rt_hdr *hdr = buffer;
940 hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
941 hdr->rt_hdr.it_pad = 0;
942 hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
943 hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
944 (1 << IEEE80211_RADIOTAP_CHANNEL) |
945 (1 << IEEE80211_RADIOTAP_RATE));
948 if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
949 hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
951 hdr->rt_rate = stats->rate / 5;
954 hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
955 _zd_chip_get_channel(&mac->chip)));
956 hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
957 ((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
958 ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
961 /* Returns 1 if the data packet is for us and 0 otherwise. */
962 static int is_data_packet_for_us(struct ieee80211_device *ieee,
963 struct ieee80211_hdr_4addr *hdr)
965 struct net_device *netdev = ieee->dev;
966 u16 fc = le16_to_cpu(hdr->frame_ctl);
968 ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
970 switch (ieee->iw_mode) {
972 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
973 memcmp(hdr->addr3, ieee->bssid, ETH_ALEN) != 0)
978 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
979 IEEE80211_FCTL_FROMDS ||
980 memcmp(hdr->addr2, ieee->bssid, ETH_ALEN) != 0)
984 ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
988 return memcmp(hdr->addr1, netdev->dev_addr, ETH_ALEN) == 0 ||
989 (is_multicast_ether_addr(hdr->addr1) &&
990 memcmp(hdr->addr3, netdev->dev_addr, ETH_ALEN) != 0) ||
991 (netdev->flags & IFF_PROMISC);
994 /* Filters received packets. The function returns 1 if the packet should be
995 * forwarded to ieee80211_rx(). If the packet should be ignored the function
996 * returns 0. If an invalid packet is found the function returns -EINVAL.
998 * The function calls ieee80211_rx_mgt() directly.
1000 * It has been based on ieee80211_rx_any.
1002 static int filter_rx(struct ieee80211_device *ieee,
1003 const u8 *buffer, unsigned int length,
1004 struct ieee80211_rx_stats *stats)
1006 struct ieee80211_hdr_4addr *hdr;
1009 if (ieee->iw_mode == IW_MODE_MONITOR)
1012 hdr = (struct ieee80211_hdr_4addr *)buffer;
1013 fc = le16_to_cpu(hdr->frame_ctl);
1014 if ((fc & IEEE80211_FCTL_VERS) != 0)
1017 switch (WLAN_FC_GET_TYPE(fc)) {
1018 case IEEE80211_FTYPE_MGMT:
1019 if (length < sizeof(struct ieee80211_hdr_3addr))
1021 ieee80211_rx_mgt(ieee, hdr, stats);
1023 case IEEE80211_FTYPE_CTL:
1025 case IEEE80211_FTYPE_DATA:
1026 /* Ignore invalid short buffers */
1027 if (length < sizeof(struct ieee80211_hdr_3addr))
1029 return is_data_packet_for_us(ieee, hdr);
1035 static void update_qual_rssi(struct zd_mac *mac,
1036 const u8 *buffer, unsigned int length,
1037 u8 qual_percent, u8 rssi_percent)
1039 unsigned long flags;
1040 struct ieee80211_hdr_3addr *hdr;
1043 hdr = (struct ieee80211_hdr_3addr *)buffer;
1044 if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
1046 if (memcmp(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid, ETH_ALEN) != 0)
1049 spin_lock_irqsave(&mac->lock, flags);
1050 i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
1051 mac->qual_buffer[i] = qual_percent;
1052 mac->rssi_buffer[i] = rssi_percent;
1054 spin_unlock_irqrestore(&mac->lock, flags);
1057 static int fill_rx_stats(struct ieee80211_rx_stats *stats,
1058 const struct rx_status **pstatus,
1060 const u8 *buffer, unsigned int length)
1062 const struct rx_status *status;
1064 *pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
1065 if (status->frame_status & ZD_RX_ERROR) {
1066 /* FIXME: update? */
1069 memset(stats, 0, sizeof(struct ieee80211_rx_stats));
1070 stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
1071 + sizeof(struct rx_status));
1072 /* FIXME: 802.11a */
1073 stats->freq = IEEE80211_24GHZ_BAND;
1074 stats->received_channel = _zd_chip_get_channel(&mac->chip);
1075 stats->rssi = zd_rx_strength_percent(status->signal_strength);
1076 stats->signal = zd_rx_qual_percent(buffer,
1077 length - sizeof(struct rx_status),
1079 stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
1080 stats->rate = zd_rx_rate(buffer, status);
1082 stats->mask |= IEEE80211_STATMASK_RATE;
1087 static void zd_mac_rx(struct zd_mac *mac, struct sk_buff *skb)
1090 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1091 struct ieee80211_rx_stats stats;
1092 const struct rx_status *status;
1094 if (skb->len < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
1095 IEEE80211_FCS_LEN + sizeof(struct rx_status))
1097 dev_dbg_f(zd_mac_dev(mac), "Packet with length %u to small.\n",
1102 r = fill_rx_stats(&stats, &status, mac, skb->data, skb->len);
1104 /* Only packets with rx errors are included here. */
1108 __skb_pull(skb, ZD_PLCP_HEADER_SIZE);
1109 __skb_trim(skb, skb->len -
1110 (IEEE80211_FCS_LEN + sizeof(struct rx_status)));
1112 update_qual_rssi(mac, skb->data, skb->len, stats.signal,
1113 status->signal_strength);
1115 r = filter_rx(ieee, skb->data, skb->len, &stats);
1118 dev_dbg_f(zd_mac_dev(mac), "Error in packet.\n");
1122 if (ieee->iw_mode == IW_MODE_MONITOR)
1123 fill_rt_header(skb_push(skb, sizeof(struct zd_rt_hdr)), mac,
1126 r = ieee80211_rx(ieee, skb, &stats);
1130 /* We are always in a soft irq. */
1134 static void do_rx(unsigned long mac_ptr)
1136 struct zd_mac *mac = (struct zd_mac *)mac_ptr;
1137 struct sk_buff *skb;
1139 while ((skb = skb_dequeue(&mac->rx_queue)) != NULL)
1140 zd_mac_rx(mac, skb);
1143 int zd_mac_rx_irq(struct zd_mac *mac, const u8 *buffer, unsigned int length)
1145 struct sk_buff *skb;
1147 skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
1149 dev_warn(zd_mac_dev(mac), "Could not allocate skb.\n");
1152 skb_reserve(skb, sizeof(struct zd_rt_hdr));
1153 memcpy(__skb_put(skb, length), buffer, length);
1154 skb_queue_tail(&mac->rx_queue, skb);
1155 tasklet_schedule(&mac->rx_tasklet);
1159 static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
1162 return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
1165 static void set_security(struct net_device *netdev,
1166 struct ieee80211_security *sec)
1168 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
1169 struct ieee80211_security *secinfo = &ieee->sec;
1172 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
1174 for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
1175 if (sec->flags & (1<<keyidx)) {
1176 secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
1177 secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
1178 memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
1182 if (sec->flags & SEC_ACTIVE_KEY) {
1183 secinfo->active_key = sec->active_key;
1184 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1185 " .active_key = %d\n", sec->active_key);
1187 if (sec->flags & SEC_UNICAST_GROUP) {
1188 secinfo->unicast_uses_group = sec->unicast_uses_group;
1189 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1190 " .unicast_uses_group = %d\n",
1191 sec->unicast_uses_group);
1193 if (sec->flags & SEC_LEVEL) {
1194 secinfo->level = sec->level;
1195 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1196 " .level = %d\n", sec->level);
1198 if (sec->flags & SEC_ENABLED) {
1199 secinfo->enabled = sec->enabled;
1200 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1201 " .enabled = %d\n", sec->enabled);
1203 if (sec->flags & SEC_ENCRYPT) {
1204 secinfo->encrypt = sec->encrypt;
1205 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1206 " .encrypt = %d\n", sec->encrypt);
1208 if (sec->flags & SEC_AUTH_MODE) {
1209 secinfo->auth_mode = sec->auth_mode;
1210 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1211 " .auth_mode = %d\n", sec->auth_mode);
1215 static void ieee_init(struct ieee80211_device *ieee)
1217 ieee->mode = IEEE_B | IEEE_G;
1218 ieee->freq_band = IEEE80211_24GHZ_BAND;
1219 ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
1220 ieee->tx_headroom = sizeof(struct zd_ctrlset);
1221 ieee->set_security = set_security;
1222 ieee->hard_start_xmit = netdev_tx;
1224 /* Software encryption/decryption for now */
1225 ieee->host_build_iv = 0;
1226 ieee->host_encrypt = 1;
1227 ieee->host_decrypt = 1;
1229 /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
1230 * correctly support AUTO */
1231 ieee->iw_mode = IW_MODE_INFRA;
1234 static void softmac_init(struct ieee80211softmac_device *sm)
1236 sm->set_channel = set_channel;
1237 sm->bssinfo_change = bssinfo_change;
1240 struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
1242 struct zd_mac *mac = zd_netdev_mac(ndev);
1243 struct iw_statistics *iw_stats = &mac->iw_stats;
1244 unsigned int i, count, qual_total, rssi_total;
1246 memset(iw_stats, 0, sizeof(struct iw_statistics));
1247 /* We are not setting the status, because ieee->state is not updated
1248 * at all and this driver doesn't track authentication state.
1250 spin_lock_irq(&mac->lock);
1251 count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
1252 mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
1253 qual_total = rssi_total = 0;
1254 for (i = 0; i < count; i++) {
1255 qual_total += mac->qual_buffer[i];
1256 rssi_total += mac->rssi_buffer[i];
1258 spin_unlock_irq(&mac->lock);
1259 iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
1261 iw_stats->qual.qual = qual_total / count;
1262 iw_stats->qual.level = rssi_total / count;
1263 iw_stats->qual.updated |=
1264 IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
1266 iw_stats->qual.updated |=
1267 IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
1269 /* TODO: update counter */
1273 #define LINK_LED_WORK_DELAY HZ
1275 static void link_led_handler(struct work_struct *work)
1277 struct zd_mac *mac =
1278 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
1279 struct zd_chip *chip = &mac->chip;
1280 struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
1284 spin_lock_irq(&mac->lock);
1285 is_associated = sm->associnfo.associated != 0;
1286 spin_unlock_irq(&mac->lock);
1288 r = zd_chip_control_leds(chip,
1289 is_associated ? LED_ASSOCIATED : LED_SCANNING);
1291 dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1293 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1294 LINK_LED_WORK_DELAY);
1297 static void housekeeping_init(struct zd_mac *mac)
1299 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
1302 static void housekeeping_enable(struct zd_mac *mac)
1304 dev_dbg_f(zd_mac_dev(mac), "\n");
1305 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1309 static void housekeeping_disable(struct zd_mac *mac)
1311 dev_dbg_f(zd_mac_dev(mac), "\n");
1312 cancel_rearming_delayed_workqueue(zd_workqueue,
1313 &mac->housekeeping.link_led_work);
1314 zd_chip_control_leds(&mac->chip, LED_OFF);