Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / net / wireless / zd1211rw / zd_mac.c
blobe29bd8e0ec7cbcd0a67c27343ad14c919f198615
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 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>
29 #include "zd_def.h"
30 #include "zd_chip.h"
31 #include "zd_mac.h"
32 #include "zd_ieee80211.h"
33 #include "zd_rf.h"
35 /* This table contains the hardware specific values for the modulation rates. */
36 static const struct ieee80211_rate zd_rates[] = {
37 { .rate = 10,
38 .val = ZD_CCK_RATE_1M,
39 .flags = IEEE80211_RATE_CCK },
40 { .rate = 20,
41 .val = ZD_CCK_RATE_2M,
42 .val2 = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
43 .flags = IEEE80211_RATE_CCK_2 },
44 { .rate = 55,
45 .val = ZD_CCK_RATE_5_5M,
46 .val2 = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
47 .flags = IEEE80211_RATE_CCK_2 },
48 { .rate = 110,
49 .val = ZD_CCK_RATE_11M,
50 .val2 = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
51 .flags = IEEE80211_RATE_CCK_2 },
52 { .rate = 60,
53 .val = ZD_OFDM_RATE_6M,
54 .flags = IEEE80211_RATE_OFDM },
55 { .rate = 90,
56 .val = ZD_OFDM_RATE_9M,
57 .flags = IEEE80211_RATE_OFDM },
58 { .rate = 120,
59 .val = ZD_OFDM_RATE_12M,
60 .flags = IEEE80211_RATE_OFDM },
61 { .rate = 180,
62 .val = ZD_OFDM_RATE_18M,
63 .flags = IEEE80211_RATE_OFDM },
64 { .rate = 240,
65 .val = ZD_OFDM_RATE_24M,
66 .flags = IEEE80211_RATE_OFDM },
67 { .rate = 360,
68 .val = ZD_OFDM_RATE_36M,
69 .flags = IEEE80211_RATE_OFDM },
70 { .rate = 480,
71 .val = ZD_OFDM_RATE_48M,
72 .flags = IEEE80211_RATE_OFDM },
73 { .rate = 540,
74 .val = ZD_OFDM_RATE_54M,
75 .flags = IEEE80211_RATE_OFDM },
78 static const struct ieee80211_channel zd_channels[] = {
79 { .chan = 1,
80 .freq = 2412},
81 { .chan = 2,
82 .freq = 2417},
83 { .chan = 3,
84 .freq = 2422},
85 { .chan = 4,
86 .freq = 2427},
87 { .chan = 5,
88 .freq = 2432},
89 { .chan = 6,
90 .freq = 2437},
91 { .chan = 7,
92 .freq = 2442},
93 { .chan = 8,
94 .freq = 2447},
95 { .chan = 9,
96 .freq = 2452},
97 { .chan = 10,
98 .freq = 2457},
99 { .chan = 11,
100 .freq = 2462},
101 { .chan = 12,
102 .freq = 2467},
103 { .chan = 13,
104 .freq = 2472},
105 { .chan = 14,
106 .freq = 2484}
109 static void housekeeping_init(struct zd_mac *mac);
110 static void housekeeping_enable(struct zd_mac *mac);
111 static void housekeeping_disable(struct zd_mac *mac);
113 int zd_mac_preinit_hw(struct ieee80211_hw *hw)
115 int r;
116 u8 addr[ETH_ALEN];
117 struct zd_mac *mac = zd_hw_mac(hw);
119 r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
120 if (r)
121 return r;
123 SET_IEEE80211_PERM_ADDR(hw, addr);
125 return 0;
128 int zd_mac_init_hw(struct ieee80211_hw *hw)
130 int r;
131 struct zd_mac *mac = zd_hw_mac(hw);
132 struct zd_chip *chip = &mac->chip;
133 u8 default_regdomain;
135 r = zd_chip_enable_int(chip);
136 if (r)
137 goto out;
138 r = zd_chip_init_hw(chip);
139 if (r)
140 goto disable_int;
142 ZD_ASSERT(!irqs_disabled());
144 r = zd_read_regdomain(chip, &default_regdomain);
145 if (r)
146 goto disable_int;
147 spin_lock_irq(&mac->lock);
148 mac->regdomain = mac->default_regdomain = default_regdomain;
149 spin_unlock_irq(&mac->lock);
151 /* We must inform the device that we are doing encryption/decryption in
152 * software at the moment. */
153 r = zd_set_encryption_type(chip, ENC_SNIFFER);
154 if (r)
155 goto disable_int;
157 zd_geo_init(hw, mac->regdomain);
159 r = 0;
160 disable_int:
161 zd_chip_disable_int(chip);
162 out:
163 return r;
166 void zd_mac_clear(struct zd_mac *mac)
168 flush_workqueue(zd_workqueue);
169 zd_chip_clear(&mac->chip);
170 ZD_ASSERT(!spin_is_locked(&mac->lock));
171 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
174 static int set_rx_filter(struct zd_mac *mac)
176 unsigned long flags;
177 u32 filter = STA_RX_FILTER;
179 spin_lock_irqsave(&mac->lock, flags);
180 if (mac->pass_ctrl)
181 filter |= RX_FILTER_CTRL;
182 spin_unlock_irqrestore(&mac->lock, flags);
184 return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
187 static int set_mc_hash(struct zd_mac *mac)
189 struct zd_mc_hash hash;
190 zd_mc_clear(&hash);
191 return zd_chip_set_multicast_hash(&mac->chip, &hash);
194 static int zd_op_start(struct ieee80211_hw *hw)
196 struct zd_mac *mac = zd_hw_mac(hw);
197 struct zd_chip *chip = &mac->chip;
198 struct zd_usb *usb = &chip->usb;
199 int r;
201 if (!usb->initialized) {
202 r = zd_usb_init_hw(usb);
203 if (r)
204 goto out;
207 r = zd_chip_enable_int(chip);
208 if (r < 0)
209 goto out;
211 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
212 if (r < 0)
213 goto disable_int;
214 r = set_rx_filter(mac);
215 if (r)
216 goto disable_int;
217 r = set_mc_hash(mac);
218 if (r)
219 goto disable_int;
220 r = zd_chip_switch_radio_on(chip);
221 if (r < 0)
222 goto disable_int;
223 r = zd_chip_enable_rxtx(chip);
224 if (r < 0)
225 goto disable_radio;
226 r = zd_chip_enable_hwint(chip);
227 if (r < 0)
228 goto disable_rxtx;
230 housekeeping_enable(mac);
231 return 0;
232 disable_rxtx:
233 zd_chip_disable_rxtx(chip);
234 disable_radio:
235 zd_chip_switch_radio_off(chip);
236 disable_int:
237 zd_chip_disable_int(chip);
238 out:
239 return r;
243 * clear_tx_skb_control_block - clears the control block of tx skbuffs
244 * @skb: a &struct sk_buff pointer
246 * This clears the control block of skbuff buffers, which were transmitted to
247 * the device. Notify that the function is not thread-safe, so prevent
248 * multiple calls.
250 static void clear_tx_skb_control_block(struct sk_buff *skb)
252 struct zd_tx_skb_control_block *cb =
253 (struct zd_tx_skb_control_block *)skb->cb;
255 kfree(cb->control);
256 cb->control = NULL;
260 * kfree_tx_skb - frees a tx skbuff
261 * @skb: a &struct sk_buff pointer
263 * Frees the tx skbuff. Frees also the allocated control structure in the
264 * control block if necessary.
266 static void kfree_tx_skb(struct sk_buff *skb)
268 clear_tx_skb_control_block(skb);
269 dev_kfree_skb_any(skb);
272 static void zd_op_stop(struct ieee80211_hw *hw)
274 struct zd_mac *mac = zd_hw_mac(hw);
275 struct zd_chip *chip = &mac->chip;
276 struct sk_buff *skb;
277 struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
279 /* The order here deliberately is a little different from the open()
280 * method, since we need to make sure there is no opportunity for RX
281 * frames to be processed by mac80211 after we have stopped it.
284 zd_chip_disable_rxtx(chip);
285 housekeeping_disable(mac);
286 flush_workqueue(zd_workqueue);
288 zd_chip_disable_hwint(chip);
289 zd_chip_switch_radio_off(chip);
290 zd_chip_disable_int(chip);
293 while ((skb = skb_dequeue(ack_wait_queue)))
294 kfree_tx_skb(skb);
298 * init_tx_skb_control_block - initializes skb control block
299 * @skb: a &sk_buff pointer
300 * @dev: pointer to the mac80221 device
301 * @control: mac80211 tx control applying for the frame in @skb
303 * Initializes the control block of the skbuff to be transmitted.
305 static int init_tx_skb_control_block(struct sk_buff *skb,
306 struct ieee80211_hw *hw,
307 struct ieee80211_tx_control *control)
309 struct zd_tx_skb_control_block *cb =
310 (struct zd_tx_skb_control_block *)skb->cb;
312 ZD_ASSERT(sizeof(*cb) <= sizeof(skb->cb));
313 memset(cb, 0, sizeof(*cb));
314 cb->hw= hw;
315 cb->control = kmalloc(sizeof(*control), GFP_ATOMIC);
316 if (cb->control == NULL)
317 return -ENOMEM;
318 memcpy(cb->control, control, sizeof(*control));
320 return 0;
324 * tx_status - reports tx status of a packet if required
325 * @hw - a &struct ieee80211_hw pointer
326 * @skb - a sk-buffer
327 * @status - the tx status of the packet without control information
328 * @success - True for successfull transmission of the frame
330 * This information calls ieee80211_tx_status_irqsafe() if required by the
331 * control information. It copies the control information into the status
332 * information.
334 * If no status information has been requested, the skb is freed.
336 static void tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
337 struct ieee80211_tx_status *status,
338 bool success)
340 struct zd_tx_skb_control_block *cb = (struct zd_tx_skb_control_block *)
341 skb->cb;
343 ZD_ASSERT(cb->control != NULL);
344 memcpy(&status->control, cb->control, sizeof(status->control));
345 if (!success)
346 status->excessive_retries = 1;
347 clear_tx_skb_control_block(skb);
348 ieee80211_tx_status_irqsafe(hw, skb, status);
352 * zd_mac_tx_failed - callback for failed frames
353 * @dev: the mac80211 wireless device
355 * This function is called if a frame couldn't be succesfully be
356 * transferred. The first frame from the tx queue, will be selected and
357 * reported as error to the upper layers.
359 void zd_mac_tx_failed(struct ieee80211_hw *hw)
361 struct sk_buff_head *q = &zd_hw_mac(hw)->ack_wait_queue;
362 struct sk_buff *skb;
363 <<<<<<< HEAD:drivers/net/wireless/zd1211rw/zd_mac.c
364 struct ieee80211_tx_status status = {{0}};
365 =======
366 struct ieee80211_tx_status status;
367 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/wireless/zd1211rw/zd_mac.c
369 skb = skb_dequeue(q);
370 if (skb == NULL)
371 return;
372 <<<<<<< HEAD:drivers/net/wireless/zd1211rw/zd_mac.c
373 =======
375 memset(&status, 0, sizeof(status));
377 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/wireless/zd1211rw/zd_mac.c
378 tx_status(hw, skb, &status, 0);
382 * zd_mac_tx_to_dev - callback for USB layer
383 * @skb: a &sk_buff pointer
384 * @error: error value, 0 if transmission successful
386 * Informs the MAC layer that the frame has successfully transferred to the
387 * device. If an ACK is required and the transfer to the device has been
388 * successful, the packets are put on the @ack_wait_queue with
389 * the control set removed.
391 void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
393 struct zd_tx_skb_control_block *cb =
394 (struct zd_tx_skb_control_block *)skb->cb;
395 struct ieee80211_hw *hw = cb->hw;
397 if (likely(cb->control)) {
398 skb_pull(skb, sizeof(struct zd_ctrlset));
399 if (unlikely(error ||
400 (cb->control->flags & IEEE80211_TXCTL_NO_ACK)))
402 <<<<<<< HEAD:drivers/net/wireless/zd1211rw/zd_mac.c
403 struct ieee80211_tx_status status = {{0}};
404 =======
405 struct ieee80211_tx_status status;
406 memset(&status, 0, sizeof(status));
407 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/wireless/zd1211rw/zd_mac.c
408 tx_status(hw, skb, &status, !error);
409 } else {
410 struct sk_buff_head *q =
411 &zd_hw_mac(hw)->ack_wait_queue;
413 skb_queue_tail(q, skb);
414 while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS)
415 zd_mac_tx_failed(hw);
417 } else {
418 kfree_tx_skb(skb);
422 static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
424 /* ZD_PURE_RATE() must be used to remove the modulation type flag of
425 * the zd-rate values.
427 static const u8 rate_divisor[] = {
428 [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
429 [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
430 /* Bits must be doubled. */
431 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
432 [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
433 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
434 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
435 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
436 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
437 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
438 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
439 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
440 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
443 u32 bits = (u32)tx_length * 8;
444 u32 divisor;
446 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
447 if (divisor == 0)
448 return -EINVAL;
450 switch (zd_rate) {
451 case ZD_CCK_RATE_5_5M:
452 bits = (2*bits) + 10; /* round up to the next integer */
453 break;
454 case ZD_CCK_RATE_11M:
455 if (service) {
456 u32 t = bits % 11;
457 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
458 if (0 < t && t <= 3) {
459 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
462 bits += 10; /* round up to the next integer */
463 break;
466 return bits/divisor;
469 static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
470 struct ieee80211_hdr *header, u32 flags)
472 u16 fctl = le16_to_cpu(header->frame_control);
475 * CONTROL TODO:
476 * - if backoff needed, enable bit 0
477 * - if burst (backoff not needed) disable bit 0
480 cs->control = 0;
482 /* First fragment */
483 if (flags & IEEE80211_TXCTL_FIRST_FRAGMENT)
484 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
486 /* Multicast */
487 if (is_multicast_ether_addr(header->addr1))
488 cs->control |= ZD_CS_MULTICAST;
490 /* PS-POLL */
491 if ((fctl & (IEEE80211_FCTL_FTYPE|IEEE80211_FCTL_STYPE)) ==
492 (IEEE80211_FTYPE_CTL|IEEE80211_STYPE_PSPOLL))
493 cs->control |= ZD_CS_PS_POLL_FRAME;
495 if (flags & IEEE80211_TXCTL_USE_RTS_CTS)
496 cs->control |= ZD_CS_RTS;
498 if (flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
499 cs->control |= ZD_CS_SELF_CTS;
501 /* FIXME: Management frame? */
504 static int fill_ctrlset(struct zd_mac *mac,
505 struct sk_buff *skb,
506 struct ieee80211_tx_control *control)
508 int r;
509 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
510 unsigned int frag_len = skb->len + FCS_LEN;
511 unsigned int packet_length;
512 struct zd_ctrlset *cs = (struct zd_ctrlset *)
513 skb_push(skb, sizeof(struct zd_ctrlset));
515 ZD_ASSERT(frag_len <= 0xffff);
517 cs->modulation = control->tx_rate;
519 cs->tx_length = cpu_to_le16(frag_len);
521 cs_set_control(mac, cs, hdr, control->flags);
523 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
524 ZD_ASSERT(packet_length <= 0xffff);
525 /* ZD1211B: Computing the length difference this way, gives us
526 * flexibility to compute the packet length.
528 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
529 packet_length - frag_len : packet_length);
532 * CURRENT LENGTH:
533 * - transmit frame length in microseconds
534 * - seems to be derived from frame length
535 * - see Cal_Us_Service() in zdinlinef.h
536 * - if macp->bTxBurstEnable is enabled, then multiply by 4
537 * - bTxBurstEnable is never set in the vendor driver
539 * SERVICE:
540 * - "for PLCP configuration"
541 * - always 0 except in some situations at 802.11b 11M
542 * - see line 53 of zdinlinef.h
544 cs->service = 0;
545 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
546 le16_to_cpu(cs->tx_length));
547 if (r < 0)
548 return r;
549 cs->current_length = cpu_to_le16(r);
550 cs->next_frame_length = 0;
552 return 0;
556 * zd_op_tx - transmits a network frame to the device
558 * @dev: mac80211 hardware device
559 * @skb: socket buffer
560 * @control: the control structure
562 * This function transmit an IEEE 802.11 network frame to the device. The
563 * control block of the skbuff will be initialized. If necessary the incoming
564 * mac80211 queues will be stopped.
566 static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
567 struct ieee80211_tx_control *control)
569 struct zd_mac *mac = zd_hw_mac(hw);
570 int r;
572 r = fill_ctrlset(mac, skb, control);
573 if (r)
574 return r;
576 r = init_tx_skb_control_block(skb, hw, control);
577 if (r)
578 return r;
579 r = zd_usb_tx(&mac->chip.usb, skb);
580 if (r) {
581 clear_tx_skb_control_block(skb);
582 return r;
584 return 0;
588 * filter_ack - filters incoming packets for acknowledgements
589 * @dev: the mac80211 device
590 * @rx_hdr: received header
591 * @stats: the status for the received packet
593 * This functions looks for ACK packets and tries to match them with the
594 * frames in the tx queue. If a match is found the frame will be dequeued and
595 * the upper layers is informed about the successful transmission. If
596 * mac80211 queues have been stopped and the number of frames still to be
597 * transmitted is low the queues will be opened again.
599 * Returns 1 if the frame was an ACK, 0 if it was ignored.
601 static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
602 struct ieee80211_rx_status *stats)
604 u16 fc = le16_to_cpu(rx_hdr->frame_control);
605 struct sk_buff *skb;
606 struct sk_buff_head *q;
607 unsigned long flags;
609 if ((fc & (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) !=
610 (IEEE80211_FTYPE_CTL | IEEE80211_STYPE_ACK))
611 return 0;
613 q = &zd_hw_mac(hw)->ack_wait_queue;
614 spin_lock_irqsave(&q->lock, flags);
615 for (skb = q->next; skb != (struct sk_buff *)q; skb = skb->next) {
616 struct ieee80211_hdr *tx_hdr;
618 tx_hdr = (struct ieee80211_hdr *)skb->data;
619 if (likely(!compare_ether_addr(tx_hdr->addr2, rx_hdr->addr1)))
621 <<<<<<< HEAD:drivers/net/wireless/zd1211rw/zd_mac.c
622 struct ieee80211_tx_status status = {{0}};
623 =======
624 struct ieee80211_tx_status status;
626 memset(&status, 0, sizeof(status));
627 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/wireless/zd1211rw/zd_mac.c
628 status.flags = IEEE80211_TX_STATUS_ACK;
629 status.ack_signal = stats->ssi;
630 __skb_unlink(skb, q);
631 tx_status(hw, skb, &status, 1);
632 goto out;
635 out:
636 spin_unlock_irqrestore(&q->lock, flags);
637 return 1;
640 int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
642 struct zd_mac *mac = zd_hw_mac(hw);
643 struct ieee80211_rx_status stats;
644 const struct rx_status *status;
645 struct sk_buff *skb;
646 int bad_frame = 0;
647 u16 fc;
648 bool is_qos, is_4addr, need_padding;
650 if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
651 FCS_LEN + sizeof(struct rx_status))
652 return -EINVAL;
654 memset(&stats, 0, sizeof(stats));
656 /* Note about pass_failed_fcs and pass_ctrl access below:
657 * mac locking intentionally omitted here, as this is the only unlocked
658 * reader and the only writer is configure_filter. Plus, if there were
659 * any races accessing these variables, it wouldn't really matter.
660 * If mac80211 ever provides a way for us to access filter flags
661 * from outside configure_filter, we could improve on this. Also, this
662 * situation may change once we implement some kind of DMA-into-skb
663 * RX path. */
665 /* Caller has to ensure that length >= sizeof(struct rx_status). */
666 status = (struct rx_status *)
667 (buffer + (length - sizeof(struct rx_status)));
668 if (status->frame_status & ZD_RX_ERROR) {
669 if (mac->pass_failed_fcs &&
670 (status->frame_status & ZD_RX_CRC32_ERROR)) {
671 stats.flag |= RX_FLAG_FAILED_FCS_CRC;
672 bad_frame = 1;
673 } else {
674 return -EINVAL;
678 stats.channel = _zd_chip_get_channel(&mac->chip);
679 stats.freq = zd_channels[stats.channel - 1].freq;
680 stats.phymode = MODE_IEEE80211G;
681 stats.ssi = status->signal_strength;
682 stats.signal = zd_rx_qual_percent(buffer,
683 length - sizeof(struct rx_status),
684 status);
685 stats.rate = zd_rx_rate(buffer, status);
687 length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
688 buffer += ZD_PLCP_HEADER_SIZE;
690 /* Except for bad frames, filter each frame to see if it is an ACK, in
691 * which case our internal TX tracking is updated. Normally we then
692 * bail here as there's no need to pass ACKs on up to the stack, but
693 * there is also the case where the stack has requested us to pass
694 * control frames on up (pass_ctrl) which we must consider. */
695 if (!bad_frame &&
696 filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
697 && !mac->pass_ctrl)
698 return 0;
700 fc = le16_to_cpu(*((__le16 *) buffer));
702 is_qos = ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
703 ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_QOS_DATA);
704 is_4addr = (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
705 (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS);
706 need_padding = is_qos ^ is_4addr;
708 skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
709 if (skb == NULL)
710 return -ENOMEM;
711 if (need_padding) {
712 /* Make sure the the payload data is 4 byte aligned. */
713 skb_reserve(skb, 2);
716 memcpy(skb_put(skb, length), buffer, length);
718 ieee80211_rx_irqsafe(hw, skb, &stats);
719 return 0;
722 static int zd_op_add_interface(struct ieee80211_hw *hw,
723 struct ieee80211_if_init_conf *conf)
725 struct zd_mac *mac = zd_hw_mac(hw);
727 /* using IEEE80211_IF_TYPE_INVALID to indicate no mode selected */
728 if (mac->type != IEEE80211_IF_TYPE_INVALID)
729 return -EOPNOTSUPP;
731 switch (conf->type) {
732 case IEEE80211_IF_TYPE_MNTR:
733 case IEEE80211_IF_TYPE_STA:
734 mac->type = conf->type;
735 break;
736 default:
737 return -EOPNOTSUPP;
740 return zd_write_mac_addr(&mac->chip, conf->mac_addr);
743 static void zd_op_remove_interface(struct ieee80211_hw *hw,
744 struct ieee80211_if_init_conf *conf)
746 struct zd_mac *mac = zd_hw_mac(hw);
747 mac->type = IEEE80211_IF_TYPE_INVALID;
748 zd_write_mac_addr(&mac->chip, NULL);
751 static int zd_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
753 struct zd_mac *mac = zd_hw_mac(hw);
754 return zd_chip_set_channel(&mac->chip, conf->channel);
757 static int zd_op_config_interface(struct ieee80211_hw *hw,
758 struct ieee80211_vif *vif,
759 struct ieee80211_if_conf *conf)
761 struct zd_mac *mac = zd_hw_mac(hw);
763 spin_lock_irq(&mac->lock);
764 mac->associated = is_valid_ether_addr(conf->bssid);
765 spin_unlock_irq(&mac->lock);
767 /* TODO: do hardware bssid filtering */
768 return 0;
771 static void set_multicast_hash_handler(struct work_struct *work)
773 struct zd_mac *mac =
774 container_of(work, struct zd_mac, set_multicast_hash_work);
775 struct zd_mc_hash hash;
777 spin_lock_irq(&mac->lock);
778 hash = mac->multicast_hash;
779 spin_unlock_irq(&mac->lock);
781 zd_chip_set_multicast_hash(&mac->chip, &hash);
784 static void set_rx_filter_handler(struct work_struct *work)
786 struct zd_mac *mac =
787 container_of(work, struct zd_mac, set_rx_filter_work);
788 int r;
790 dev_dbg_f(zd_mac_dev(mac), "\n");
791 r = set_rx_filter(mac);
792 if (r)
793 dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r);
796 #define SUPPORTED_FIF_FLAGS \
797 (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
798 FIF_OTHER_BSS)
799 static void zd_op_configure_filter(struct ieee80211_hw *hw,
800 unsigned int changed_flags,
801 unsigned int *new_flags,
802 int mc_count, struct dev_mc_list *mclist)
804 struct zd_mc_hash hash;
805 struct zd_mac *mac = zd_hw_mac(hw);
806 unsigned long flags;
807 int i;
809 /* Only deal with supported flags */
810 changed_flags &= SUPPORTED_FIF_FLAGS;
811 *new_flags &= SUPPORTED_FIF_FLAGS;
813 /* changed_flags is always populated but this driver
814 * doesn't support all FIF flags so its possible we don't
815 * need to do anything */
816 if (!changed_flags)
817 return;
819 if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI)) {
820 zd_mc_add_all(&hash);
821 } else {
822 DECLARE_MAC_BUF(macbuf);
824 zd_mc_clear(&hash);
825 for (i = 0; i < mc_count; i++) {
826 if (!mclist)
827 break;
828 dev_dbg_f(zd_mac_dev(mac), "mc addr %s\n",
829 print_mac(macbuf, mclist->dmi_addr));
830 zd_mc_add_addr(&hash, mclist->dmi_addr);
831 mclist = mclist->next;
835 spin_lock_irqsave(&mac->lock, flags);
836 mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
837 mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
838 mac->multicast_hash = hash;
839 spin_unlock_irqrestore(&mac->lock, flags);
840 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
842 if (changed_flags & FIF_CONTROL)
843 queue_work(zd_workqueue, &mac->set_rx_filter_work);
845 /* no handling required for FIF_OTHER_BSS as we don't currently
846 * do BSSID filtering */
847 /* FIXME: in future it would be nice to enable the probe response
848 * filter (so that the driver doesn't see them) until
849 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
850 * have to schedule work to enable prbresp reception, which might
851 * happen too late. For now we'll just listen and forward them all the
852 * time. */
855 static void set_rts_cts_work(struct work_struct *work)
857 struct zd_mac *mac =
858 container_of(work, struct zd_mac, set_rts_cts_work);
859 unsigned long flags;
860 unsigned int short_preamble;
862 mutex_lock(&mac->chip.mutex);
864 spin_lock_irqsave(&mac->lock, flags);
865 mac->updating_rts_rate = 0;
866 short_preamble = mac->short_preamble;
867 spin_unlock_irqrestore(&mac->lock, flags);
869 zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
870 mutex_unlock(&mac->chip.mutex);
873 static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
874 struct ieee80211_vif *vif,
875 struct ieee80211_bss_conf *bss_conf,
876 u32 changes)
878 struct zd_mac *mac = zd_hw_mac(hw);
879 unsigned long flags;
881 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
883 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
884 spin_lock_irqsave(&mac->lock, flags);
885 mac->short_preamble = bss_conf->use_short_preamble;
886 if (!mac->updating_rts_rate) {
887 mac->updating_rts_rate = 1;
888 /* FIXME: should disable TX here, until work has
889 * completed and RTS_CTS reg is updated */
890 queue_work(zd_workqueue, &mac->set_rts_cts_work);
892 spin_unlock_irqrestore(&mac->lock, flags);
896 static const struct ieee80211_ops zd_ops = {
897 .tx = zd_op_tx,
898 .start = zd_op_start,
899 .stop = zd_op_stop,
900 .add_interface = zd_op_add_interface,
901 .remove_interface = zd_op_remove_interface,
902 .config = zd_op_config,
903 .config_interface = zd_op_config_interface,
904 .configure_filter = zd_op_configure_filter,
905 .bss_info_changed = zd_op_bss_info_changed,
908 struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
910 struct zd_mac *mac;
911 struct ieee80211_hw *hw;
912 int i;
914 hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
915 if (!hw) {
916 dev_dbg_f(&intf->dev, "out of memory\n");
917 return NULL;
920 mac = zd_hw_mac(hw);
922 memset(mac, 0, sizeof(*mac));
923 spin_lock_init(&mac->lock);
924 mac->hw = hw;
926 mac->type = IEEE80211_IF_TYPE_INVALID;
928 memcpy(mac->channels, zd_channels, sizeof(zd_channels));
929 memcpy(mac->rates, zd_rates, sizeof(zd_rates));
930 mac->modes[0].mode = MODE_IEEE80211G;
931 mac->modes[0].num_rates = ARRAY_SIZE(zd_rates);
932 mac->modes[0].rates = mac->rates;
933 mac->modes[0].num_channels = ARRAY_SIZE(zd_channels);
934 mac->modes[0].channels = mac->channels;
935 mac->modes[1].mode = MODE_IEEE80211B;
936 mac->modes[1].num_rates = 4;
937 mac->modes[1].rates = mac->rates;
938 mac->modes[1].num_channels = ARRAY_SIZE(zd_channels);
939 mac->modes[1].channels = mac->channels;
941 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
942 IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED;
943 hw->max_rssi = 100;
944 hw->max_signal = 100;
946 hw->queues = 1;
947 hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
949 skb_queue_head_init(&mac->ack_wait_queue);
951 for (i = 0; i < 2; i++) {
952 if (ieee80211_register_hwmode(hw, &mac->modes[i])) {
953 dev_dbg_f(&intf->dev, "cannot register hwmode\n");
954 ieee80211_free_hw(hw);
955 return NULL;
959 zd_chip_init(&mac->chip, hw, intf);
960 housekeeping_init(mac);
961 INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
962 INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
963 INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler);
965 SET_IEEE80211_DEV(hw, &intf->dev);
966 return hw;
969 #define LINK_LED_WORK_DELAY HZ
971 static void link_led_handler(struct work_struct *work)
973 struct zd_mac *mac =
974 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
975 struct zd_chip *chip = &mac->chip;
976 int is_associated;
977 int r;
979 spin_lock_irq(&mac->lock);
980 is_associated = mac->associated;
981 spin_unlock_irq(&mac->lock);
983 r = zd_chip_control_leds(chip,
984 is_associated ? LED_ASSOCIATED : LED_SCANNING);
985 if (r)
986 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
988 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
989 LINK_LED_WORK_DELAY);
992 static void housekeeping_init(struct zd_mac *mac)
994 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
997 static void housekeeping_enable(struct zd_mac *mac)
999 dev_dbg_f(zd_mac_dev(mac), "\n");
1000 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1004 static void housekeeping_disable(struct zd_mac *mac)
1006 dev_dbg_f(zd_mac_dev(mac), "\n");
1007 cancel_rearming_delayed_workqueue(zd_workqueue,
1008 &mac->housekeeping.link_led_work);
1009 zd_chip_control_leds(&mac->chip, LED_OFF);