Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/linville/wireles...
[safe/jmp/linux-2.6] / drivers / net / wireless / zd1211rw / zd_mac.c
1 /* ZD1211 USB-WLAN driver for Linux
2  *
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>
7  *
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.
12  *
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.
17  *
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
21  */
22
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>
28
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"
34
35 /* This table contains the hardware specific values for the modulation rates. */
36 static const struct ieee80211_rate zd_rates[] = {
37         { .bitrate = 10,
38           .hw_value = ZD_CCK_RATE_1M, },
39         { .bitrate = 20,
40           .hw_value = ZD_CCK_RATE_2M,
41           .hw_value_short = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
42           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
43         { .bitrate = 55,
44           .hw_value = ZD_CCK_RATE_5_5M,
45           .hw_value_short = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
46           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
47         { .bitrate = 110,
48           .hw_value = ZD_CCK_RATE_11M,
49           .hw_value_short = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
50           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
51         { .bitrate = 60,
52           .hw_value = ZD_OFDM_RATE_6M,
53           .flags = 0 },
54         { .bitrate = 90,
55           .hw_value = ZD_OFDM_RATE_9M,
56           .flags = 0 },
57         { .bitrate = 120,
58           .hw_value = ZD_OFDM_RATE_12M,
59           .flags = 0 },
60         { .bitrate = 180,
61           .hw_value = ZD_OFDM_RATE_18M,
62           .flags = 0 },
63         { .bitrate = 240,
64           .hw_value = ZD_OFDM_RATE_24M,
65           .flags = 0 },
66         { .bitrate = 360,
67           .hw_value = ZD_OFDM_RATE_36M,
68           .flags = 0 },
69         { .bitrate = 480,
70           .hw_value = ZD_OFDM_RATE_48M,
71           .flags = 0 },
72         { .bitrate = 540,
73           .hw_value = ZD_OFDM_RATE_54M,
74           .flags = 0 },
75 };
76
77 static const struct ieee80211_channel zd_channels[] = {
78         { .center_freq = 2412, .hw_value = 1 },
79         { .center_freq = 2417, .hw_value = 2 },
80         { .center_freq = 2422, .hw_value = 3 },
81         { .center_freq = 2427, .hw_value = 4 },
82         { .center_freq = 2432, .hw_value = 5 },
83         { .center_freq = 2437, .hw_value = 6 },
84         { .center_freq = 2442, .hw_value = 7 },
85         { .center_freq = 2447, .hw_value = 8 },
86         { .center_freq = 2452, .hw_value = 9 },
87         { .center_freq = 2457, .hw_value = 10 },
88         { .center_freq = 2462, .hw_value = 11 },
89         { .center_freq = 2467, .hw_value = 12 },
90         { .center_freq = 2472, .hw_value = 13 },
91         { .center_freq = 2484, .hw_value = 14 },
92 };
93
94 static void housekeeping_init(struct zd_mac *mac);
95 static void housekeeping_enable(struct zd_mac *mac);
96 static void housekeeping_disable(struct zd_mac *mac);
97
98 int zd_mac_preinit_hw(struct ieee80211_hw *hw)
99 {
100         int r;
101         u8 addr[ETH_ALEN];
102         struct zd_mac *mac = zd_hw_mac(hw);
103
104         r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
105         if (r)
106                 return r;
107
108         SET_IEEE80211_PERM_ADDR(hw, addr);
109
110         return 0;
111 }
112
113 int zd_mac_init_hw(struct ieee80211_hw *hw)
114 {
115         int r;
116         struct zd_mac *mac = zd_hw_mac(hw);
117         struct zd_chip *chip = &mac->chip;
118         u8 default_regdomain;
119
120         r = zd_chip_enable_int(chip);
121         if (r)
122                 goto out;
123         r = zd_chip_init_hw(chip);
124         if (r)
125                 goto disable_int;
126
127         ZD_ASSERT(!irqs_disabled());
128
129         r = zd_read_regdomain(chip, &default_regdomain);
130         if (r)
131                 goto disable_int;
132         spin_lock_irq(&mac->lock);
133         mac->regdomain = mac->default_regdomain = default_regdomain;
134         spin_unlock_irq(&mac->lock);
135
136         /* We must inform the device that we are doing encryption/decryption in
137          * software at the moment. */
138         r = zd_set_encryption_type(chip, ENC_SNIFFER);
139         if (r)
140                 goto disable_int;
141
142         zd_geo_init(hw, mac->regdomain);
143
144         r = 0;
145 disable_int:
146         zd_chip_disable_int(chip);
147 out:
148         return r;
149 }
150
151 void zd_mac_clear(struct zd_mac *mac)
152 {
153         flush_workqueue(zd_workqueue);
154         zd_chip_clear(&mac->chip);
155         ZD_ASSERT(!spin_is_locked(&mac->lock));
156         ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
157 }
158
159 static int set_rx_filter(struct zd_mac *mac)
160 {
161         unsigned long flags;
162         u32 filter = STA_RX_FILTER;
163
164         spin_lock_irqsave(&mac->lock, flags);
165         if (mac->pass_ctrl)
166                 filter |= RX_FILTER_CTRL;
167         spin_unlock_irqrestore(&mac->lock, flags);
168
169         return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
170 }
171
172 static int set_mc_hash(struct zd_mac *mac)
173 {
174         struct zd_mc_hash hash;
175         zd_mc_clear(&hash);
176         return zd_chip_set_multicast_hash(&mac->chip, &hash);
177 }
178
179 static int zd_op_start(struct ieee80211_hw *hw)
180 {
181         struct zd_mac *mac = zd_hw_mac(hw);
182         struct zd_chip *chip = &mac->chip;
183         struct zd_usb *usb = &chip->usb;
184         int r;
185
186         if (!usb->initialized) {
187                 r = zd_usb_init_hw(usb);
188                 if (r)
189                         goto out;
190         }
191
192         r = zd_chip_enable_int(chip);
193         if (r < 0)
194                 goto out;
195
196         r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
197         if (r < 0)
198                 goto disable_int;
199         r = set_rx_filter(mac);
200         if (r)
201                 goto disable_int;
202         r = set_mc_hash(mac);
203         if (r)
204                 goto disable_int;
205         r = zd_chip_switch_radio_on(chip);
206         if (r < 0)
207                 goto disable_int;
208         r = zd_chip_enable_rxtx(chip);
209         if (r < 0)
210                 goto disable_radio;
211         r = zd_chip_enable_hwint(chip);
212         if (r < 0)
213                 goto disable_rxtx;
214
215         housekeeping_enable(mac);
216         return 0;
217 disable_rxtx:
218         zd_chip_disable_rxtx(chip);
219 disable_radio:
220         zd_chip_switch_radio_off(chip);
221 disable_int:
222         zd_chip_disable_int(chip);
223 out:
224         return r;
225 }
226
227 static void zd_op_stop(struct ieee80211_hw *hw)
228 {
229         struct zd_mac *mac = zd_hw_mac(hw);
230         struct zd_chip *chip = &mac->chip;
231         struct sk_buff *skb;
232         struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
233
234         /* The order here deliberately is a little different from the open()
235          * method, since we need to make sure there is no opportunity for RX
236          * frames to be processed by mac80211 after we have stopped it.
237          */
238
239         zd_chip_disable_rxtx(chip);
240         housekeeping_disable(mac);
241         flush_workqueue(zd_workqueue);
242
243         zd_chip_disable_hwint(chip);
244         zd_chip_switch_radio_off(chip);
245         zd_chip_disable_int(chip);
246
247
248         while ((skb = skb_dequeue(ack_wait_queue)))
249                 dev_kfree_skb_any(skb);
250 }
251
252 /**
253  * tx_status - reports tx status of a packet if required
254  * @hw - a &struct ieee80211_hw pointer
255  * @skb - a sk-buffer
256  * @flags: extra flags to set in the TX status info
257  * @ackssi: ACK signal strength
258  * @success - True for successfull transmission of the frame
259  *
260  * This information calls ieee80211_tx_status_irqsafe() if required by the
261  * control information. It copies the control information into the status
262  * information.
263  *
264  * If no status information has been requested, the skb is freed.
265  */
266 static void tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
267                       u32 flags, int ackssi, bool success)
268 {
269         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
270
271         memset(&info->status, 0, sizeof(info->status));
272
273         if (!success)
274                 info->status.excessive_retries = 1;
275         info->flags |= flags;
276         info->status.ack_signal = ackssi;
277         ieee80211_tx_status_irqsafe(hw, skb);
278 }
279
280 /**
281  * zd_mac_tx_failed - callback for failed frames
282  * @dev: the mac80211 wireless device
283  *
284  * This function is called if a frame couldn't be succesfully be
285  * transferred. The first frame from the tx queue, will be selected and
286  * reported as error to the upper layers.
287  */
288 void zd_mac_tx_failed(struct ieee80211_hw *hw)
289 {
290         struct sk_buff_head *q = &zd_hw_mac(hw)->ack_wait_queue;
291         struct sk_buff *skb;
292
293         skb = skb_dequeue(q);
294         if (skb == NULL)
295                 return;
296
297         tx_status(hw, skb, 0, 0, 0);
298 }
299
300 /**
301  * zd_mac_tx_to_dev - callback for USB layer
302  * @skb: a &sk_buff pointer
303  * @error: error value, 0 if transmission successful
304  *
305  * Informs the MAC layer that the frame has successfully transferred to the
306  * device. If an ACK is required and the transfer to the device has been
307  * successful, the packets are put on the @ack_wait_queue with
308  * the control set removed.
309  */
310 void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
311 {
312         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
313         struct ieee80211_hw *hw = info->driver_data[0];
314
315         skb_pull(skb, sizeof(struct zd_ctrlset));
316         if (unlikely(error ||
317             (info->flags & IEEE80211_TX_CTL_NO_ACK))) {
318                 tx_status(hw, skb, 0, 0, !error);
319         } else {
320                 struct sk_buff_head *q =
321                         &zd_hw_mac(hw)->ack_wait_queue;
322
323                 skb_queue_tail(q, skb);
324                 while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS)
325                         zd_mac_tx_failed(hw);
326         }
327 }
328
329 static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
330 {
331         /* ZD_PURE_RATE() must be used to remove the modulation type flag of
332          * the zd-rate values.
333          */
334         static const u8 rate_divisor[] = {
335                 [ZD_PURE_RATE(ZD_CCK_RATE_1M)]   =  1,
336                 [ZD_PURE_RATE(ZD_CCK_RATE_2M)]   =  2,
337                 /* Bits must be doubled. */
338                 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
339                 [ZD_PURE_RATE(ZD_CCK_RATE_11M)]  = 11,
340                 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)]  =  6,
341                 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)]  =  9,
342                 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
343                 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
344                 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
345                 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
346                 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
347                 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
348         };
349
350         u32 bits = (u32)tx_length * 8;
351         u32 divisor;
352
353         divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
354         if (divisor == 0)
355                 return -EINVAL;
356
357         switch (zd_rate) {
358         case ZD_CCK_RATE_5_5M:
359                 bits = (2*bits) + 10; /* round up to the next integer */
360                 break;
361         case ZD_CCK_RATE_11M:
362                 if (service) {
363                         u32 t = bits % 11;
364                         *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
365                         if (0 < t && t <= 3) {
366                                 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
367                         }
368                 }
369                 bits += 10; /* round up to the next integer */
370                 break;
371         }
372
373         return bits/divisor;
374 }
375
376 static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
377                            struct ieee80211_hdr *header, u32 flags)
378 {
379         u16 fctl = le16_to_cpu(header->frame_control);
380
381         /*
382          * CONTROL TODO:
383          * - if backoff needed, enable bit 0
384          * - if burst (backoff not needed) disable bit 0
385          */
386
387         cs->control = 0;
388
389         /* First fragment */
390         if (flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
391                 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
392
393         /* Multicast */
394         if (is_multicast_ether_addr(header->addr1))
395                 cs->control |= ZD_CS_MULTICAST;
396
397         /* PS-POLL */
398         if ((fctl & (IEEE80211_FCTL_FTYPE|IEEE80211_FCTL_STYPE)) ==
399             (IEEE80211_FTYPE_CTL|IEEE80211_STYPE_PSPOLL))
400                 cs->control |= ZD_CS_PS_POLL_FRAME;
401
402         if (flags & IEEE80211_TX_CTL_USE_RTS_CTS)
403                 cs->control |= ZD_CS_RTS;
404
405         if (flags & IEEE80211_TX_CTL_USE_CTS_PROTECT)
406                 cs->control |= ZD_CS_SELF_CTS;
407
408         /* FIXME: Management frame? */
409 }
410
411 void zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon)
412 {
413         struct zd_mac *mac = zd_hw_mac(hw);
414         u32 tmp, j = 0;
415         /* 4 more bytes for tail CRC */
416         u32 full_len = beacon->len + 4;
417         zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 0);
418         zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
419         while (tmp & 0x2) {
420                 zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
421                 if ((++j % 100) == 0) {
422                         printk(KERN_ERR "CR_BCN_FIFO_SEMAPHORE not ready\n");
423                         if (j >= 500)  {
424                                 printk(KERN_ERR "Giving up beacon config.\n");
425                                 return;
426                         }
427                 }
428                 msleep(1);
429         }
430
431         zd_iowrite32(&mac->chip, CR_BCN_FIFO, full_len - 1);
432         if (zd_chip_is_zd1211b(&mac->chip))
433                 zd_iowrite32(&mac->chip, CR_BCN_LENGTH, full_len - 1);
434
435         for (j = 0 ; j < beacon->len; j++)
436                 zd_iowrite32(&mac->chip, CR_BCN_FIFO,
437                                 *((u8 *)(beacon->data + j)));
438
439         for (j = 0; j < 4; j++)
440                 zd_iowrite32(&mac->chip, CR_BCN_FIFO, 0x0);
441
442         zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 1);
443         /* 802.11b/g 2.4G CCK 1Mb
444          * 802.11a, not yet implemented, uses different values (see GPL vendor
445          * driver)
446          */
447         zd_iowrite32(&mac->chip, CR_BCN_PLCP_CFG, 0x00000400 |
448                         (full_len << 19));
449 }
450
451 static int fill_ctrlset(struct zd_mac *mac,
452                         struct sk_buff *skb)
453 {
454         int r;
455         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
456         unsigned int frag_len = skb->len + FCS_LEN;
457         unsigned int packet_length;
458         struct ieee80211_rate *txrate;
459         struct zd_ctrlset *cs = (struct zd_ctrlset *)
460                 skb_push(skb, sizeof(struct zd_ctrlset));
461         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
462
463         ZD_ASSERT(frag_len <= 0xffff);
464
465         txrate = ieee80211_get_tx_rate(mac->hw, info);
466
467         cs->modulation = txrate->hw_value;
468         if (info->flags & IEEE80211_TX_CTL_SHORT_PREAMBLE)
469                 cs->modulation = txrate->hw_value_short;
470
471         cs->tx_length = cpu_to_le16(frag_len);
472
473         cs_set_control(mac, cs, hdr, info->flags);
474
475         packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
476         ZD_ASSERT(packet_length <= 0xffff);
477         /* ZD1211B: Computing the length difference this way, gives us
478          * flexibility to compute the packet length.
479          */
480         cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
481                         packet_length - frag_len : packet_length);
482
483         /*
484          * CURRENT LENGTH:
485          * - transmit frame length in microseconds
486          * - seems to be derived from frame length
487          * - see Cal_Us_Service() in zdinlinef.h
488          * - if macp->bTxBurstEnable is enabled, then multiply by 4
489          *  - bTxBurstEnable is never set in the vendor driver
490          *
491          * SERVICE:
492          * - "for PLCP configuration"
493          * - always 0 except in some situations at 802.11b 11M
494          * - see line 53 of zdinlinef.h
495          */
496         cs->service = 0;
497         r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
498                                  le16_to_cpu(cs->tx_length));
499         if (r < 0)
500                 return r;
501         cs->current_length = cpu_to_le16(r);
502         cs->next_frame_length = 0;
503
504         return 0;
505 }
506
507 /**
508  * zd_op_tx - transmits a network frame to the device
509  *
510  * @dev: mac80211 hardware device
511  * @skb: socket buffer
512  * @control: the control structure
513  *
514  * This function transmit an IEEE 802.11 network frame to the device. The
515  * control block of the skbuff will be initialized. If necessary the incoming
516  * mac80211 queues will be stopped.
517  */
518 static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
519 {
520         struct zd_mac *mac = zd_hw_mac(hw);
521         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
522         int r;
523
524         r = fill_ctrlset(mac, skb);
525         if (r)
526                 return r;
527
528         info->driver_data[0] = hw;
529
530         r = zd_usb_tx(&mac->chip.usb, skb);
531         if (r)
532                 return r;
533         return 0;
534 }
535
536 /**
537  * filter_ack - filters incoming packets for acknowledgements
538  * @dev: the mac80211 device
539  * @rx_hdr: received header
540  * @stats: the status for the received packet
541  *
542  * This functions looks for ACK packets and tries to match them with the
543  * frames in the tx queue. If a match is found the frame will be dequeued and
544  * the upper layers is informed about the successful transmission. If
545  * mac80211 queues have been stopped and the number of frames still to be
546  * transmitted is low the queues will be opened again.
547  *
548  * Returns 1 if the frame was an ACK, 0 if it was ignored.
549  */
550 static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
551                       struct ieee80211_rx_status *stats)
552 {
553         u16 fc = le16_to_cpu(rx_hdr->frame_control);
554         struct sk_buff *skb;
555         struct sk_buff_head *q;
556         unsigned long flags;
557
558         if ((fc & (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) !=
559             (IEEE80211_FTYPE_CTL | IEEE80211_STYPE_ACK))
560                 return 0;
561
562         q = &zd_hw_mac(hw)->ack_wait_queue;
563         spin_lock_irqsave(&q->lock, flags);
564         for (skb = q->next; skb != (struct sk_buff *)q; skb = skb->next) {
565                 struct ieee80211_hdr *tx_hdr;
566
567                 tx_hdr = (struct ieee80211_hdr *)skb->data;
568                 if (likely(!compare_ether_addr(tx_hdr->addr2, rx_hdr->addr1)))
569                 {
570                         __skb_unlink(skb, q);
571                         tx_status(hw, skb, IEEE80211_TX_STAT_ACK, stats->signal, 1);
572                         goto out;
573                 }
574         }
575 out:
576         spin_unlock_irqrestore(&q->lock, flags);
577         return 1;
578 }
579
580 int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
581 {
582         struct zd_mac *mac = zd_hw_mac(hw);
583         struct ieee80211_rx_status stats;
584         const struct rx_status *status;
585         struct sk_buff *skb;
586         int bad_frame = 0;
587         u16 fc;
588         bool is_qos, is_4addr, need_padding;
589         int i;
590         u8 rate;
591
592         if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
593                      FCS_LEN + sizeof(struct rx_status))
594                 return -EINVAL;
595
596         memset(&stats, 0, sizeof(stats));
597
598         /* Note about pass_failed_fcs and pass_ctrl access below:
599          * mac locking intentionally omitted here, as this is the only unlocked
600          * reader and the only writer is configure_filter. Plus, if there were
601          * any races accessing these variables, it wouldn't really matter.
602          * If mac80211 ever provides a way for us to access filter flags
603          * from outside configure_filter, we could improve on this. Also, this
604          * situation may change once we implement some kind of DMA-into-skb
605          * RX path. */
606
607         /* Caller has to ensure that length >= sizeof(struct rx_status). */
608         status = (struct rx_status *)
609                 (buffer + (length - sizeof(struct rx_status)));
610         if (status->frame_status & ZD_RX_ERROR) {
611                 if (mac->pass_failed_fcs &&
612                                 (status->frame_status & ZD_RX_CRC32_ERROR)) {
613                         stats.flag |= RX_FLAG_FAILED_FCS_CRC;
614                         bad_frame = 1;
615                 } else {
616                         return -EINVAL;
617                 }
618         }
619
620         stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
621         stats.band = IEEE80211_BAND_2GHZ;
622         stats.signal = status->signal_strength;
623         stats.qual = zd_rx_qual_percent(buffer,
624                                           length - sizeof(struct rx_status),
625                                           status);
626
627         rate = zd_rx_rate(buffer, status);
628
629         /* todo: return index in the big switches in zd_rx_rate instead */
630         for (i = 0; i < mac->band.n_bitrates; i++)
631                 if (rate == mac->band.bitrates[i].hw_value)
632                         stats.rate_idx = i;
633
634         length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
635         buffer += ZD_PLCP_HEADER_SIZE;
636
637         /* Except for bad frames, filter each frame to see if it is an ACK, in
638          * which case our internal TX tracking is updated. Normally we then
639          * bail here as there's no need to pass ACKs on up to the stack, but
640          * there is also the case where the stack has requested us to pass
641          * control frames on up (pass_ctrl) which we must consider. */
642         if (!bad_frame &&
643                         filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
644                         && !mac->pass_ctrl)
645                 return 0;
646
647         fc = le16_to_cpu(*((__le16 *) buffer));
648
649         is_qos = ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
650                  ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_QOS_DATA);
651         is_4addr = (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
652                    (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS);
653         need_padding = is_qos ^ is_4addr;
654
655         skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
656         if (skb == NULL)
657                 return -ENOMEM;
658         if (need_padding) {
659                 /* Make sure the the payload data is 4 byte aligned. */
660                 skb_reserve(skb, 2);
661         }
662
663         memcpy(skb_put(skb, length), buffer, length);
664
665         ieee80211_rx_irqsafe(hw, skb, &stats);
666         return 0;
667 }
668
669 static int zd_op_add_interface(struct ieee80211_hw *hw,
670                                 struct ieee80211_if_init_conf *conf)
671 {
672         struct zd_mac *mac = zd_hw_mac(hw);
673
674         /* using IEEE80211_IF_TYPE_INVALID to indicate no mode selected */
675         if (mac->type != IEEE80211_IF_TYPE_INVALID)
676                 return -EOPNOTSUPP;
677
678         switch (conf->type) {
679         case IEEE80211_IF_TYPE_MNTR:
680         case IEEE80211_IF_TYPE_MESH_POINT:
681         case IEEE80211_IF_TYPE_STA:
682         case IEEE80211_IF_TYPE_IBSS:
683                 mac->type = conf->type;
684                 break;
685         default:
686                 return -EOPNOTSUPP;
687         }
688
689         return zd_write_mac_addr(&mac->chip, conf->mac_addr);
690 }
691
692 static void zd_op_remove_interface(struct ieee80211_hw *hw,
693                                     struct ieee80211_if_init_conf *conf)
694 {
695         struct zd_mac *mac = zd_hw_mac(hw);
696         mac->type = IEEE80211_IF_TYPE_INVALID;
697         zd_write_mac_addr(&mac->chip, NULL);
698 }
699
700 static int zd_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
701 {
702         struct zd_mac *mac = zd_hw_mac(hw);
703         return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
704 }
705
706 static int zd_op_config_interface(struct ieee80211_hw *hw,
707                                   struct ieee80211_vif *vif,
708                                    struct ieee80211_if_conf *conf)
709 {
710         struct zd_mac *mac = zd_hw_mac(hw);
711         int associated;
712
713         if (mac->type == IEEE80211_IF_TYPE_MESH_POINT ||
714             mac->type == IEEE80211_IF_TYPE_IBSS) {
715                 associated = true;
716                 if (conf->beacon) {
717                         zd_mac_config_beacon(hw, conf->beacon);
718                         kfree_skb(conf->beacon);
719                         zd_set_beacon_interval(&mac->chip, BCN_MODE_IBSS |
720                                         hw->conf.beacon_int);
721                 }
722         } else
723                 associated = is_valid_ether_addr(conf->bssid);
724
725         spin_lock_irq(&mac->lock);
726         mac->associated = associated;
727         spin_unlock_irq(&mac->lock);
728
729         /* TODO: do hardware bssid filtering */
730         return 0;
731 }
732
733 void zd_process_intr(struct work_struct *work)
734 {
735         u16 int_status;
736         struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
737
738         int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer+4));
739         if (int_status & INT_CFG_NEXT_BCN) {
740                 if (net_ratelimit())
741                         dev_dbg_f(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");
742         } else
743                 dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
744
745         zd_chip_enable_hwint(&mac->chip);
746 }
747
748
749 static void set_multicast_hash_handler(struct work_struct *work)
750 {
751         struct zd_mac *mac =
752                 container_of(work, struct zd_mac, set_multicast_hash_work);
753         struct zd_mc_hash hash;
754
755         spin_lock_irq(&mac->lock);
756         hash = mac->multicast_hash;
757         spin_unlock_irq(&mac->lock);
758
759         zd_chip_set_multicast_hash(&mac->chip, &hash);
760 }
761
762 static void set_rx_filter_handler(struct work_struct *work)
763 {
764         struct zd_mac *mac =
765                 container_of(work, struct zd_mac, set_rx_filter_work);
766         int r;
767
768         dev_dbg_f(zd_mac_dev(mac), "\n");
769         r = set_rx_filter(mac);
770         if (r)
771                 dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r);
772 }
773
774 #define SUPPORTED_FIF_FLAGS \
775         (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
776         FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
777 static void zd_op_configure_filter(struct ieee80211_hw *hw,
778                         unsigned int changed_flags,
779                         unsigned int *new_flags,
780                         int mc_count, struct dev_mc_list *mclist)
781 {
782         struct zd_mc_hash hash;
783         struct zd_mac *mac = zd_hw_mac(hw);
784         unsigned long flags;
785         int i;
786
787         /* Only deal with supported flags */
788         changed_flags &= SUPPORTED_FIF_FLAGS;
789         *new_flags &= SUPPORTED_FIF_FLAGS;
790
791         /* changed_flags is always populated but this driver
792          * doesn't support all FIF flags so its possible we don't
793          * need to do anything */
794         if (!changed_flags)
795                 return;
796
797         if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI)) {
798                 zd_mc_add_all(&hash);
799         } else {
800                 DECLARE_MAC_BUF(macbuf);
801
802                 zd_mc_clear(&hash);
803                 for (i = 0; i < mc_count; i++) {
804                         if (!mclist)
805                                 break;
806                         dev_dbg_f(zd_mac_dev(mac), "mc addr %s\n",
807                                   print_mac(macbuf, mclist->dmi_addr));
808                         zd_mc_add_addr(&hash, mclist->dmi_addr);
809                         mclist = mclist->next;
810                 }
811         }
812
813         spin_lock_irqsave(&mac->lock, flags);
814         mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
815         mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
816         mac->multicast_hash = hash;
817         spin_unlock_irqrestore(&mac->lock, flags);
818         queue_work(zd_workqueue, &mac->set_multicast_hash_work);
819
820         if (changed_flags & FIF_CONTROL)
821                 queue_work(zd_workqueue, &mac->set_rx_filter_work);
822
823         /* no handling required for FIF_OTHER_BSS as we don't currently
824          * do BSSID filtering */
825         /* FIXME: in future it would be nice to enable the probe response
826          * filter (so that the driver doesn't see them) until
827          * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
828          * have to schedule work to enable prbresp reception, which might
829          * happen too late. For now we'll just listen and forward them all the
830          * time. */
831 }
832
833 static void set_rts_cts_work(struct work_struct *work)
834 {
835         struct zd_mac *mac =
836                 container_of(work, struct zd_mac, set_rts_cts_work);
837         unsigned long flags;
838         unsigned int short_preamble;
839
840         mutex_lock(&mac->chip.mutex);
841
842         spin_lock_irqsave(&mac->lock, flags);
843         mac->updating_rts_rate = 0;
844         short_preamble = mac->short_preamble;
845         spin_unlock_irqrestore(&mac->lock, flags);
846
847         zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
848         mutex_unlock(&mac->chip.mutex);
849 }
850
851 static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
852                                    struct ieee80211_vif *vif,
853                                    struct ieee80211_bss_conf *bss_conf,
854                                    u32 changes)
855 {
856         struct zd_mac *mac = zd_hw_mac(hw);
857         unsigned long flags;
858
859         dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
860
861         if (changes & BSS_CHANGED_ERP_PREAMBLE) {
862                 spin_lock_irqsave(&mac->lock, flags);
863                 mac->short_preamble = bss_conf->use_short_preamble;
864                 if (!mac->updating_rts_rate) {
865                         mac->updating_rts_rate = 1;
866                         /* FIXME: should disable TX here, until work has
867                          * completed and RTS_CTS reg is updated */
868                         queue_work(zd_workqueue, &mac->set_rts_cts_work);
869                 }
870                 spin_unlock_irqrestore(&mac->lock, flags);
871         }
872 }
873
874 static int zd_op_beacon_update(struct ieee80211_hw *hw,
875                                struct sk_buff *skb)
876 {
877         struct zd_mac *mac = zd_hw_mac(hw);
878         zd_mac_config_beacon(hw, skb);
879         kfree_skb(skb);
880         zd_set_beacon_interval(&mac->chip, BCN_MODE_IBSS |
881                                         hw->conf.beacon_int);
882         return 0;
883 }
884
885 static const struct ieee80211_ops zd_ops = {
886         .tx                     = zd_op_tx,
887         .start                  = zd_op_start,
888         .stop                   = zd_op_stop,
889         .add_interface          = zd_op_add_interface,
890         .remove_interface       = zd_op_remove_interface,
891         .config                 = zd_op_config,
892         .config_interface       = zd_op_config_interface,
893         .configure_filter       = zd_op_configure_filter,
894         .bss_info_changed       = zd_op_bss_info_changed,
895         .beacon_update          = zd_op_beacon_update,
896 };
897
898 struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
899 {
900         struct zd_mac *mac;
901         struct ieee80211_hw *hw;
902
903         hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
904         if (!hw) {
905                 dev_dbg_f(&intf->dev, "out of memory\n");
906                 return NULL;
907         }
908
909         mac = zd_hw_mac(hw);
910
911         memset(mac, 0, sizeof(*mac));
912         spin_lock_init(&mac->lock);
913         mac->hw = hw;
914
915         mac->type = IEEE80211_IF_TYPE_INVALID;
916
917         memcpy(mac->channels, zd_channels, sizeof(zd_channels));
918         memcpy(mac->rates, zd_rates, sizeof(zd_rates));
919         mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
920         mac->band.bitrates = mac->rates;
921         mac->band.n_channels = ARRAY_SIZE(zd_channels);
922         mac->band.channels = mac->channels;
923
924         hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
925
926         hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
927                     IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE |
928                     IEEE80211_HW_SIGNAL_DB;
929
930         hw->max_signal = 100;
931         hw->queues = 1;
932         hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
933
934         skb_queue_head_init(&mac->ack_wait_queue);
935
936         zd_chip_init(&mac->chip, hw, intf);
937         housekeeping_init(mac);
938         INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
939         INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
940         INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler);
941         INIT_WORK(&mac->process_intr, zd_process_intr);
942
943         SET_IEEE80211_DEV(hw, &intf->dev);
944         return hw;
945 }
946
947 #define LINK_LED_WORK_DELAY HZ
948
949 static void link_led_handler(struct work_struct *work)
950 {
951         struct zd_mac *mac =
952                 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
953         struct zd_chip *chip = &mac->chip;
954         int is_associated;
955         int r;
956
957         spin_lock_irq(&mac->lock);
958         is_associated = mac->associated;
959         spin_unlock_irq(&mac->lock);
960
961         r = zd_chip_control_leds(chip,
962                                  is_associated ? LED_ASSOCIATED : LED_SCANNING);
963         if (r)
964                 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
965
966         queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
967                            LINK_LED_WORK_DELAY);
968 }
969
970 static void housekeeping_init(struct zd_mac *mac)
971 {
972         INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
973 }
974
975 static void housekeeping_enable(struct zd_mac *mac)
976 {
977         dev_dbg_f(zd_mac_dev(mac), "\n");
978         queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
979                            0);
980 }
981
982 static void housekeeping_disable(struct zd_mac *mac)
983 {
984         dev_dbg_f(zd_mac_dev(mac), "\n");
985         cancel_rearming_delayed_workqueue(zd_workqueue,
986                 &mac->housekeeping.link_led_work);
987         zd_chip_control_leds(&mac->chip, LED_OFF);
988 }