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/usb.h>
26 #include <linux/jiffies.h>
27 #include <net/ieee80211_radiotap.h>
34 struct zd_reg_alpha2_map {
39 static struct zd_reg_alpha2_map reg_alpha2_map[] = {
40 { ZD_REGDOMAIN_FCC, "US" },
41 { ZD_REGDOMAIN_IC, "CA" },
42 { ZD_REGDOMAIN_ETSI, "DE" }, /* Generic ETSI, use most restrictive */
43 { ZD_REGDOMAIN_JAPAN, "JP" },
44 { ZD_REGDOMAIN_JAPAN_ADD, "JP" },
45 { ZD_REGDOMAIN_SPAIN, "ES" },
46 { ZD_REGDOMAIN_FRANCE, "FR" },
49 /* This table contains the hardware specific values for the modulation rates. */
50 static const struct ieee80211_rate zd_rates[] = {
52 .hw_value = ZD_CCK_RATE_1M, },
54 .hw_value = ZD_CCK_RATE_2M,
55 .hw_value_short = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
56 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
58 .hw_value = ZD_CCK_RATE_5_5M,
59 .hw_value_short = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
60 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
62 .hw_value = ZD_CCK_RATE_11M,
63 .hw_value_short = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
64 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
66 .hw_value = ZD_OFDM_RATE_6M,
69 .hw_value = ZD_OFDM_RATE_9M,
72 .hw_value = ZD_OFDM_RATE_12M,
75 .hw_value = ZD_OFDM_RATE_18M,
78 .hw_value = ZD_OFDM_RATE_24M,
81 .hw_value = ZD_OFDM_RATE_36M,
84 .hw_value = ZD_OFDM_RATE_48M,
87 .hw_value = ZD_OFDM_RATE_54M,
92 * Zydas retry rates table. Each line is listed in the same order as
93 * in zd_rates[] and contains all the rate used when a packet is sent
94 * starting with a given rates. Let's consider an example :
96 * "11 Mbits : 4, 3, 2, 1, 0" means :
97 * - packet is sent using 4 different rates
98 * - 1st rate is index 3 (ie 11 Mbits)
99 * - 2nd rate is index 2 (ie 5.5 Mbits)
100 * - 3rd rate is index 1 (ie 2 Mbits)
101 * - 4th rate is index 0 (ie 1 Mbits)
104 static const struct tx_retry_rate zd_retry_rates[] = {
105 { /* 1 Mbits */ 1, { 0 }},
106 { /* 2 Mbits */ 2, { 1, 0 }},
107 { /* 5.5 Mbits */ 3, { 2, 1, 0 }},
108 { /* 11 Mbits */ 4, { 3, 2, 1, 0 }},
109 { /* 6 Mbits */ 5, { 4, 3, 2, 1, 0 }},
110 { /* 9 Mbits */ 6, { 5, 4, 3, 2, 1, 0}},
111 { /* 12 Mbits */ 5, { 6, 3, 2, 1, 0 }},
112 { /* 18 Mbits */ 6, { 7, 6, 3, 2, 1, 0 }},
113 { /* 24 Mbits */ 6, { 8, 6, 3, 2, 1, 0 }},
114 { /* 36 Mbits */ 7, { 9, 8, 6, 3, 2, 1, 0 }},
115 { /* 48 Mbits */ 8, {10, 9, 8, 6, 3, 2, 1, 0 }},
116 { /* 54 Mbits */ 9, {11, 10, 9, 8, 6, 3, 2, 1, 0 }}
119 static const struct ieee80211_channel zd_channels[] = {
120 { .center_freq = 2412, .hw_value = 1 },
121 { .center_freq = 2417, .hw_value = 2 },
122 { .center_freq = 2422, .hw_value = 3 },
123 { .center_freq = 2427, .hw_value = 4 },
124 { .center_freq = 2432, .hw_value = 5 },
125 { .center_freq = 2437, .hw_value = 6 },
126 { .center_freq = 2442, .hw_value = 7 },
127 { .center_freq = 2447, .hw_value = 8 },
128 { .center_freq = 2452, .hw_value = 9 },
129 { .center_freq = 2457, .hw_value = 10 },
130 { .center_freq = 2462, .hw_value = 11 },
131 { .center_freq = 2467, .hw_value = 12 },
132 { .center_freq = 2472, .hw_value = 13 },
133 { .center_freq = 2484, .hw_value = 14 },
136 static void housekeeping_init(struct zd_mac *mac);
137 static void housekeeping_enable(struct zd_mac *mac);
138 static void housekeeping_disable(struct zd_mac *mac);
140 static int zd_reg2alpha2(u8 regdomain, char *alpha2)
143 struct zd_reg_alpha2_map *reg_map;
144 for (i = 0; i < ARRAY_SIZE(reg_alpha2_map); i++) {
145 reg_map = ®_alpha2_map[i];
146 if (regdomain == reg_map->reg) {
147 alpha2[0] = reg_map->alpha2[0];
148 alpha2[1] = reg_map->alpha2[1];
155 int zd_mac_preinit_hw(struct ieee80211_hw *hw)
159 struct zd_mac *mac = zd_hw_mac(hw);
161 r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
165 SET_IEEE80211_PERM_ADDR(hw, addr);
170 int zd_mac_init_hw(struct ieee80211_hw *hw)
173 struct zd_mac *mac = zd_hw_mac(hw);
174 struct zd_chip *chip = &mac->chip;
176 u8 default_regdomain;
178 r = zd_chip_enable_int(chip);
181 r = zd_chip_init_hw(chip);
185 ZD_ASSERT(!irqs_disabled());
187 r = zd_read_regdomain(chip, &default_regdomain);
190 spin_lock_irq(&mac->lock);
191 mac->regdomain = mac->default_regdomain = default_regdomain;
192 spin_unlock_irq(&mac->lock);
194 /* We must inform the device that we are doing encryption/decryption in
195 * software at the moment. */
196 r = zd_set_encryption_type(chip, ENC_SNIFFER);
200 r = zd_reg2alpha2(mac->regdomain, alpha2);
204 r = regulatory_hint(hw->wiphy, alpha2);
206 zd_chip_disable_int(chip);
211 void zd_mac_clear(struct zd_mac *mac)
213 flush_workqueue(zd_workqueue);
214 zd_chip_clear(&mac->chip);
215 ZD_ASSERT(!spin_is_locked(&mac->lock));
216 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
219 static int set_rx_filter(struct zd_mac *mac)
222 u32 filter = STA_RX_FILTER;
224 spin_lock_irqsave(&mac->lock, flags);
226 filter |= RX_FILTER_CTRL;
227 spin_unlock_irqrestore(&mac->lock, flags);
229 return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
232 static int set_mc_hash(struct zd_mac *mac)
234 struct zd_mc_hash hash;
236 return zd_chip_set_multicast_hash(&mac->chip, &hash);
239 static int zd_op_start(struct ieee80211_hw *hw)
241 struct zd_mac *mac = zd_hw_mac(hw);
242 struct zd_chip *chip = &mac->chip;
243 struct zd_usb *usb = &chip->usb;
246 if (!usb->initialized) {
247 r = zd_usb_init_hw(usb);
252 r = zd_chip_enable_int(chip);
256 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
259 r = set_rx_filter(mac);
262 r = set_mc_hash(mac);
265 r = zd_chip_switch_radio_on(chip);
268 r = zd_chip_enable_rxtx(chip);
271 r = zd_chip_enable_hwint(chip);
275 housekeeping_enable(mac);
278 zd_chip_disable_rxtx(chip);
280 zd_chip_switch_radio_off(chip);
282 zd_chip_disable_int(chip);
287 static void zd_op_stop(struct ieee80211_hw *hw)
289 struct zd_mac *mac = zd_hw_mac(hw);
290 struct zd_chip *chip = &mac->chip;
292 struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
294 /* The order here deliberately is a little different from the open()
295 * method, since we need to make sure there is no opportunity for RX
296 * frames to be processed by mac80211 after we have stopped it.
299 zd_chip_disable_rxtx(chip);
300 housekeeping_disable(mac);
301 flush_workqueue(zd_workqueue);
303 zd_chip_disable_hwint(chip);
304 zd_chip_switch_radio_off(chip);
305 zd_chip_disable_int(chip);
308 while ((skb = skb_dequeue(ack_wait_queue)))
309 dev_kfree_skb_any(skb);
313 * zd_mac_tx_status - reports tx status of a packet if required
314 * @hw - a &struct ieee80211_hw pointer
316 * @flags: extra flags to set in the TX status info
317 * @ackssi: ACK signal strength
318 * @success - True for successful transmission of the frame
320 * This information calls ieee80211_tx_status_irqsafe() if required by the
321 * control information. It copies the control information into the status
324 * If no status information has been requested, the skb is freed.
326 static void zd_mac_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
327 int ackssi, struct tx_status *tx_status)
329 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
331 int success = 1, retry = 1;
333 const struct tx_retry_rate *retries;
335 ieee80211_tx_info_clear_status(info);
338 success = !tx_status->failure;
339 retry = tx_status->retry + success;
344 info->flags |= IEEE80211_TX_STAT_ACK;
347 info->flags &= ~IEEE80211_TX_STAT_ACK;
350 first_idx = info->status.rates[0].idx;
351 ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
352 retries = &zd_retry_rates[first_idx];
353 ZD_ASSERT(1 <= retry && retry <= retries->count);
355 info->status.rates[0].idx = retries->rate[0];
356 info->status.rates[0].count = 1; // (retry > 1 ? 2 : 1);
358 for (i=1; i<IEEE80211_TX_MAX_RATES-1 && i<retry; i++) {
359 info->status.rates[i].idx = retries->rate[i];
360 info->status.rates[i].count = 1; // ((i==retry-1) && success ? 1:2);
362 for (; i<IEEE80211_TX_MAX_RATES && i<retry; i++) {
363 info->status.rates[i].idx = retries->rate[retry - 1];
364 info->status.rates[i].count = 1; // (success ? 1:2);
366 if (i<IEEE80211_TX_MAX_RATES)
367 info->status.rates[i].idx = -1; /* terminate */
369 info->status.ack_signal = ackssi;
370 ieee80211_tx_status_irqsafe(hw, skb);
374 * zd_mac_tx_failed - callback for failed frames
375 * @dev: the mac80211 wireless device
377 * This function is called if a frame couldn't be successfully
378 * transferred. The first frame from the tx queue, will be selected and
379 * reported as error to the upper layers.
381 void zd_mac_tx_failed(struct urb *urb)
383 struct ieee80211_hw * hw = zd_usb_to_hw(urb->context);
384 struct zd_mac *mac = zd_hw_mac(hw);
385 struct sk_buff_head *q = &mac->ack_wait_queue;
387 struct tx_status *tx_status = (struct tx_status *)urb->transfer_buffer;
389 int success = !tx_status->failure;
390 int retry = tx_status->retry + success;
394 q = &mac->ack_wait_queue;
395 spin_lock_irqsave(&q->lock, flags);
397 skb_queue_walk(q, skb) {
398 struct ieee80211_hdr *tx_hdr;
399 struct ieee80211_tx_info *info;
400 int first_idx, final_idx;
401 const struct tx_retry_rate *retries;
406 /* if the hardware reports a failure and we had a 802.11 ACK
407 * pending, then we skip the first skb when searching for a
409 if (tx_status->failure && mac->ack_pending &&
410 skb_queue_is_first(q, skb)) {
414 tx_hdr = (struct ieee80211_hdr *)skb->data;
416 /* we skip all frames not matching the reported destination */
417 if (unlikely(memcmp(tx_hdr->addr1, tx_status->mac, ETH_ALEN))) {
421 /* we skip all frames not matching the reported final rate */
423 info = IEEE80211_SKB_CB(skb);
424 first_idx = info->status.rates[0].idx;
425 ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
426 retries = &zd_retry_rates[first_idx];
427 if (retry <= 0 || retry > retries->count)
430 final_idx = retries->rate[retry - 1];
431 final_rate = zd_rates[final_idx].hw_value;
433 if (final_rate != tx_status->rate) {
442 for (i=1; i<=position; i++) {
443 skb = __skb_dequeue(q);
444 zd_mac_tx_status(hw, skb,
445 mac->ack_pending ? mac->ack_signal : 0,
446 i == position ? tx_status : NULL);
447 mac->ack_pending = 0;
451 spin_unlock_irqrestore(&q->lock, flags);
455 * zd_mac_tx_to_dev - callback for USB layer
456 * @skb: a &sk_buff pointer
457 * @error: error value, 0 if transmission successful
459 * Informs the MAC layer that the frame has successfully transferred to the
460 * device. If an ACK is required and the transfer to the device has been
461 * successful, the packets are put on the @ack_wait_queue with
462 * the control set removed.
464 void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
466 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
467 struct ieee80211_hw *hw = info->rate_driver_data[0];
468 struct zd_mac *mac = zd_hw_mac(hw);
470 ieee80211_tx_info_clear_status(info);
472 skb_pull(skb, sizeof(struct zd_ctrlset));
473 if (unlikely(error ||
474 (info->flags & IEEE80211_TX_CTL_NO_ACK))) {
476 * FIXME : do we need to fill in anything ?
478 ieee80211_tx_status_irqsafe(hw, skb);
480 struct sk_buff_head *q = &mac->ack_wait_queue;
482 skb_queue_tail(q, skb);
483 while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS) {
484 zd_mac_tx_status(hw, skb_dequeue(q),
485 mac->ack_pending ? mac->ack_signal : 0,
487 mac->ack_pending = 0;
492 static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
494 /* ZD_PURE_RATE() must be used to remove the modulation type flag of
495 * the zd-rate values.
497 static const u8 rate_divisor[] = {
498 [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
499 [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
500 /* Bits must be doubled. */
501 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
502 [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
503 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
504 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
505 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
506 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
507 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
508 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
509 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
510 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
513 u32 bits = (u32)tx_length * 8;
516 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
521 case ZD_CCK_RATE_5_5M:
522 bits = (2*bits) + 10; /* round up to the next integer */
524 case ZD_CCK_RATE_11M:
527 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
528 if (0 < t && t <= 3) {
529 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
532 bits += 10; /* round up to the next integer */
539 static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
540 struct ieee80211_hdr *header,
541 struct ieee80211_tx_info *info)
545 * - if backoff needed, enable bit 0
546 * - if burst (backoff not needed) disable bit 0
552 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
553 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
555 /* No ACK expected (multicast, etc.) */
556 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
557 cs->control |= ZD_CS_NO_ACK;
560 if (ieee80211_is_pspoll(header->frame_control))
561 cs->control |= ZD_CS_PS_POLL_FRAME;
563 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
564 cs->control |= ZD_CS_RTS;
566 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
567 cs->control |= ZD_CS_SELF_CTS;
569 /* FIXME: Management frame? */
572 static int zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon)
574 struct zd_mac *mac = zd_hw_mac(hw);
577 /* 4 more bytes for tail CRC */
578 u32 full_len = beacon->len + 4;
580 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 0);
583 r = zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
588 r = zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
591 if ((++j % 100) == 0) {
592 printk(KERN_ERR "CR_BCN_FIFO_SEMAPHORE not ready\n");
594 printk(KERN_ERR "Giving up beacon config.\n");
601 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO, full_len - 1);
604 if (zd_chip_is_zd1211b(&mac->chip)) {
605 r = zd_iowrite32(&mac->chip, CR_BCN_LENGTH, full_len - 1);
610 for (j = 0 ; j < beacon->len; j++) {
611 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO,
612 *((u8 *)(beacon->data + j)));
617 for (j = 0; j < 4; j++) {
618 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO, 0x0);
623 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 1);
627 /* 802.11b/g 2.4G CCK 1Mb
628 * 802.11a, not yet implemented, uses different values (see GPL vendor
631 return zd_iowrite32(&mac->chip, CR_BCN_PLCP_CFG, 0x00000400 |
635 static int fill_ctrlset(struct zd_mac *mac,
639 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
640 unsigned int frag_len = skb->len + FCS_LEN;
641 unsigned int packet_length;
642 struct ieee80211_rate *txrate;
643 struct zd_ctrlset *cs = (struct zd_ctrlset *)
644 skb_push(skb, sizeof(struct zd_ctrlset));
645 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
647 ZD_ASSERT(frag_len <= 0xffff);
649 txrate = ieee80211_get_tx_rate(mac->hw, info);
651 cs->modulation = txrate->hw_value;
652 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
653 cs->modulation = txrate->hw_value_short;
655 cs->tx_length = cpu_to_le16(frag_len);
657 cs_set_control(mac, cs, hdr, info);
659 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
660 ZD_ASSERT(packet_length <= 0xffff);
661 /* ZD1211B: Computing the length difference this way, gives us
662 * flexibility to compute the packet length.
664 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
665 packet_length - frag_len : packet_length);
669 * - transmit frame length in microseconds
670 * - seems to be derived from frame length
671 * - see Cal_Us_Service() in zdinlinef.h
672 * - if macp->bTxBurstEnable is enabled, then multiply by 4
673 * - bTxBurstEnable is never set in the vendor driver
676 * - "for PLCP configuration"
677 * - always 0 except in some situations at 802.11b 11M
678 * - see line 53 of zdinlinef.h
681 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
682 le16_to_cpu(cs->tx_length));
685 cs->current_length = cpu_to_le16(r);
686 cs->next_frame_length = 0;
692 * zd_op_tx - transmits a network frame to the device
694 * @dev: mac80211 hardware device
695 * @skb: socket buffer
696 * @control: the control structure
698 * This function transmit an IEEE 802.11 network frame to the device. The
699 * control block of the skbuff will be initialized. If necessary the incoming
700 * mac80211 queues will be stopped.
702 static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
704 struct zd_mac *mac = zd_hw_mac(hw);
705 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
708 r = fill_ctrlset(mac, skb);
712 info->rate_driver_data[0] = hw;
714 r = zd_usb_tx(&mac->chip.usb, skb);
725 * filter_ack - filters incoming packets for acknowledgements
726 * @dev: the mac80211 device
727 * @rx_hdr: received header
728 * @stats: the status for the received packet
730 * This functions looks for ACK packets and tries to match them with the
731 * frames in the tx queue. If a match is found the frame will be dequeued and
732 * the upper layers is informed about the successful transmission. If
733 * mac80211 queues have been stopped and the number of frames still to be
734 * transmitted is low the queues will be opened again.
736 * Returns 1 if the frame was an ACK, 0 if it was ignored.
738 static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
739 struct ieee80211_rx_status *stats)
741 struct zd_mac *mac = zd_hw_mac(hw);
743 struct sk_buff_head *q;
748 if (!ieee80211_is_ack(rx_hdr->frame_control))
751 q = &mac->ack_wait_queue;
752 spin_lock_irqsave(&q->lock, flags);
753 skb_queue_walk(q, skb) {
754 struct ieee80211_hdr *tx_hdr;
758 if (mac->ack_pending && skb_queue_is_first(q, skb))
761 tx_hdr = (struct ieee80211_hdr *)skb->data;
762 if (likely(!memcmp(tx_hdr->addr2, rx_hdr->addr1, ETH_ALEN)))
770 for (i=1; i<position; i++) {
771 skb = __skb_dequeue(q);
772 zd_mac_tx_status(hw, skb,
773 mac->ack_pending ? mac->ack_signal : 0,
775 mac->ack_pending = 0;
778 mac->ack_pending = 1;
779 mac->ack_signal = stats->signal;
782 spin_unlock_irqrestore(&q->lock, flags);
786 int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
788 struct zd_mac *mac = zd_hw_mac(hw);
789 struct ieee80211_rx_status stats;
790 const struct rx_status *status;
798 if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
799 FCS_LEN + sizeof(struct rx_status))
802 memset(&stats, 0, sizeof(stats));
804 /* Note about pass_failed_fcs and pass_ctrl access below:
805 * mac locking intentionally omitted here, as this is the only unlocked
806 * reader and the only writer is configure_filter. Plus, if there were
807 * any races accessing these variables, it wouldn't really matter.
808 * If mac80211 ever provides a way for us to access filter flags
809 * from outside configure_filter, we could improve on this. Also, this
810 * situation may change once we implement some kind of DMA-into-skb
813 /* Caller has to ensure that length >= sizeof(struct rx_status). */
814 status = (struct rx_status *)
815 (buffer + (length - sizeof(struct rx_status)));
816 if (status->frame_status & ZD_RX_ERROR) {
817 if (mac->pass_failed_fcs &&
818 (status->frame_status & ZD_RX_CRC32_ERROR)) {
819 stats.flag |= RX_FLAG_FAILED_FCS_CRC;
826 stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
827 stats.band = IEEE80211_BAND_2GHZ;
828 stats.signal = status->signal_strength;
830 rate = zd_rx_rate(buffer, status);
832 /* todo: return index in the big switches in zd_rx_rate instead */
833 for (i = 0; i < mac->band.n_bitrates; i++)
834 if (rate == mac->band.bitrates[i].hw_value)
837 length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
838 buffer += ZD_PLCP_HEADER_SIZE;
840 /* Except for bad frames, filter each frame to see if it is an ACK, in
841 * which case our internal TX tracking is updated. Normally we then
842 * bail here as there's no need to pass ACKs on up to the stack, but
843 * there is also the case where the stack has requested us to pass
844 * control frames on up (pass_ctrl) which we must consider. */
846 filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
850 fc = get_unaligned((__le16*)buffer);
851 need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
853 skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
857 /* Make sure the the payload data is 4 byte aligned. */
861 /* FIXME : could we avoid this big memcpy ? */
862 memcpy(skb_put(skb, length), buffer, length);
864 memcpy(IEEE80211_SKB_RXCB(skb), &stats, sizeof(stats));
865 ieee80211_rx_irqsafe(hw, skb);
869 static int zd_op_add_interface(struct ieee80211_hw *hw,
870 struct ieee80211_vif *vif)
872 struct zd_mac *mac = zd_hw_mac(hw);
874 /* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */
875 if (mac->type != NL80211_IFTYPE_UNSPECIFIED)
879 case NL80211_IFTYPE_MONITOR:
880 case NL80211_IFTYPE_MESH_POINT:
881 case NL80211_IFTYPE_STATION:
882 case NL80211_IFTYPE_ADHOC:
883 mac->type = vif->type;
889 return zd_write_mac_addr(&mac->chip, vif->addr);
892 static void zd_op_remove_interface(struct ieee80211_hw *hw,
893 struct ieee80211_vif *vif)
895 struct zd_mac *mac = zd_hw_mac(hw);
896 mac->type = NL80211_IFTYPE_UNSPECIFIED;
897 zd_set_beacon_interval(&mac->chip, 0);
898 zd_write_mac_addr(&mac->chip, NULL);
901 static int zd_op_config(struct ieee80211_hw *hw, u32 changed)
903 struct zd_mac *mac = zd_hw_mac(hw);
904 struct ieee80211_conf *conf = &hw->conf;
906 return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
909 static void zd_process_intr(struct work_struct *work)
912 struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
914 int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer+4));
915 if (int_status & INT_CFG_NEXT_BCN)
916 dev_dbg_f_limit(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");
918 dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
920 zd_chip_enable_hwint(&mac->chip);
924 static void set_multicast_hash_handler(struct work_struct *work)
927 container_of(work, struct zd_mac, set_multicast_hash_work);
928 struct zd_mc_hash hash;
930 spin_lock_irq(&mac->lock);
931 hash = mac->multicast_hash;
932 spin_unlock_irq(&mac->lock);
934 zd_chip_set_multicast_hash(&mac->chip, &hash);
937 static void set_rx_filter_handler(struct work_struct *work)
940 container_of(work, struct zd_mac, set_rx_filter_work);
943 dev_dbg_f(zd_mac_dev(mac), "\n");
944 r = set_rx_filter(mac);
946 dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r);
949 static u64 zd_op_prepare_multicast(struct ieee80211_hw *hw,
950 int mc_count, struct dev_addr_list *mclist)
952 struct zd_mac *mac = zd_hw_mac(hw);
953 struct zd_mc_hash hash;
958 for (i = 0; i < mc_count; i++) {
961 dev_dbg_f(zd_mac_dev(mac), "mc addr %pM\n", mclist->dmi_addr);
962 zd_mc_add_addr(&hash, mclist->dmi_addr);
963 mclist = mclist->next;
966 return hash.low | ((u64)hash.high << 32);
969 #define SUPPORTED_FIF_FLAGS \
970 (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
971 FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
972 static void zd_op_configure_filter(struct ieee80211_hw *hw,
973 unsigned int changed_flags,
974 unsigned int *new_flags,
977 struct zd_mc_hash hash = {
979 .high = multicast >> 32,
981 struct zd_mac *mac = zd_hw_mac(hw);
984 /* Only deal with supported flags */
985 changed_flags &= SUPPORTED_FIF_FLAGS;
986 *new_flags &= SUPPORTED_FIF_FLAGS;
989 * If multicast parameter (as returned by zd_op_prepare_multicast)
990 * has changed, no bit in changed_flags is set. To handle this
991 * situation, we do not return if changed_flags is 0. If we do so,
992 * we will have some issue with IPv6 which uses multicast for link
993 * layer address resolution.
995 if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI))
996 zd_mc_add_all(&hash);
998 spin_lock_irqsave(&mac->lock, flags);
999 mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
1000 mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
1001 mac->multicast_hash = hash;
1002 spin_unlock_irqrestore(&mac->lock, flags);
1004 /* XXX: these can be called here now, can sleep now! */
1005 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
1007 if (changed_flags & FIF_CONTROL)
1008 queue_work(zd_workqueue, &mac->set_rx_filter_work);
1010 /* no handling required for FIF_OTHER_BSS as we don't currently
1011 * do BSSID filtering */
1012 /* FIXME: in future it would be nice to enable the probe response
1013 * filter (so that the driver doesn't see them) until
1014 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
1015 * have to schedule work to enable prbresp reception, which might
1016 * happen too late. For now we'll just listen and forward them all the
1020 static void set_rts_cts_work(struct work_struct *work)
1022 struct zd_mac *mac =
1023 container_of(work, struct zd_mac, set_rts_cts_work);
1024 unsigned long flags;
1025 unsigned int short_preamble;
1027 mutex_lock(&mac->chip.mutex);
1029 spin_lock_irqsave(&mac->lock, flags);
1030 mac->updating_rts_rate = 0;
1031 short_preamble = mac->short_preamble;
1032 spin_unlock_irqrestore(&mac->lock, flags);
1034 zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
1035 mutex_unlock(&mac->chip.mutex);
1038 static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
1039 struct ieee80211_vif *vif,
1040 struct ieee80211_bss_conf *bss_conf,
1043 struct zd_mac *mac = zd_hw_mac(hw);
1044 unsigned long flags;
1047 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
1049 if (mac->type == NL80211_IFTYPE_MESH_POINT ||
1050 mac->type == NL80211_IFTYPE_ADHOC) {
1052 if (changes & BSS_CHANGED_BEACON) {
1053 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
1056 zd_mac_config_beacon(hw, beacon);
1061 if (changes & BSS_CHANGED_BEACON_ENABLED) {
1064 if (bss_conf->enable_beacon)
1065 interval = BCN_MODE_IBSS |
1066 bss_conf->beacon_int;
1070 zd_set_beacon_interval(&mac->chip, interval);
1073 associated = is_valid_ether_addr(bss_conf->bssid);
1075 spin_lock_irq(&mac->lock);
1076 mac->associated = associated;
1077 spin_unlock_irq(&mac->lock);
1079 /* TODO: do hardware bssid filtering */
1081 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
1082 spin_lock_irqsave(&mac->lock, flags);
1083 mac->short_preamble = bss_conf->use_short_preamble;
1084 if (!mac->updating_rts_rate) {
1085 mac->updating_rts_rate = 1;
1086 /* FIXME: should disable TX here, until work has
1087 * completed and RTS_CTS reg is updated */
1088 queue_work(zd_workqueue, &mac->set_rts_cts_work);
1090 spin_unlock_irqrestore(&mac->lock, flags);
1094 static u64 zd_op_get_tsf(struct ieee80211_hw *hw)
1096 struct zd_mac *mac = zd_hw_mac(hw);
1097 return zd_chip_get_tsf(&mac->chip);
1100 static const struct ieee80211_ops zd_ops = {
1102 .start = zd_op_start,
1104 .add_interface = zd_op_add_interface,
1105 .remove_interface = zd_op_remove_interface,
1106 .config = zd_op_config,
1107 .prepare_multicast = zd_op_prepare_multicast,
1108 .configure_filter = zd_op_configure_filter,
1109 .bss_info_changed = zd_op_bss_info_changed,
1110 .get_tsf = zd_op_get_tsf,
1113 struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
1116 struct ieee80211_hw *hw;
1118 hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
1120 dev_dbg_f(&intf->dev, "out of memory\n");
1124 mac = zd_hw_mac(hw);
1126 memset(mac, 0, sizeof(*mac));
1127 spin_lock_init(&mac->lock);
1130 mac->type = NL80211_IFTYPE_UNSPECIFIED;
1132 memcpy(mac->channels, zd_channels, sizeof(zd_channels));
1133 memcpy(mac->rates, zd_rates, sizeof(zd_rates));
1134 mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
1135 mac->band.bitrates = mac->rates;
1136 mac->band.n_channels = ARRAY_SIZE(zd_channels);
1137 mac->band.channels = mac->channels;
1139 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
1141 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
1142 IEEE80211_HW_SIGNAL_UNSPEC;
1144 hw->wiphy->interface_modes =
1145 BIT(NL80211_IFTYPE_MESH_POINT) |
1146 BIT(NL80211_IFTYPE_STATION) |
1147 BIT(NL80211_IFTYPE_ADHOC);
1149 hw->max_signal = 100;
1151 hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
1154 * Tell mac80211 that we support multi rate retries
1156 hw->max_rates = IEEE80211_TX_MAX_RATES;
1157 hw->max_rate_tries = 18; /* 9 rates * 2 retries/rate */
1159 skb_queue_head_init(&mac->ack_wait_queue);
1160 mac->ack_pending = 0;
1162 zd_chip_init(&mac->chip, hw, intf);
1163 housekeeping_init(mac);
1164 INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
1165 INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
1166 INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler);
1167 INIT_WORK(&mac->process_intr, zd_process_intr);
1169 SET_IEEE80211_DEV(hw, &intf->dev);
1173 #define LINK_LED_WORK_DELAY HZ
1175 static void link_led_handler(struct work_struct *work)
1177 struct zd_mac *mac =
1178 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
1179 struct zd_chip *chip = &mac->chip;
1183 spin_lock_irq(&mac->lock);
1184 is_associated = mac->associated;
1185 spin_unlock_irq(&mac->lock);
1187 r = zd_chip_control_leds(chip,
1188 is_associated ? ZD_LED_ASSOCIATED : ZD_LED_SCANNING);
1190 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1192 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1193 LINK_LED_WORK_DELAY);
1196 static void housekeeping_init(struct zd_mac *mac)
1198 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
1201 static void housekeeping_enable(struct zd_mac *mac)
1203 dev_dbg_f(zd_mac_dev(mac), "\n");
1204 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1208 static void housekeeping_disable(struct zd_mac *mac)
1210 dev_dbg_f(zd_mac_dev(mac), "\n");
1211 cancel_rearming_delayed_workqueue(zd_workqueue,
1212 &mac->housekeeping.link_led_work);
1213 zd_chip_control_leds(&mac->chip, ZD_LED_OFF);