1 /* ZD1211 USB-WLAN driver for Linux
3 * Copyright (C) 2005-2007 Ulrich Kunitz <kune@deine-taler.de>
4 * Copyright (C) 2006-2007 Daniel Drake <dsd@gentoo.org>
5 * Copyright (C) 2006-2007 Michael Wu <flamingice@sourmilk.net>
6 * Copyright (C) 2007-2008 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/netdevice.h>
24 #include <linux/etherdevice.h>
25 #include <linux/slab.h>
26 #include <linux/usb.h>
27 #include <linux/jiffies.h>
28 #include <net/ieee80211_radiotap.h>
35 struct zd_reg_alpha2_map {
40 static struct zd_reg_alpha2_map reg_alpha2_map[] = {
41 { ZD_REGDOMAIN_FCC, "US" },
42 { ZD_REGDOMAIN_IC, "CA" },
43 { ZD_REGDOMAIN_ETSI, "DE" }, /* Generic ETSI, use most restrictive */
44 { ZD_REGDOMAIN_JAPAN, "JP" },
45 { ZD_REGDOMAIN_JAPAN_2, "JP" },
46 { ZD_REGDOMAIN_JAPAN_3, "JP" },
47 { ZD_REGDOMAIN_SPAIN, "ES" },
48 { ZD_REGDOMAIN_FRANCE, "FR" },
51 /* This table contains the hardware specific values for the modulation rates. */
52 static const struct ieee80211_rate zd_rates[] = {
54 .hw_value = ZD_CCK_RATE_1M, },
56 .hw_value = ZD_CCK_RATE_2M,
57 .hw_value_short = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
58 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
60 .hw_value = ZD_CCK_RATE_5_5M,
61 .hw_value_short = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
62 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
64 .hw_value = ZD_CCK_RATE_11M,
65 .hw_value_short = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
66 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
68 .hw_value = ZD_OFDM_RATE_6M,
71 .hw_value = ZD_OFDM_RATE_9M,
74 .hw_value = ZD_OFDM_RATE_12M,
77 .hw_value = ZD_OFDM_RATE_18M,
80 .hw_value = ZD_OFDM_RATE_24M,
83 .hw_value = ZD_OFDM_RATE_36M,
86 .hw_value = ZD_OFDM_RATE_48M,
89 .hw_value = ZD_OFDM_RATE_54M,
94 * Zydas retry rates table. Each line is listed in the same order as
95 * in zd_rates[] and contains all the rate used when a packet is sent
96 * starting with a given rates. Let's consider an example :
98 * "11 Mbits : 4, 3, 2, 1, 0" means :
99 * - packet is sent using 4 different rates
100 * - 1st rate is index 3 (ie 11 Mbits)
101 * - 2nd rate is index 2 (ie 5.5 Mbits)
102 * - 3rd rate is index 1 (ie 2 Mbits)
103 * - 4th rate is index 0 (ie 1 Mbits)
106 static const struct tx_retry_rate zd_retry_rates[] = {
107 { /* 1 Mbits */ 1, { 0 }},
108 { /* 2 Mbits */ 2, { 1, 0 }},
109 { /* 5.5 Mbits */ 3, { 2, 1, 0 }},
110 { /* 11 Mbits */ 4, { 3, 2, 1, 0 }},
111 { /* 6 Mbits */ 5, { 4, 3, 2, 1, 0 }},
112 { /* 9 Mbits */ 6, { 5, 4, 3, 2, 1, 0}},
113 { /* 12 Mbits */ 5, { 6, 3, 2, 1, 0 }},
114 { /* 18 Mbits */ 6, { 7, 6, 3, 2, 1, 0 }},
115 { /* 24 Mbits */ 6, { 8, 6, 3, 2, 1, 0 }},
116 { /* 36 Mbits */ 7, { 9, 8, 6, 3, 2, 1, 0 }},
117 { /* 48 Mbits */ 8, {10, 9, 8, 6, 3, 2, 1, 0 }},
118 { /* 54 Mbits */ 9, {11, 10, 9, 8, 6, 3, 2, 1, 0 }}
121 static const struct ieee80211_channel zd_channels[] = {
122 { .center_freq = 2412, .hw_value = 1 },
123 { .center_freq = 2417, .hw_value = 2 },
124 { .center_freq = 2422, .hw_value = 3 },
125 { .center_freq = 2427, .hw_value = 4 },
126 { .center_freq = 2432, .hw_value = 5 },
127 { .center_freq = 2437, .hw_value = 6 },
128 { .center_freq = 2442, .hw_value = 7 },
129 { .center_freq = 2447, .hw_value = 8 },
130 { .center_freq = 2452, .hw_value = 9 },
131 { .center_freq = 2457, .hw_value = 10 },
132 { .center_freq = 2462, .hw_value = 11 },
133 { .center_freq = 2467, .hw_value = 12 },
134 { .center_freq = 2472, .hw_value = 13 },
135 { .center_freq = 2484, .hw_value = 14 },
138 static void housekeeping_init(struct zd_mac *mac);
139 static void housekeeping_enable(struct zd_mac *mac);
140 static void housekeeping_disable(struct zd_mac *mac);
141 static void beacon_init(struct zd_mac *mac);
142 static void beacon_enable(struct zd_mac *mac);
143 static void beacon_disable(struct zd_mac *mac);
144 static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble);
145 static int zd_mac_config_beacon(struct ieee80211_hw *hw,
146 struct sk_buff *beacon);
148 static int zd_reg2alpha2(u8 regdomain, char *alpha2)
151 struct zd_reg_alpha2_map *reg_map;
152 for (i = 0; i < ARRAY_SIZE(reg_alpha2_map); i++) {
153 reg_map = ®_alpha2_map[i];
154 if (regdomain == reg_map->reg) {
155 alpha2[0] = reg_map->alpha2[0];
156 alpha2[1] = reg_map->alpha2[1];
163 int zd_mac_preinit_hw(struct ieee80211_hw *hw)
167 struct zd_mac *mac = zd_hw_mac(hw);
169 r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
173 SET_IEEE80211_PERM_ADDR(hw, addr);
178 int zd_mac_init_hw(struct ieee80211_hw *hw)
181 struct zd_mac *mac = zd_hw_mac(hw);
182 struct zd_chip *chip = &mac->chip;
184 u8 default_regdomain;
186 r = zd_chip_enable_int(chip);
189 r = zd_chip_init_hw(chip);
193 ZD_ASSERT(!irqs_disabled());
195 r = zd_read_regdomain(chip, &default_regdomain);
198 spin_lock_irq(&mac->lock);
199 mac->regdomain = mac->default_regdomain = default_regdomain;
200 spin_unlock_irq(&mac->lock);
202 /* We must inform the device that we are doing encryption/decryption in
203 * software at the moment. */
204 r = zd_set_encryption_type(chip, ENC_SNIFFER);
208 r = zd_reg2alpha2(mac->regdomain, alpha2);
212 r = regulatory_hint(hw->wiphy, alpha2);
214 zd_chip_disable_int(chip);
219 void zd_mac_clear(struct zd_mac *mac)
221 flush_workqueue(zd_workqueue);
222 zd_chip_clear(&mac->chip);
223 ZD_ASSERT(!spin_is_locked(&mac->lock));
224 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
227 static int set_rx_filter(struct zd_mac *mac)
230 u32 filter = STA_RX_FILTER;
232 spin_lock_irqsave(&mac->lock, flags);
234 filter |= RX_FILTER_CTRL;
235 spin_unlock_irqrestore(&mac->lock, flags);
237 return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
240 static int set_mac_and_bssid(struct zd_mac *mac)
247 r = zd_write_mac_addr(&mac->chip, mac->vif->addr);
251 /* Vendor driver after setting MAC either sets BSSID for AP or
252 * filter for other modes.
254 if (mac->type != NL80211_IFTYPE_AP)
255 return set_rx_filter(mac);
257 return zd_write_bssid(&mac->chip, mac->vif->addr);
260 static int set_mc_hash(struct zd_mac *mac)
262 struct zd_mc_hash hash;
264 return zd_chip_set_multicast_hash(&mac->chip, &hash);
267 int zd_op_start(struct ieee80211_hw *hw)
269 struct zd_mac *mac = zd_hw_mac(hw);
270 struct zd_chip *chip = &mac->chip;
271 struct zd_usb *usb = &chip->usb;
274 if (!usb->initialized) {
275 r = zd_usb_init_hw(usb);
280 r = zd_chip_enable_int(chip);
284 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
287 r = set_rx_filter(mac);
290 r = set_mc_hash(mac);
293 r = zd_chip_switch_radio_on(chip);
296 r = zd_chip_enable_rxtx(chip);
299 r = zd_chip_enable_hwint(chip);
303 housekeeping_enable(mac);
305 set_bit(ZD_DEVICE_RUNNING, &mac->flags);
308 zd_chip_disable_rxtx(chip);
310 zd_chip_switch_radio_off(chip);
312 zd_chip_disable_int(chip);
317 void zd_op_stop(struct ieee80211_hw *hw)
319 struct zd_mac *mac = zd_hw_mac(hw);
320 struct zd_chip *chip = &mac->chip;
322 struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
324 clear_bit(ZD_DEVICE_RUNNING, &mac->flags);
326 /* The order here deliberately is a little different from the open()
327 * method, since we need to make sure there is no opportunity for RX
328 * frames to be processed by mac80211 after we have stopped it.
331 zd_chip_disable_rxtx(chip);
333 housekeeping_disable(mac);
334 flush_workqueue(zd_workqueue);
336 zd_chip_disable_hwint(chip);
337 zd_chip_switch_radio_off(chip);
338 zd_chip_disable_int(chip);
341 while ((skb = skb_dequeue(ack_wait_queue)))
342 dev_kfree_skb_any(skb);
345 int zd_restore_settings(struct zd_mac *mac)
347 struct sk_buff *beacon;
348 struct zd_mc_hash multicast_hash;
349 unsigned int short_preamble;
350 int r, beacon_interval, beacon_period;
353 dev_dbg_f(zd_mac_dev(mac), "\n");
355 spin_lock_irq(&mac->lock);
356 multicast_hash = mac->multicast_hash;
357 short_preamble = mac->short_preamble;
358 beacon_interval = mac->beacon.interval;
359 beacon_period = mac->beacon.period;
360 channel = mac->channel;
361 spin_unlock_irq(&mac->lock);
363 r = set_mac_and_bssid(mac);
365 dev_dbg_f(zd_mac_dev(mac), "set_mac_and_bssid failed, %d\n", r);
369 r = zd_chip_set_channel(&mac->chip, channel);
371 dev_dbg_f(zd_mac_dev(mac), "zd_chip_set_channel failed, %d\n",
376 set_rts_cts(mac, short_preamble);
378 r = zd_chip_set_multicast_hash(&mac->chip, &multicast_hash);
380 dev_dbg_f(zd_mac_dev(mac),
381 "zd_chip_set_multicast_hash failed, %d\n", r);
385 if (mac->type == NL80211_IFTYPE_MESH_POINT ||
386 mac->type == NL80211_IFTYPE_ADHOC ||
387 mac->type == NL80211_IFTYPE_AP) {
388 if (mac->vif != NULL) {
389 beacon = ieee80211_beacon_get(mac->hw, mac->vif);
391 zd_mac_config_beacon(mac->hw, beacon);
396 zd_set_beacon_interval(&mac->chip, beacon_interval,
397 beacon_period, mac->type);
399 spin_lock_irq(&mac->lock);
400 mac->beacon.last_update = jiffies;
401 spin_unlock_irq(&mac->lock);
408 * zd_mac_tx_status - reports tx status of a packet if required
409 * @hw - a &struct ieee80211_hw pointer
411 * @flags: extra flags to set in the TX status info
412 * @ackssi: ACK signal strength
413 * @success - True for successful transmission of the frame
415 * This information calls ieee80211_tx_status_irqsafe() if required by the
416 * control information. It copies the control information into the status
419 * If no status information has been requested, the skb is freed.
421 static void zd_mac_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
422 int ackssi, struct tx_status *tx_status)
424 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
426 int success = 1, retry = 1;
428 const struct tx_retry_rate *retries;
430 ieee80211_tx_info_clear_status(info);
433 success = !tx_status->failure;
434 retry = tx_status->retry + success;
439 info->flags |= IEEE80211_TX_STAT_ACK;
442 info->flags &= ~IEEE80211_TX_STAT_ACK;
445 first_idx = info->status.rates[0].idx;
446 ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
447 retries = &zd_retry_rates[first_idx];
448 ZD_ASSERT(1 <= retry && retry <= retries->count);
450 info->status.rates[0].idx = retries->rate[0];
451 info->status.rates[0].count = 1; // (retry > 1 ? 2 : 1);
453 for (i=1; i<IEEE80211_TX_MAX_RATES-1 && i<retry; i++) {
454 info->status.rates[i].idx = retries->rate[i];
455 info->status.rates[i].count = 1; // ((i==retry-1) && success ? 1:2);
457 for (; i<IEEE80211_TX_MAX_RATES && i<retry; i++) {
458 info->status.rates[i].idx = retries->rate[retry - 1];
459 info->status.rates[i].count = 1; // (success ? 1:2);
461 if (i<IEEE80211_TX_MAX_RATES)
462 info->status.rates[i].idx = -1; /* terminate */
464 info->status.ack_signal = ackssi;
465 ieee80211_tx_status_irqsafe(hw, skb);
469 * zd_mac_tx_failed - callback for failed frames
470 * @dev: the mac80211 wireless device
472 * This function is called if a frame couldn't be successfully
473 * transferred. The first frame from the tx queue, will be selected and
474 * reported as error to the upper layers.
476 void zd_mac_tx_failed(struct urb *urb)
478 struct ieee80211_hw * hw = zd_usb_to_hw(urb->context);
479 struct zd_mac *mac = zd_hw_mac(hw);
480 struct sk_buff_head *q = &mac->ack_wait_queue;
482 struct tx_status *tx_status = (struct tx_status *)urb->transfer_buffer;
484 int success = !tx_status->failure;
485 int retry = tx_status->retry + success;
489 q = &mac->ack_wait_queue;
490 spin_lock_irqsave(&q->lock, flags);
492 skb_queue_walk(q, skb) {
493 struct ieee80211_hdr *tx_hdr;
494 struct ieee80211_tx_info *info;
495 int first_idx, final_idx;
496 const struct tx_retry_rate *retries;
501 /* if the hardware reports a failure and we had a 802.11 ACK
502 * pending, then we skip the first skb when searching for a
504 if (tx_status->failure && mac->ack_pending &&
505 skb_queue_is_first(q, skb)) {
509 tx_hdr = (struct ieee80211_hdr *)skb->data;
511 /* we skip all frames not matching the reported destination */
512 if (unlikely(memcmp(tx_hdr->addr1, tx_status->mac, ETH_ALEN))) {
516 /* we skip all frames not matching the reported final rate */
518 info = IEEE80211_SKB_CB(skb);
519 first_idx = info->status.rates[0].idx;
520 ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
521 retries = &zd_retry_rates[first_idx];
522 if (retry <= 0 || retry > retries->count)
525 final_idx = retries->rate[retry - 1];
526 final_rate = zd_rates[final_idx].hw_value;
528 if (final_rate != tx_status->rate) {
537 for (i=1; i<=position; i++) {
538 skb = __skb_dequeue(q);
539 zd_mac_tx_status(hw, skb,
540 mac->ack_pending ? mac->ack_signal : 0,
541 i == position ? tx_status : NULL);
542 mac->ack_pending = 0;
546 spin_unlock_irqrestore(&q->lock, flags);
550 * zd_mac_tx_to_dev - callback for USB layer
551 * @skb: a &sk_buff pointer
552 * @error: error value, 0 if transmission successful
554 * Informs the MAC layer that the frame has successfully transferred to the
555 * device. If an ACK is required and the transfer to the device has been
556 * successful, the packets are put on the @ack_wait_queue with
557 * the control set removed.
559 void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
561 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
562 struct ieee80211_hw *hw = info->rate_driver_data[0];
563 struct zd_mac *mac = zd_hw_mac(hw);
565 ieee80211_tx_info_clear_status(info);
567 skb_pull(skb, sizeof(struct zd_ctrlset));
568 if (unlikely(error ||
569 (info->flags & IEEE80211_TX_CTL_NO_ACK))) {
571 * FIXME : do we need to fill in anything ?
573 ieee80211_tx_status_irqsafe(hw, skb);
575 struct sk_buff_head *q = &mac->ack_wait_queue;
577 skb_queue_tail(q, skb);
578 while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS) {
579 zd_mac_tx_status(hw, skb_dequeue(q),
580 mac->ack_pending ? mac->ack_signal : 0,
582 mac->ack_pending = 0;
587 static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
589 /* ZD_PURE_RATE() must be used to remove the modulation type flag of
590 * the zd-rate values.
592 static const u8 rate_divisor[] = {
593 [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
594 [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
595 /* Bits must be doubled. */
596 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
597 [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
598 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
599 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
600 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
601 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
602 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
603 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
604 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
605 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
608 u32 bits = (u32)tx_length * 8;
611 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
616 case ZD_CCK_RATE_5_5M:
617 bits = (2*bits) + 10; /* round up to the next integer */
619 case ZD_CCK_RATE_11M:
622 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
623 if (0 < t && t <= 3) {
624 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
627 bits += 10; /* round up to the next integer */
634 static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
635 struct ieee80211_hdr *header,
636 struct ieee80211_tx_info *info)
640 * - if backoff needed, enable bit 0
641 * - if burst (backoff not needed) disable bit 0
647 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
648 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
650 /* No ACK expected (multicast, etc.) */
651 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
652 cs->control |= ZD_CS_NO_ACK;
655 if (ieee80211_is_pspoll(header->frame_control))
656 cs->control |= ZD_CS_PS_POLL_FRAME;
658 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
659 cs->control |= ZD_CS_RTS;
661 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
662 cs->control |= ZD_CS_SELF_CTS;
664 /* FIXME: Management frame? */
667 static int zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon)
669 struct zd_mac *mac = zd_hw_mac(hw);
670 int r, ret, num_cmds, req_pos = 0;
672 /* 4 more bytes for tail CRC */
673 u32 full_len = beacon->len + 4;
674 unsigned long end_jiffies, message_jiffies;
675 struct zd_ioreq32 *ioreqs;
677 /* Alloc memory for full beacon write at once. */
678 num_cmds = 1 + zd_chip_is_zd1211b(&mac->chip) + full_len;
679 ioreqs = kmalloc(num_cmds * sizeof(struct zd_ioreq32), GFP_KERNEL);
683 mutex_lock(&mac->chip.mutex);
685 r = zd_iowrite32_locked(&mac->chip, 0, CR_BCN_FIFO_SEMAPHORE);
688 r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
692 end_jiffies = jiffies + HZ / 2; /*~500ms*/
693 message_jiffies = jiffies + HZ / 10; /*~100ms*/
695 r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
698 if (time_is_before_eq_jiffies(message_jiffies)) {
699 message_jiffies = jiffies + HZ / 10;
700 dev_err(zd_mac_dev(mac),
701 "CR_BCN_FIFO_SEMAPHORE not ready\n");
702 if (time_is_before_eq_jiffies(end_jiffies)) {
703 dev_err(zd_mac_dev(mac),
704 "Giving up beacon config.\n");
712 ioreqs[req_pos].addr = CR_BCN_FIFO;
713 ioreqs[req_pos].value = full_len - 1;
715 if (zd_chip_is_zd1211b(&mac->chip)) {
716 ioreqs[req_pos].addr = CR_BCN_LENGTH;
717 ioreqs[req_pos].value = full_len - 1;
721 for (j = 0 ; j < beacon->len; j++) {
722 ioreqs[req_pos].addr = CR_BCN_FIFO;
723 ioreqs[req_pos].value = *((u8 *)(beacon->data + j));
727 for (j = 0; j < 4; j++) {
728 ioreqs[req_pos].addr = CR_BCN_FIFO;
729 ioreqs[req_pos].value = 0x0;
733 BUG_ON(req_pos != num_cmds);
735 r = zd_iowrite32a_locked(&mac->chip, ioreqs, num_cmds);
739 * Try very hard to release device beacon semaphore, as otherwise
740 * device/driver can be left in unusable state.
742 end_jiffies = jiffies + HZ / 2; /*~500ms*/
743 ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
745 if (time_is_before_eq_jiffies(end_jiffies)) {
751 ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
755 dev_err(zd_mac_dev(mac), "Could not release "
756 "CR_BCN_FIFO_SEMAPHORE!\n");
757 if (r < 0 || ret < 0) {
763 /* 802.11b/g 2.4G CCK 1Mb
764 * 802.11a, not yet implemented, uses different values (see GPL vendor
767 r = zd_iowrite32_locked(&mac->chip, 0x00000400 | (full_len << 19),
770 mutex_unlock(&mac->chip.mutex);
775 mutex_unlock(&mac->chip.mutex);
778 /* semaphore stuck, reset device to avoid fw freeze later */
779 dev_warn(zd_mac_dev(mac), "CR_BCN_FIFO_SEMAPHORE stuck, "
780 "reseting device...");
781 usb_queue_reset_device(mac->chip.usb.intf);
786 static int fill_ctrlset(struct zd_mac *mac,
790 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
791 unsigned int frag_len = skb->len + FCS_LEN;
792 unsigned int packet_length;
793 struct ieee80211_rate *txrate;
794 struct zd_ctrlset *cs = (struct zd_ctrlset *)
795 skb_push(skb, sizeof(struct zd_ctrlset));
796 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
798 ZD_ASSERT(frag_len <= 0xffff);
800 txrate = ieee80211_get_tx_rate(mac->hw, info);
802 cs->modulation = txrate->hw_value;
803 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
804 cs->modulation = txrate->hw_value_short;
806 cs->tx_length = cpu_to_le16(frag_len);
808 cs_set_control(mac, cs, hdr, info);
810 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
811 ZD_ASSERT(packet_length <= 0xffff);
812 /* ZD1211B: Computing the length difference this way, gives us
813 * flexibility to compute the packet length.
815 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
816 packet_length - frag_len : packet_length);
820 * - transmit frame length in microseconds
821 * - seems to be derived from frame length
822 * - see Cal_Us_Service() in zdinlinef.h
823 * - if macp->bTxBurstEnable is enabled, then multiply by 4
824 * - bTxBurstEnable is never set in the vendor driver
827 * - "for PLCP configuration"
828 * - always 0 except in some situations at 802.11b 11M
829 * - see line 53 of zdinlinef.h
832 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
833 le16_to_cpu(cs->tx_length));
836 cs->current_length = cpu_to_le16(r);
837 cs->next_frame_length = 0;
843 * zd_op_tx - transmits a network frame to the device
845 * @dev: mac80211 hardware device
846 * @skb: socket buffer
847 * @control: the control structure
849 * This function transmit an IEEE 802.11 network frame to the device. The
850 * control block of the skbuff will be initialized. If necessary the incoming
851 * mac80211 queues will be stopped.
853 static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
855 struct zd_mac *mac = zd_hw_mac(hw);
856 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
859 r = fill_ctrlset(mac, skb);
863 info->rate_driver_data[0] = hw;
865 r = zd_usb_tx(&mac->chip.usb, skb);
876 * filter_ack - filters incoming packets for acknowledgements
877 * @dev: the mac80211 device
878 * @rx_hdr: received header
879 * @stats: the status for the received packet
881 * This functions looks for ACK packets and tries to match them with the
882 * frames in the tx queue. If a match is found the frame will be dequeued and
883 * the upper layers is informed about the successful transmission. If
884 * mac80211 queues have been stopped and the number of frames still to be
885 * transmitted is low the queues will be opened again.
887 * Returns 1 if the frame was an ACK, 0 if it was ignored.
889 static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
890 struct ieee80211_rx_status *stats)
892 struct zd_mac *mac = zd_hw_mac(hw);
894 struct sk_buff_head *q;
899 if (!ieee80211_is_ack(rx_hdr->frame_control))
902 q = &mac->ack_wait_queue;
903 spin_lock_irqsave(&q->lock, flags);
904 skb_queue_walk(q, skb) {
905 struct ieee80211_hdr *tx_hdr;
909 if (mac->ack_pending && skb_queue_is_first(q, skb))
912 tx_hdr = (struct ieee80211_hdr *)skb->data;
913 if (likely(!memcmp(tx_hdr->addr2, rx_hdr->addr1, ETH_ALEN)))
921 for (i=1; i<position; i++) {
922 skb = __skb_dequeue(q);
923 zd_mac_tx_status(hw, skb,
924 mac->ack_pending ? mac->ack_signal : 0,
926 mac->ack_pending = 0;
929 mac->ack_pending = 1;
930 mac->ack_signal = stats->signal;
932 /* Prevent pending tx-packet on AP-mode */
933 if (mac->type == NL80211_IFTYPE_AP) {
934 skb = __skb_dequeue(q);
935 zd_mac_tx_status(hw, skb, mac->ack_signal, NULL);
936 mac->ack_pending = 0;
940 spin_unlock_irqrestore(&q->lock, flags);
944 int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
946 struct zd_mac *mac = zd_hw_mac(hw);
947 struct ieee80211_rx_status stats;
948 const struct rx_status *status;
956 if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
957 FCS_LEN + sizeof(struct rx_status))
960 memset(&stats, 0, sizeof(stats));
962 /* Note about pass_failed_fcs and pass_ctrl access below:
963 * mac locking intentionally omitted here, as this is the only unlocked
964 * reader and the only writer is configure_filter. Plus, if there were
965 * any races accessing these variables, it wouldn't really matter.
966 * If mac80211 ever provides a way for us to access filter flags
967 * from outside configure_filter, we could improve on this. Also, this
968 * situation may change once we implement some kind of DMA-into-skb
971 /* Caller has to ensure that length >= sizeof(struct rx_status). */
972 status = (struct rx_status *)
973 (buffer + (length - sizeof(struct rx_status)));
974 if (status->frame_status & ZD_RX_ERROR) {
975 if (mac->pass_failed_fcs &&
976 (status->frame_status & ZD_RX_CRC32_ERROR)) {
977 stats.flag |= RX_FLAG_FAILED_FCS_CRC;
984 stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
985 stats.band = IEEE80211_BAND_2GHZ;
986 stats.signal = status->signal_strength;
988 rate = zd_rx_rate(buffer, status);
990 /* todo: return index in the big switches in zd_rx_rate instead */
991 for (i = 0; i < mac->band.n_bitrates; i++)
992 if (rate == mac->band.bitrates[i].hw_value)
995 length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
996 buffer += ZD_PLCP_HEADER_SIZE;
998 /* Except for bad frames, filter each frame to see if it is an ACK, in
999 * which case our internal TX tracking is updated. Normally we then
1000 * bail here as there's no need to pass ACKs on up to the stack, but
1001 * there is also the case where the stack has requested us to pass
1002 * control frames on up (pass_ctrl) which we must consider. */
1004 filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
1008 fc = get_unaligned((__le16*)buffer);
1009 need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
1011 skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
1015 /* Make sure the payload data is 4 byte aligned. */
1016 skb_reserve(skb, 2);
1019 /* FIXME : could we avoid this big memcpy ? */
1020 memcpy(skb_put(skb, length), buffer, length);
1022 memcpy(IEEE80211_SKB_RXCB(skb), &stats, sizeof(stats));
1023 ieee80211_rx_irqsafe(hw, skb);
1027 static int zd_op_add_interface(struct ieee80211_hw *hw,
1028 struct ieee80211_vif *vif)
1030 struct zd_mac *mac = zd_hw_mac(hw);
1032 /* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */
1033 if (mac->type != NL80211_IFTYPE_UNSPECIFIED)
1036 switch (vif->type) {
1037 case NL80211_IFTYPE_MONITOR:
1038 case NL80211_IFTYPE_MESH_POINT:
1039 case NL80211_IFTYPE_STATION:
1040 case NL80211_IFTYPE_ADHOC:
1041 case NL80211_IFTYPE_AP:
1042 mac->type = vif->type;
1050 return set_mac_and_bssid(mac);
1053 static void zd_op_remove_interface(struct ieee80211_hw *hw,
1054 struct ieee80211_vif *vif)
1056 struct zd_mac *mac = zd_hw_mac(hw);
1057 mac->type = NL80211_IFTYPE_UNSPECIFIED;
1059 zd_set_beacon_interval(&mac->chip, 0, 0, NL80211_IFTYPE_UNSPECIFIED);
1060 zd_write_mac_addr(&mac->chip, NULL);
1063 static int zd_op_config(struct ieee80211_hw *hw, u32 changed)
1065 struct zd_mac *mac = zd_hw_mac(hw);
1066 struct ieee80211_conf *conf = &hw->conf;
1068 spin_lock_irq(&mac->lock);
1069 mac->channel = conf->channel->hw_value;
1070 spin_unlock_irq(&mac->lock);
1072 return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
1075 static void zd_beacon_done(struct zd_mac *mac)
1077 struct sk_buff *skb, *beacon;
1079 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1081 if (!mac->vif || mac->vif->type != NL80211_IFTYPE_AP)
1085 * Send out buffered broad- and multicast frames.
1087 while (!ieee80211_queue_stopped(mac->hw, 0)) {
1088 skb = ieee80211_get_buffered_bc(mac->hw, mac->vif);
1091 zd_op_tx(mac->hw, skb);
1095 * Fetch next beacon so that tim_count is updated.
1097 beacon = ieee80211_beacon_get(mac->hw, mac->vif);
1099 zd_mac_config_beacon(mac->hw, beacon);
1103 spin_lock_irq(&mac->lock);
1104 mac->beacon.last_update = jiffies;
1105 spin_unlock_irq(&mac->lock);
1108 static void zd_process_intr(struct work_struct *work)
1111 unsigned long flags;
1112 struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
1114 spin_lock_irqsave(&mac->lock, flags);
1115 int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer + 4));
1116 spin_unlock_irqrestore(&mac->lock, flags);
1118 if (int_status & INT_CFG_NEXT_BCN) {
1119 /*dev_dbg_f_limit(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");*/
1120 zd_beacon_done(mac);
1122 dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
1125 zd_chip_enable_hwint(&mac->chip);
1129 static u64 zd_op_prepare_multicast(struct ieee80211_hw *hw,
1130 struct netdev_hw_addr_list *mc_list)
1132 struct zd_mac *mac = zd_hw_mac(hw);
1133 struct zd_mc_hash hash;
1134 struct netdev_hw_addr *ha;
1138 netdev_hw_addr_list_for_each(ha, mc_list) {
1139 dev_dbg_f(zd_mac_dev(mac), "mc addr %pM\n", ha->addr);
1140 zd_mc_add_addr(&hash, ha->addr);
1143 return hash.low | ((u64)hash.high << 32);
1146 #define SUPPORTED_FIF_FLAGS \
1147 (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
1148 FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
1149 static void zd_op_configure_filter(struct ieee80211_hw *hw,
1150 unsigned int changed_flags,
1151 unsigned int *new_flags,
1154 struct zd_mc_hash hash = {
1156 .high = multicast >> 32,
1158 struct zd_mac *mac = zd_hw_mac(hw);
1159 unsigned long flags;
1162 /* Only deal with supported flags */
1163 changed_flags &= SUPPORTED_FIF_FLAGS;
1164 *new_flags &= SUPPORTED_FIF_FLAGS;
1167 * If multicast parameter (as returned by zd_op_prepare_multicast)
1168 * has changed, no bit in changed_flags is set. To handle this
1169 * situation, we do not return if changed_flags is 0. If we do so,
1170 * we will have some issue with IPv6 which uses multicast for link
1171 * layer address resolution.
1173 if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI))
1174 zd_mc_add_all(&hash);
1176 spin_lock_irqsave(&mac->lock, flags);
1177 mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
1178 mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
1179 mac->multicast_hash = hash;
1180 spin_unlock_irqrestore(&mac->lock, flags);
1182 zd_chip_set_multicast_hash(&mac->chip, &hash);
1184 if (changed_flags & FIF_CONTROL) {
1185 r = set_rx_filter(mac);
1187 dev_err(zd_mac_dev(mac), "set_rx_filter error %d\n", r);
1190 /* no handling required for FIF_OTHER_BSS as we don't currently
1191 * do BSSID filtering */
1192 /* FIXME: in future it would be nice to enable the probe response
1193 * filter (so that the driver doesn't see them) until
1194 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
1195 * have to schedule work to enable prbresp reception, which might
1196 * happen too late. For now we'll just listen and forward them all the
1200 static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble)
1202 mutex_lock(&mac->chip.mutex);
1203 zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
1204 mutex_unlock(&mac->chip.mutex);
1207 static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
1208 struct ieee80211_vif *vif,
1209 struct ieee80211_bss_conf *bss_conf,
1212 struct zd_mac *mac = zd_hw_mac(hw);
1215 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
1217 if (mac->type == NL80211_IFTYPE_MESH_POINT ||
1218 mac->type == NL80211_IFTYPE_ADHOC ||
1219 mac->type == NL80211_IFTYPE_AP) {
1221 if (changes & BSS_CHANGED_BEACON) {
1222 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
1225 zd_chip_disable_hwint(&mac->chip);
1226 zd_mac_config_beacon(hw, beacon);
1227 zd_chip_enable_hwint(&mac->chip);
1232 if (changes & BSS_CHANGED_BEACON_ENABLED) {
1236 if (bss_conf->enable_beacon) {
1237 period = bss_conf->dtim_period;
1238 interval = bss_conf->beacon_int;
1241 spin_lock_irq(&mac->lock);
1242 mac->beacon.period = period;
1243 mac->beacon.interval = interval;
1244 mac->beacon.last_update = jiffies;
1245 spin_unlock_irq(&mac->lock);
1247 zd_set_beacon_interval(&mac->chip, interval, period,
1251 associated = is_valid_ether_addr(bss_conf->bssid);
1253 spin_lock_irq(&mac->lock);
1254 mac->associated = associated;
1255 spin_unlock_irq(&mac->lock);
1257 /* TODO: do hardware bssid filtering */
1259 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
1260 spin_lock_irq(&mac->lock);
1261 mac->short_preamble = bss_conf->use_short_preamble;
1262 spin_unlock_irq(&mac->lock);
1264 set_rts_cts(mac, bss_conf->use_short_preamble);
1268 static u64 zd_op_get_tsf(struct ieee80211_hw *hw)
1270 struct zd_mac *mac = zd_hw_mac(hw);
1271 return zd_chip_get_tsf(&mac->chip);
1274 static const struct ieee80211_ops zd_ops = {
1276 .start = zd_op_start,
1278 .add_interface = zd_op_add_interface,
1279 .remove_interface = zd_op_remove_interface,
1280 .config = zd_op_config,
1281 .prepare_multicast = zd_op_prepare_multicast,
1282 .configure_filter = zd_op_configure_filter,
1283 .bss_info_changed = zd_op_bss_info_changed,
1284 .get_tsf = zd_op_get_tsf,
1287 struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
1290 struct ieee80211_hw *hw;
1292 hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
1294 dev_dbg_f(&intf->dev, "out of memory\n");
1298 mac = zd_hw_mac(hw);
1300 memset(mac, 0, sizeof(*mac));
1301 spin_lock_init(&mac->lock);
1304 mac->type = NL80211_IFTYPE_UNSPECIFIED;
1306 memcpy(mac->channels, zd_channels, sizeof(zd_channels));
1307 memcpy(mac->rates, zd_rates, sizeof(zd_rates));
1308 mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
1309 mac->band.bitrates = mac->rates;
1310 mac->band.n_channels = ARRAY_SIZE(zd_channels);
1311 mac->band.channels = mac->channels;
1313 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
1315 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
1316 IEEE80211_HW_SIGNAL_UNSPEC |
1317 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
1319 hw->wiphy->interface_modes =
1320 BIT(NL80211_IFTYPE_MESH_POINT) |
1321 BIT(NL80211_IFTYPE_STATION) |
1322 BIT(NL80211_IFTYPE_ADHOC) |
1323 BIT(NL80211_IFTYPE_AP);
1325 hw->max_signal = 100;
1327 hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
1330 * Tell mac80211 that we support multi rate retries
1332 hw->max_rates = IEEE80211_TX_MAX_RATES;
1333 hw->max_rate_tries = 18; /* 9 rates * 2 retries/rate */
1335 skb_queue_head_init(&mac->ack_wait_queue);
1336 mac->ack_pending = 0;
1338 zd_chip_init(&mac->chip, hw, intf);
1339 housekeeping_init(mac);
1341 INIT_WORK(&mac->process_intr, zd_process_intr);
1343 SET_IEEE80211_DEV(hw, &intf->dev);
1347 #define BEACON_WATCHDOG_DELAY round_jiffies_relative(HZ)
1349 static void beacon_watchdog_handler(struct work_struct *work)
1351 struct zd_mac *mac =
1352 container_of(work, struct zd_mac, beacon.watchdog_work.work);
1353 struct sk_buff *beacon;
1354 unsigned long timeout;
1355 int interval, period;
1357 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1359 if (mac->type != NL80211_IFTYPE_AP || !mac->vif)
1362 spin_lock_irq(&mac->lock);
1363 interval = mac->beacon.interval;
1364 period = mac->beacon.period;
1365 timeout = mac->beacon.last_update + msecs_to_jiffies(interval) + HZ;
1366 spin_unlock_irq(&mac->lock);
1368 if (interval > 0 && time_is_before_jiffies(timeout)) {
1369 dev_dbg_f(zd_mac_dev(mac), "beacon interrupt stalled, "
1371 "(interval: %d, dtim: %d)\n",
1374 zd_chip_disable_hwint(&mac->chip);
1376 beacon = ieee80211_beacon_get(mac->hw, mac->vif);
1378 zd_mac_config_beacon(mac->hw, beacon);
1382 zd_set_beacon_interval(&mac->chip, interval, period, mac->type);
1384 zd_chip_enable_hwint(&mac->chip);
1386 spin_lock_irq(&mac->lock);
1387 mac->beacon.last_update = jiffies;
1388 spin_unlock_irq(&mac->lock);
1392 queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
1393 BEACON_WATCHDOG_DELAY);
1396 static void beacon_init(struct zd_mac *mac)
1398 INIT_DELAYED_WORK(&mac->beacon.watchdog_work, beacon_watchdog_handler);
1401 static void beacon_enable(struct zd_mac *mac)
1403 dev_dbg_f(zd_mac_dev(mac), "\n");
1405 mac->beacon.last_update = jiffies;
1406 queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
1407 BEACON_WATCHDOG_DELAY);
1410 static void beacon_disable(struct zd_mac *mac)
1412 dev_dbg_f(zd_mac_dev(mac), "\n");
1413 cancel_delayed_work_sync(&mac->beacon.watchdog_work);
1416 #define LINK_LED_WORK_DELAY HZ
1418 static void link_led_handler(struct work_struct *work)
1420 struct zd_mac *mac =
1421 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
1422 struct zd_chip *chip = &mac->chip;
1426 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1429 spin_lock_irq(&mac->lock);
1430 is_associated = mac->associated;
1431 spin_unlock_irq(&mac->lock);
1433 r = zd_chip_control_leds(chip,
1434 is_associated ? ZD_LED_ASSOCIATED : ZD_LED_SCANNING);
1436 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1439 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1440 LINK_LED_WORK_DELAY);
1443 static void housekeeping_init(struct zd_mac *mac)
1445 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
1448 static void housekeeping_enable(struct zd_mac *mac)
1450 dev_dbg_f(zd_mac_dev(mac), "\n");
1451 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1455 static void housekeeping_disable(struct zd_mac *mac)
1457 dev_dbg_f(zd_mac_dev(mac), "\n");
1458 cancel_delayed_work_sync(&mac->housekeeping.link_led_work);
1459 zd_chip_control_leds(&mac->chip, ZD_LED_OFF);