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 u32 filter = (ieee->iw_mode == IW_MODE_MONITOR) ? ~0 : STA_RX_FILTER;
160 return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
163 int zd_mac_open(struct net_device *netdev)
165 struct zd_mac *mac = zd_netdev_mac(netdev);
166 struct zd_chip *chip = &mac->chip;
169 tasklet_enable(&mac->rx_tasklet);
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 skb_queue_purge(&mac->rx_queue);
222 tasklet_disable(&mac->rx_tasklet);
223 housekeeping_disable(mac);
224 ieee80211softmac_stop(netdev);
226 /* Ensure no work items are running or queued from this point */
227 cancel_delayed_work(&mac->set_rts_cts_work);
228 cancel_delayed_work(&mac->set_basic_rates_work);
229 flush_workqueue(zd_workqueue);
230 mac->updating_rts_rate = 0;
231 mac->updating_basic_rates = 0;
233 zd_chip_disable_hwint(chip);
234 zd_chip_switch_radio_off(chip);
235 zd_chip_disable_int(chip);
240 int zd_mac_set_mac_address(struct net_device *netdev, void *p)
244 struct sockaddr *addr = p;
245 struct zd_mac *mac = zd_netdev_mac(netdev);
246 struct zd_chip *chip = &mac->chip;
248 if (!is_valid_ether_addr(addr->sa_data))
249 return -EADDRNOTAVAIL;
251 dev_dbg_f(zd_mac_dev(mac),
252 "Setting MAC to " MAC_FMT "\n", MAC_ARG(addr->sa_data));
254 r = zd_write_mac_addr(chip, addr->sa_data);
258 spin_lock_irqsave(&mac->lock, flags);
259 memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
260 spin_unlock_irqrestore(&mac->lock, flags);
265 static void set_multicast_hash_handler(struct work_struct *work)
267 struct zd_mac *mac = container_of(work, struct zd_mac,
268 set_multicast_hash_work);
269 struct zd_mc_hash hash;
271 spin_lock_irq(&mac->lock);
272 hash = mac->multicast_hash;
273 spin_unlock_irq(&mac->lock);
275 zd_chip_set_multicast_hash(&mac->chip, &hash);
278 void zd_mac_set_multicast_list(struct net_device *dev)
280 struct zd_mc_hash hash;
281 struct zd_mac *mac = zd_netdev_mac(dev);
282 struct dev_mc_list *mc;
285 if (dev->flags & (IFF_PROMISC|IFF_ALLMULTI)) {
286 zd_mc_add_all(&hash);
289 for (mc = dev->mc_list; mc; mc = mc->next) {
290 dev_dbg_f(zd_mac_dev(mac), "mc addr " MAC_FMT "\n",
291 MAC_ARG(mc->dmi_addr));
292 zd_mc_add_addr(&hash, mc->dmi_addr);
296 spin_lock_irqsave(&mac->lock, flags);
297 mac->multicast_hash = hash;
298 spin_unlock_irqrestore(&mac->lock, flags);
299 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
302 int zd_mac_set_regdomain(struct zd_mac *mac, u8 regdomain)
307 ZD_ASSERT(!irqs_disabled());
308 spin_lock_irq(&mac->lock);
309 if (regdomain == 0) {
310 regdomain = mac->default_regdomain;
312 if (!zd_regdomain_supported(regdomain)) {
313 spin_unlock_irq(&mac->lock);
316 mac->regdomain = regdomain;
317 channel = mac->requested_channel;
318 spin_unlock_irq(&mac->lock);
320 r = zd_geo_init(zd_mac_to_ieee80211(mac), regdomain);
323 if (!zd_regdomain_supports_channel(regdomain, channel)) {
324 r = reset_channel(mac);
332 u8 zd_mac_get_regdomain(struct zd_mac *mac)
337 spin_lock_irqsave(&mac->lock, flags);
338 regdomain = mac->regdomain;
339 spin_unlock_irqrestore(&mac->lock, flags);
343 /* Fallback to lowest rate, if rate is unknown. */
344 static u8 rate_to_zd_rate(u8 rate)
347 case IEEE80211_CCK_RATE_2MB:
348 return ZD_CCK_RATE_2M;
349 case IEEE80211_CCK_RATE_5MB:
350 return ZD_CCK_RATE_5_5M;
351 case IEEE80211_CCK_RATE_11MB:
352 return ZD_CCK_RATE_11M;
353 case IEEE80211_OFDM_RATE_6MB:
354 return ZD_OFDM_RATE_6M;
355 case IEEE80211_OFDM_RATE_9MB:
356 return ZD_OFDM_RATE_9M;
357 case IEEE80211_OFDM_RATE_12MB:
358 return ZD_OFDM_RATE_12M;
359 case IEEE80211_OFDM_RATE_18MB:
360 return ZD_OFDM_RATE_18M;
361 case IEEE80211_OFDM_RATE_24MB:
362 return ZD_OFDM_RATE_24M;
363 case IEEE80211_OFDM_RATE_36MB:
364 return ZD_OFDM_RATE_36M;
365 case IEEE80211_OFDM_RATE_48MB:
366 return ZD_OFDM_RATE_48M;
367 case IEEE80211_OFDM_RATE_54MB:
368 return ZD_OFDM_RATE_54M;
370 return ZD_CCK_RATE_1M;
373 static u16 rate_to_cr_rate(u8 rate)
376 case IEEE80211_CCK_RATE_2MB:
378 case IEEE80211_CCK_RATE_5MB:
380 case IEEE80211_CCK_RATE_11MB:
382 case IEEE80211_OFDM_RATE_6MB:
384 case IEEE80211_OFDM_RATE_9MB:
386 case IEEE80211_OFDM_RATE_12MB:
388 case IEEE80211_OFDM_RATE_18MB:
390 case IEEE80211_OFDM_RATE_24MB:
392 case IEEE80211_OFDM_RATE_36MB:
394 case IEEE80211_OFDM_RATE_48MB:
396 case IEEE80211_OFDM_RATE_54MB:
402 static void try_enable_tx(struct zd_mac *mac)
406 spin_lock_irqsave(&mac->lock, flags);
407 if (mac->updating_rts_rate == 0 && mac->updating_basic_rates == 0)
408 netif_wake_queue(mac->netdev);
409 spin_unlock_irqrestore(&mac->lock, flags);
412 static void set_rts_cts_work(struct work_struct *work)
415 container_of(work, struct zd_mac, set_rts_cts_work.work);
418 unsigned int short_preamble;
420 mutex_lock(&mac->chip.mutex);
422 spin_lock_irqsave(&mac->lock, flags);
423 mac->updating_rts_rate = 0;
424 rts_rate = mac->rts_rate;
425 short_preamble = mac->short_preamble;
426 spin_unlock_irqrestore(&mac->lock, flags);
428 zd_chip_set_rts_cts_rate_locked(&mac->chip, rts_rate, short_preamble);
429 mutex_unlock(&mac->chip.mutex);
434 static void set_basic_rates_work(struct work_struct *work)
437 container_of(work, struct zd_mac, set_basic_rates_work.work);
441 mutex_lock(&mac->chip.mutex);
443 spin_lock_irqsave(&mac->lock, flags);
444 mac->updating_basic_rates = 0;
445 basic_rates = mac->basic_rates;
446 spin_unlock_irqrestore(&mac->lock, flags);
448 zd_chip_set_basic_rates_locked(&mac->chip, basic_rates);
449 mutex_unlock(&mac->chip.mutex);
454 static void bssinfo_change(struct net_device *netdev, u32 changes)
456 struct zd_mac *mac = zd_netdev_mac(netdev);
457 struct ieee80211softmac_device *softmac = ieee80211_priv(netdev);
458 struct ieee80211softmac_bss_info *bssinfo = &softmac->bssinfo;
459 int need_set_rts_cts = 0;
460 int need_set_rates = 0;
464 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
466 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_SHORT_PREAMBLE) {
467 spin_lock_irqsave(&mac->lock, flags);
468 mac->short_preamble = bssinfo->short_preamble;
469 spin_unlock_irqrestore(&mac->lock, flags);
470 need_set_rts_cts = 1;
473 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_RATES) {
474 /* Set RTS rate to highest available basic rate */
475 u8 hi_rate = ieee80211softmac_highest_supported_rate(softmac,
476 &bssinfo->supported_rates, 1);
477 hi_rate = rate_to_zd_rate(hi_rate);
479 spin_lock_irqsave(&mac->lock, flags);
480 if (hi_rate != mac->rts_rate) {
481 mac->rts_rate = hi_rate;
482 need_set_rts_cts = 1;
484 spin_unlock_irqrestore(&mac->lock, flags);
486 /* Set basic rates */
488 if (bssinfo->supported_rates.count == 0) {
489 /* Allow the device to be flexible */
490 basic_rates = CR_RATES_80211B | CR_RATES_80211G;
495 for (i = 0; i < bssinfo->supported_rates.count; i++) {
496 u16 rate = bssinfo->supported_rates.rates[i];
497 if ((rate & IEEE80211_BASIC_RATE_MASK) == 0)
500 rate &= ~IEEE80211_BASIC_RATE_MASK;
501 basic_rates |= rate_to_cr_rate(rate);
504 spin_lock_irqsave(&mac->lock, flags);
505 mac->basic_rates = basic_rates;
506 spin_unlock_irqrestore(&mac->lock, flags);
509 /* Schedule any changes we made above */
511 spin_lock_irqsave(&mac->lock, flags);
512 if (need_set_rts_cts && !mac->updating_rts_rate) {
513 mac->updating_rts_rate = 1;
514 netif_stop_queue(mac->netdev);
515 queue_delayed_work(zd_workqueue, &mac->set_rts_cts_work, 0);
517 if (need_set_rates && !mac->updating_basic_rates) {
518 mac->updating_basic_rates = 1;
519 netif_stop_queue(mac->netdev);
520 queue_delayed_work(zd_workqueue, &mac->set_basic_rates_work,
523 spin_unlock_irqrestore(&mac->lock, flags);
526 static void set_channel(struct net_device *netdev, u8 channel)
528 struct zd_mac *mac = zd_netdev_mac(netdev);
530 dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
532 zd_chip_set_channel(&mac->chip, channel);
535 int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
537 unsigned long lock_flags;
538 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
540 if (ieee->iw_mode == IW_MODE_INFRA)
543 spin_lock_irqsave(&mac->lock, lock_flags);
544 if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
545 spin_unlock_irqrestore(&mac->lock, lock_flags);
548 mac->requested_channel = channel;
549 spin_unlock_irqrestore(&mac->lock, lock_flags);
550 if (netif_running(mac->netdev))
551 return zd_chip_set_channel(&mac->chip, channel);
556 u8 zd_mac_get_channel(struct zd_mac *mac)
558 u8 channel = zd_chip_get_channel(&mac->chip);
560 dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
564 /* If wrong rate is given, we are falling back to the slowest rate: 1MBit/s */
565 static u8 zd_rate_typed(u8 zd_rate)
567 static const u8 typed_rates[16] = {
568 [ZD_CCK_RATE_1M] = ZD_CS_CCK|ZD_CCK_RATE_1M,
569 [ZD_CCK_RATE_2M] = ZD_CS_CCK|ZD_CCK_RATE_2M,
570 [ZD_CCK_RATE_5_5M] = ZD_CS_CCK|ZD_CCK_RATE_5_5M,
571 [ZD_CCK_RATE_11M] = ZD_CS_CCK|ZD_CCK_RATE_11M,
572 [ZD_OFDM_RATE_6M] = ZD_CS_OFDM|ZD_OFDM_RATE_6M,
573 [ZD_OFDM_RATE_9M] = ZD_CS_OFDM|ZD_OFDM_RATE_9M,
574 [ZD_OFDM_RATE_12M] = ZD_CS_OFDM|ZD_OFDM_RATE_12M,
575 [ZD_OFDM_RATE_18M] = ZD_CS_OFDM|ZD_OFDM_RATE_18M,
576 [ZD_OFDM_RATE_24M] = ZD_CS_OFDM|ZD_OFDM_RATE_24M,
577 [ZD_OFDM_RATE_36M] = ZD_CS_OFDM|ZD_OFDM_RATE_36M,
578 [ZD_OFDM_RATE_48M] = ZD_CS_OFDM|ZD_OFDM_RATE_48M,
579 [ZD_OFDM_RATE_54M] = ZD_CS_OFDM|ZD_OFDM_RATE_54M,
582 ZD_ASSERT(ZD_CS_RATE_MASK == 0x0f);
583 return typed_rates[zd_rate & ZD_CS_RATE_MASK];
586 int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
588 struct ieee80211_device *ieee;
594 mac->netdev->type = ARPHRD_ETHER;
596 case IW_MODE_MONITOR:
597 mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
600 dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
604 ieee = zd_mac_to_ieee80211(mac);
605 ZD_ASSERT(!irqs_disabled());
606 spin_lock_irq(&ieee->lock);
607 ieee->iw_mode = mode;
608 spin_unlock_irq(&ieee->lock);
610 if (netif_running(mac->netdev))
611 return reset_mode(mac);
616 int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
619 struct ieee80211_device *ieee;
621 ieee = zd_mac_to_ieee80211(mac);
622 spin_lock_irqsave(&ieee->lock, flags);
623 *mode = ieee->iw_mode;
624 spin_unlock_irqrestore(&ieee->lock, flags);
628 int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
631 const struct channel_range *channel_range;
634 memset(range, 0, sizeof(*range));
636 /* FIXME: Not so important and depends on the mode. For 802.11g
637 * usually this value is used. It seems to be that Bit/s number is
640 range->throughput = 27 * 1000 * 1000;
642 range->max_qual.qual = 100;
643 range->max_qual.level = 100;
645 /* FIXME: Needs still to be tuned. */
646 range->avg_qual.qual = 71;
647 range->avg_qual.level = 80;
649 /* FIXME: depends on standard? */
650 range->min_rts = 256;
651 range->max_rts = 2346;
653 range->min_frag = MIN_FRAG_THRESHOLD;
654 range->max_frag = MAX_FRAG_THRESHOLD;
656 range->max_encoding_tokens = WEP_KEYS;
657 range->num_encoding_sizes = 2;
658 range->encoding_size[0] = 5;
659 range->encoding_size[1] = WEP_KEY_LEN;
661 range->we_version_compiled = WIRELESS_EXT;
662 range->we_version_source = 20;
664 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
665 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
667 ZD_ASSERT(!irqs_disabled());
668 spin_lock_irq(&mac->lock);
669 regdomain = mac->regdomain;
670 spin_unlock_irq(&mac->lock);
671 channel_range = zd_channel_range(regdomain);
673 range->num_channels = channel_range->end - channel_range->start;
674 range->old_num_channels = range->num_channels;
675 range->num_frequency = range->num_channels;
676 range->old_num_frequency = range->num_frequency;
678 for (i = 0; i < range->num_frequency; i++) {
679 struct iw_freq *freq = &range->freq[i];
680 freq->i = channel_range->start + i;
681 zd_channel_to_freq(freq, freq->i);
687 static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
689 static const u8 rate_divisor[] = {
690 [ZD_CCK_RATE_1M] = 1,
691 [ZD_CCK_RATE_2M] = 2,
692 [ZD_CCK_RATE_5_5M] = 11, /* bits must be doubled */
693 [ZD_CCK_RATE_11M] = 11,
694 [ZD_OFDM_RATE_6M] = 6,
695 [ZD_OFDM_RATE_9M] = 9,
696 [ZD_OFDM_RATE_12M] = 12,
697 [ZD_OFDM_RATE_18M] = 18,
698 [ZD_OFDM_RATE_24M] = 24,
699 [ZD_OFDM_RATE_36M] = 36,
700 [ZD_OFDM_RATE_48M] = 48,
701 [ZD_OFDM_RATE_54M] = 54,
704 u32 bits = (u32)tx_length * 8;
707 divisor = rate_divisor[zd_rate];
712 case ZD_CCK_RATE_5_5M:
713 bits = (2*bits) + 10; /* round up to the next integer */
715 case ZD_CCK_RATE_11M:
718 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
719 if (0 < t && t <= 3) {
720 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
723 bits += 10; /* round up to the next integer */
731 R2M_SHORT_PREAMBLE = 0x01,
735 static u8 zd_rate_to_modulation(u8 zd_rate, int flags)
739 modulation = zd_rate_typed(zd_rate);
740 if (flags & R2M_SHORT_PREAMBLE) {
741 switch (ZD_CS_RATE(modulation)) {
743 case ZD_CCK_RATE_5_5M:
744 case ZD_CCK_RATE_11M:
745 modulation |= ZD_CS_CCK_PREA_SHORT;
749 if (flags & R2M_11A) {
750 if (ZD_CS_TYPE(modulation) == ZD_CS_OFDM)
751 modulation |= ZD_CS_OFDM_MODE_11A;
756 static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
757 struct ieee80211_hdr_4addr *hdr)
759 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
760 u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
762 int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
763 int is_multicast = is_multicast_ether_addr(hdr->addr1);
764 int short_preamble = ieee80211softmac_short_preamble_ok(softmac,
765 is_multicast, is_mgt);
768 /* FIXME: 802.11a? */
769 rate = ieee80211softmac_suggest_txrate(softmac, is_multicast, is_mgt);
772 flags |= R2M_SHORT_PREAMBLE;
774 zd_rate = rate_to_zd_rate(rate);
775 cs->modulation = zd_rate_to_modulation(zd_rate, flags);
778 static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
779 struct ieee80211_hdr_4addr *header)
781 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
782 unsigned int tx_length = le16_to_cpu(cs->tx_length);
783 u16 fctl = le16_to_cpu(header->frame_ctl);
784 u16 ftype = WLAN_FC_GET_TYPE(fctl);
785 u16 stype = WLAN_FC_GET_STYPE(fctl);
789 * - if backoff needed, enable bit 0
790 * - if burst (backoff not needed) disable bit 0
796 if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
797 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
800 if (is_multicast_ether_addr(header->addr1))
801 cs->control |= ZD_CS_MULTICAST;
804 if (stype == IEEE80211_STYPE_PSPOLL)
805 cs->control |= ZD_CS_PS_POLL_FRAME;
807 /* Unicast data frames over the threshold should have RTS */
808 if (!is_multicast_ether_addr(header->addr1) &&
809 ftype != IEEE80211_FTYPE_MGMT &&
810 tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
811 cs->control |= ZD_CS_RTS;
813 /* Use CTS-to-self protection if required */
814 if (ZD_CS_TYPE(cs->modulation) == ZD_CS_OFDM &&
815 ieee80211softmac_protection_needed(softmac)) {
816 /* FIXME: avoid sending RTS *and* self-CTS, is that correct? */
817 cs->control &= ~ZD_CS_RTS;
818 cs->control |= ZD_CS_SELF_CTS;
821 /* FIXME: Management frame? */
824 static int fill_ctrlset(struct zd_mac *mac,
825 struct ieee80211_txb *txb,
829 struct sk_buff *skb = txb->fragments[frag_num];
830 struct ieee80211_hdr_4addr *hdr =
831 (struct ieee80211_hdr_4addr *) skb->data;
832 unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
833 unsigned int next_frag_len;
834 unsigned int packet_length;
835 struct zd_ctrlset *cs = (struct zd_ctrlset *)
836 skb_push(skb, sizeof(struct zd_ctrlset));
838 if (frag_num+1 < txb->nr_frags) {
839 next_frag_len = txb->fragments[frag_num+1]->len +
844 ZD_ASSERT(frag_len <= 0xffff);
845 ZD_ASSERT(next_frag_len <= 0xffff);
847 cs_set_modulation(mac, cs, hdr);
849 cs->tx_length = cpu_to_le16(frag_len);
851 cs_set_control(mac, cs, hdr);
853 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
854 ZD_ASSERT(packet_length <= 0xffff);
855 /* ZD1211B: Computing the length difference this way, gives us
856 * flexibility to compute the packet length.
858 cs->packet_length = cpu_to_le16(mac->chip.is_zd1211b ?
859 packet_length - frag_len : packet_length);
863 * - transmit frame length in microseconds
864 * - seems to be derived from frame length
865 * - see Cal_Us_Service() in zdinlinef.h
866 * - if macp->bTxBurstEnable is enabled, then multiply by 4
867 * - bTxBurstEnable is never set in the vendor driver
870 * - "for PLCP configuration"
871 * - always 0 except in some situations at 802.11b 11M
872 * - see line 53 of zdinlinef.h
875 r = zd_calc_tx_length_us(&cs->service, ZD_CS_RATE(cs->modulation),
876 le16_to_cpu(cs->tx_length));
879 cs->current_length = cpu_to_le16(r);
881 if (next_frag_len == 0) {
882 cs->next_frame_length = 0;
884 r = zd_calc_tx_length_us(NULL, ZD_CS_RATE(cs->modulation),
888 cs->next_frame_length = cpu_to_le16(r);
894 static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
897 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
899 for (i = 0; i < txb->nr_frags; i++) {
900 struct sk_buff *skb = txb->fragments[i];
902 r = fill_ctrlset(mac, txb, i);
904 ieee->stats.tx_dropped++;
907 r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
909 ieee->stats.tx_dropped++;
914 /* FIXME: shouldn't this be handled by the upper layers? */
915 mac->netdev->trans_start = jiffies;
917 ieee80211_txb_free(txb);
922 struct ieee80211_radiotap_header rt_hdr;
927 } __attribute__((packed));
929 static void fill_rt_header(void *buffer, struct zd_mac *mac,
930 const struct ieee80211_rx_stats *stats,
931 const struct rx_status *status)
933 struct zd_rt_hdr *hdr = buffer;
935 hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
936 hdr->rt_hdr.it_pad = 0;
937 hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
938 hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
939 (1 << IEEE80211_RADIOTAP_CHANNEL) |
940 (1 << IEEE80211_RADIOTAP_RATE));
943 if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
944 hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
946 hdr->rt_rate = stats->rate / 5;
949 hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
950 _zd_chip_get_channel(&mac->chip)));
951 hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
952 ((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
953 ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
956 /* Returns 1 if the data packet is for us and 0 otherwise. */
957 static int is_data_packet_for_us(struct ieee80211_device *ieee,
958 struct ieee80211_hdr_4addr *hdr)
960 struct net_device *netdev = ieee->dev;
961 u16 fc = le16_to_cpu(hdr->frame_ctl);
963 ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
965 switch (ieee->iw_mode) {
967 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
968 compare_ether_addr(hdr->addr3, ieee->bssid) != 0)
973 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
974 IEEE80211_FCTL_FROMDS ||
975 compare_ether_addr(hdr->addr2, ieee->bssid) != 0)
979 ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
983 return compare_ether_addr(hdr->addr1, netdev->dev_addr) == 0 ||
984 (is_multicast_ether_addr(hdr->addr1) &&
985 compare_ether_addr(hdr->addr3, netdev->dev_addr) != 0) ||
986 (netdev->flags & IFF_PROMISC);
989 /* Filters received packets. The function returns 1 if the packet should be
990 * forwarded to ieee80211_rx(). If the packet should be ignored the function
991 * returns 0. If an invalid packet is found the function returns -EINVAL.
993 * The function calls ieee80211_rx_mgt() directly.
995 * It has been based on ieee80211_rx_any.
997 static int filter_rx(struct ieee80211_device *ieee,
998 const u8 *buffer, unsigned int length,
999 struct ieee80211_rx_stats *stats)
1001 struct ieee80211_hdr_4addr *hdr;
1004 if (ieee->iw_mode == IW_MODE_MONITOR)
1007 hdr = (struct ieee80211_hdr_4addr *)buffer;
1008 fc = le16_to_cpu(hdr->frame_ctl);
1009 if ((fc & IEEE80211_FCTL_VERS) != 0)
1012 switch (WLAN_FC_GET_TYPE(fc)) {
1013 case IEEE80211_FTYPE_MGMT:
1014 if (length < sizeof(struct ieee80211_hdr_3addr))
1016 ieee80211_rx_mgt(ieee, hdr, stats);
1018 case IEEE80211_FTYPE_CTL:
1020 case IEEE80211_FTYPE_DATA:
1021 /* Ignore invalid short buffers */
1022 if (length < sizeof(struct ieee80211_hdr_3addr))
1024 return is_data_packet_for_us(ieee, hdr);
1030 static void update_qual_rssi(struct zd_mac *mac,
1031 const u8 *buffer, unsigned int length,
1032 u8 qual_percent, u8 rssi_percent)
1034 unsigned long flags;
1035 struct ieee80211_hdr_3addr *hdr;
1038 hdr = (struct ieee80211_hdr_3addr *)buffer;
1039 if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
1041 if (compare_ether_addr(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid) != 0)
1044 spin_lock_irqsave(&mac->lock, flags);
1045 i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
1046 mac->qual_buffer[i] = qual_percent;
1047 mac->rssi_buffer[i] = rssi_percent;
1049 spin_unlock_irqrestore(&mac->lock, flags);
1052 static int fill_rx_stats(struct ieee80211_rx_stats *stats,
1053 const struct rx_status **pstatus,
1055 const u8 *buffer, unsigned int length)
1057 const struct rx_status *status;
1059 *pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
1060 if (status->frame_status & ZD_RX_ERROR) {
1061 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1062 ieee->stats.rx_errors++;
1063 if (status->frame_status & ZD_RX_TIMEOUT_ERROR)
1064 ieee->stats.rx_missed_errors++;
1065 else if (status->frame_status & ZD_RX_FIFO_OVERRUN_ERROR)
1066 ieee->stats.rx_fifo_errors++;
1067 else if (status->frame_status & ZD_RX_DECRYPTION_ERROR)
1068 ieee->ieee_stats.rx_discards_undecryptable++;
1069 else if (status->frame_status & ZD_RX_CRC32_ERROR) {
1070 ieee->stats.rx_crc_errors++;
1071 ieee->ieee_stats.rx_fcs_errors++;
1073 else if (status->frame_status & ZD_RX_CRC16_ERROR)
1074 ieee->stats.rx_crc_errors++;
1078 memset(stats, 0, sizeof(struct ieee80211_rx_stats));
1079 stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
1080 + sizeof(struct rx_status));
1081 /* FIXME: 802.11a */
1082 stats->freq = IEEE80211_24GHZ_BAND;
1083 stats->received_channel = _zd_chip_get_channel(&mac->chip);
1084 stats->rssi = zd_rx_strength_percent(status->signal_strength);
1085 stats->signal = zd_rx_qual_percent(buffer,
1086 length - sizeof(struct rx_status),
1088 stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
1089 stats->rate = zd_rx_rate(buffer, status);
1091 stats->mask |= IEEE80211_STATMASK_RATE;
1096 static void zd_mac_rx(struct zd_mac *mac, struct sk_buff *skb)
1099 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1100 struct ieee80211_rx_stats stats;
1101 const struct rx_status *status;
1103 if (skb->len < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
1104 IEEE80211_FCS_LEN + sizeof(struct rx_status))
1106 ieee->stats.rx_errors++;
1107 ieee->stats.rx_length_errors++;
1111 r = fill_rx_stats(&stats, &status, mac, skb->data, skb->len);
1113 /* Only packets with rx errors are included here.
1114 * The error stats have already been set in fill_rx_stats.
1119 __skb_pull(skb, ZD_PLCP_HEADER_SIZE);
1120 __skb_trim(skb, skb->len -
1121 (IEEE80211_FCS_LEN + sizeof(struct rx_status)));
1123 update_qual_rssi(mac, skb->data, skb->len, stats.signal,
1124 status->signal_strength);
1126 r = filter_rx(ieee, skb->data, skb->len, &stats);
1129 ieee->stats.rx_errors++;
1130 dev_dbg_f(zd_mac_dev(mac), "Error in packet.\n");
1135 if (ieee->iw_mode == IW_MODE_MONITOR)
1136 fill_rt_header(skb_push(skb, sizeof(struct zd_rt_hdr)), mac,
1139 r = ieee80211_rx(ieee, skb, &stats);
1143 /* We are always in a soft irq. */
1147 static void do_rx(unsigned long mac_ptr)
1149 struct zd_mac *mac = (struct zd_mac *)mac_ptr;
1150 struct sk_buff *skb;
1152 while ((skb = skb_dequeue(&mac->rx_queue)) != NULL)
1153 zd_mac_rx(mac, skb);
1156 int zd_mac_rx_irq(struct zd_mac *mac, const u8 *buffer, unsigned int length)
1158 struct sk_buff *skb;
1160 skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
1162 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1163 dev_warn(zd_mac_dev(mac), "Could not allocate skb.\n");
1164 ieee->stats.rx_dropped++;
1167 skb_reserve(skb, sizeof(struct zd_rt_hdr));
1168 memcpy(__skb_put(skb, length), buffer, length);
1169 skb_queue_tail(&mac->rx_queue, skb);
1170 tasklet_schedule(&mac->rx_tasklet);
1174 static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
1177 return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
1180 static void set_security(struct net_device *netdev,
1181 struct ieee80211_security *sec)
1183 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
1184 struct ieee80211_security *secinfo = &ieee->sec;
1187 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
1189 for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
1190 if (sec->flags & (1<<keyidx)) {
1191 secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
1192 secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
1193 memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
1197 if (sec->flags & SEC_ACTIVE_KEY) {
1198 secinfo->active_key = sec->active_key;
1199 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1200 " .active_key = %d\n", sec->active_key);
1202 if (sec->flags & SEC_UNICAST_GROUP) {
1203 secinfo->unicast_uses_group = sec->unicast_uses_group;
1204 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1205 " .unicast_uses_group = %d\n",
1206 sec->unicast_uses_group);
1208 if (sec->flags & SEC_LEVEL) {
1209 secinfo->level = sec->level;
1210 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1211 " .level = %d\n", sec->level);
1213 if (sec->flags & SEC_ENABLED) {
1214 secinfo->enabled = sec->enabled;
1215 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1216 " .enabled = %d\n", sec->enabled);
1218 if (sec->flags & SEC_ENCRYPT) {
1219 secinfo->encrypt = sec->encrypt;
1220 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1221 " .encrypt = %d\n", sec->encrypt);
1223 if (sec->flags & SEC_AUTH_MODE) {
1224 secinfo->auth_mode = sec->auth_mode;
1225 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1226 " .auth_mode = %d\n", sec->auth_mode);
1230 static void ieee_init(struct ieee80211_device *ieee)
1232 ieee->mode = IEEE_B | IEEE_G;
1233 ieee->freq_band = IEEE80211_24GHZ_BAND;
1234 ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
1235 ieee->tx_headroom = sizeof(struct zd_ctrlset);
1236 ieee->set_security = set_security;
1237 ieee->hard_start_xmit = netdev_tx;
1239 /* Software encryption/decryption for now */
1240 ieee->host_build_iv = 0;
1241 ieee->host_encrypt = 1;
1242 ieee->host_decrypt = 1;
1244 /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
1245 * correctly support AUTO */
1246 ieee->iw_mode = IW_MODE_INFRA;
1249 static void softmac_init(struct ieee80211softmac_device *sm)
1251 sm->set_channel = set_channel;
1252 sm->bssinfo_change = bssinfo_change;
1255 struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
1257 struct zd_mac *mac = zd_netdev_mac(ndev);
1258 struct iw_statistics *iw_stats = &mac->iw_stats;
1259 unsigned int i, count, qual_total, rssi_total;
1261 memset(iw_stats, 0, sizeof(struct iw_statistics));
1262 /* We are not setting the status, because ieee->state is not updated
1263 * at all and this driver doesn't track authentication state.
1265 spin_lock_irq(&mac->lock);
1266 count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
1267 mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
1268 qual_total = rssi_total = 0;
1269 for (i = 0; i < count; i++) {
1270 qual_total += mac->qual_buffer[i];
1271 rssi_total += mac->rssi_buffer[i];
1273 spin_unlock_irq(&mac->lock);
1274 iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
1276 iw_stats->qual.qual = qual_total / count;
1277 iw_stats->qual.level = rssi_total / count;
1278 iw_stats->qual.updated |=
1279 IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
1281 iw_stats->qual.updated |=
1282 IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
1284 /* TODO: update counter */
1288 #define LINK_LED_WORK_DELAY HZ
1290 static void link_led_handler(struct work_struct *work)
1292 struct zd_mac *mac =
1293 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
1294 struct zd_chip *chip = &mac->chip;
1295 struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
1299 spin_lock_irq(&mac->lock);
1300 is_associated = sm->associnfo.associated != 0;
1301 spin_unlock_irq(&mac->lock);
1303 r = zd_chip_control_leds(chip,
1304 is_associated ? LED_ASSOCIATED : LED_SCANNING);
1306 dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1308 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1309 LINK_LED_WORK_DELAY);
1312 static void housekeeping_init(struct zd_mac *mac)
1314 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
1317 static void housekeeping_enable(struct zd_mac *mac)
1319 dev_dbg_f(zd_mac_dev(mac), "\n");
1320 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1324 static void housekeeping_disable(struct zd_mac *mac)
1326 dev_dbg_f(zd_mac_dev(mac), "\n");
1327 cancel_rearming_delayed_workqueue(zd_workqueue,
1328 &mac->housekeeping.link_led_work);
1329 zd_chip_control_leds(&mac->chip, LED_OFF);