zd1211rw: Fix beacon filter flags thinko
[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 /**
228  * clear_tx_skb_control_block - clears the control block of tx skbuffs
229  * @skb: a &struct sk_buff pointer
230  *
231  * This clears the control block of skbuff buffers, which were transmitted to
232  * the device. Notify that the function is not thread-safe, so prevent
233  * multiple calls.
234  */
235 static void clear_tx_skb_control_block(struct sk_buff *skb)
236 {
237         struct zd_tx_skb_control_block *cb =
238                 (struct zd_tx_skb_control_block *)skb->cb;
239
240         kfree(cb->control);
241         cb->control = NULL;
242 }
243
244 /**
245  * kfree_tx_skb - frees a tx skbuff
246  * @skb: a &struct sk_buff pointer
247  *
248  * Frees the tx skbuff. Frees also the allocated control structure in the
249  * control block if necessary.
250  */
251 static void kfree_tx_skb(struct sk_buff *skb)
252 {
253         clear_tx_skb_control_block(skb);
254         dev_kfree_skb_any(skb);
255 }
256
257 static void zd_op_stop(struct ieee80211_hw *hw)
258 {
259         struct zd_mac *mac = zd_hw_mac(hw);
260         struct zd_chip *chip = &mac->chip;
261         struct sk_buff *skb;
262         struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
263
264         /* The order here deliberately is a little different from the open()
265          * method, since we need to make sure there is no opportunity for RX
266          * frames to be processed by mac80211 after we have stopped it.
267          */
268
269         zd_chip_disable_rxtx(chip);
270         housekeeping_disable(mac);
271         flush_workqueue(zd_workqueue);
272
273         zd_chip_disable_hwint(chip);
274         zd_chip_switch_radio_off(chip);
275         zd_chip_disable_int(chip);
276
277
278         while ((skb = skb_dequeue(ack_wait_queue)))
279                 kfree_tx_skb(skb);
280 }
281
282 /**
283  * init_tx_skb_control_block - initializes skb control block
284  * @skb: a &sk_buff pointer
285  * @dev: pointer to the mac80221 device
286  * @control: mac80211 tx control applying for the frame in @skb
287  *
288  * Initializes the control block of the skbuff to be transmitted.
289  */
290 static int init_tx_skb_control_block(struct sk_buff *skb,
291                                      struct ieee80211_hw *hw,
292                                      struct ieee80211_tx_control *control)
293 {
294         struct zd_tx_skb_control_block *cb =
295                 (struct zd_tx_skb_control_block *)skb->cb;
296
297         ZD_ASSERT(sizeof(*cb) <= sizeof(skb->cb));
298         memset(cb, 0, sizeof(*cb));
299         cb->hw= hw;
300         cb->control = kmalloc(sizeof(*control), GFP_ATOMIC);
301         if (cb->control == NULL)
302                 return -ENOMEM;
303         memcpy(cb->control, control, sizeof(*control));
304
305         return 0;
306 }
307
308 /**
309  * tx_status - reports tx status of a packet if required
310  * @hw - a &struct ieee80211_hw pointer
311  * @skb - a sk-buffer
312  * @status - the tx status of the packet without control information
313  * @success - True for successfull transmission of the frame
314  *
315  * This information calls ieee80211_tx_status_irqsafe() if required by the
316  * control information. It copies the control information into the status
317  * information.
318  *
319  * If no status information has been requested, the skb is freed.
320  */
321 static void tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
322                       struct ieee80211_tx_status *status,
323                       bool success)
324 {
325         struct zd_tx_skb_control_block *cb = (struct zd_tx_skb_control_block *)
326                 skb->cb;
327
328         ZD_ASSERT(cb->control != NULL);
329         memcpy(&status->control, cb->control, sizeof(status->control));
330         if (!success)
331                 status->excessive_retries = 1;
332         clear_tx_skb_control_block(skb);
333         ieee80211_tx_status_irqsafe(hw, skb, status);
334 }
335
336 /**
337  * zd_mac_tx_failed - callback for failed frames
338  * @dev: the mac80211 wireless device
339  *
340  * This function is called if a frame couldn't be succesfully be
341  * transferred. The first frame from the tx queue, will be selected and
342  * reported as error to the upper layers.
343  */
344 void zd_mac_tx_failed(struct ieee80211_hw *hw)
345 {
346         struct sk_buff_head *q = &zd_hw_mac(hw)->ack_wait_queue;
347         struct sk_buff *skb;
348         struct ieee80211_tx_status status;
349
350         skb = skb_dequeue(q);
351         if (skb == NULL)
352                 return;
353
354         memset(&status, 0, sizeof(status));
355
356         tx_status(hw, skb, &status, 0);
357 }
358
359 /**
360  * zd_mac_tx_to_dev - callback for USB layer
361  * @skb: a &sk_buff pointer
362  * @error: error value, 0 if transmission successful
363  *
364  * Informs the MAC layer that the frame has successfully transferred to the
365  * device. If an ACK is required and the transfer to the device has been
366  * successful, the packets are put on the @ack_wait_queue with
367  * the control set removed.
368  */
369 void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
370 {
371         struct zd_tx_skb_control_block *cb =
372                 (struct zd_tx_skb_control_block *)skb->cb;
373         struct ieee80211_hw *hw = cb->hw;
374
375         if (likely(cb->control)) {
376                 skb_pull(skb, sizeof(struct zd_ctrlset));
377                 if (unlikely(error ||
378                     (cb->control->flags & IEEE80211_TXCTL_NO_ACK)))
379                 {
380                         struct ieee80211_tx_status status;
381                         memset(&status, 0, sizeof(status));
382                         tx_status(hw, skb, &status, !error);
383                 } else {
384                         struct sk_buff_head *q =
385                                 &zd_hw_mac(hw)->ack_wait_queue;
386
387                         skb_queue_tail(q, skb);
388                         while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS)
389                                 zd_mac_tx_failed(hw);
390                 }
391         } else {
392                 kfree_tx_skb(skb);
393         }
394 }
395
396 static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
397 {
398         /* ZD_PURE_RATE() must be used to remove the modulation type flag of
399          * the zd-rate values.
400          */
401         static const u8 rate_divisor[] = {
402                 [ZD_PURE_RATE(ZD_CCK_RATE_1M)]   =  1,
403                 [ZD_PURE_RATE(ZD_CCK_RATE_2M)]   =  2,
404                 /* Bits must be doubled. */
405                 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
406                 [ZD_PURE_RATE(ZD_CCK_RATE_11M)]  = 11,
407                 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)]  =  6,
408                 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)]  =  9,
409                 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
410                 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
411                 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
412                 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
413                 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
414                 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
415         };
416
417         u32 bits = (u32)tx_length * 8;
418         u32 divisor;
419
420         divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
421         if (divisor == 0)
422                 return -EINVAL;
423
424         switch (zd_rate) {
425         case ZD_CCK_RATE_5_5M:
426                 bits = (2*bits) + 10; /* round up to the next integer */
427                 break;
428         case ZD_CCK_RATE_11M:
429                 if (service) {
430                         u32 t = bits % 11;
431                         *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
432                         if (0 < t && t <= 3) {
433                                 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
434                         }
435                 }
436                 bits += 10; /* round up to the next integer */
437                 break;
438         }
439
440         return bits/divisor;
441 }
442
443 static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
444                            struct ieee80211_hdr *header, u32 flags)
445 {
446         u16 fctl = le16_to_cpu(header->frame_control);
447
448         /*
449          * CONTROL TODO:
450          * - if backoff needed, enable bit 0
451          * - if burst (backoff not needed) disable bit 0
452          */
453
454         cs->control = 0;
455
456         /* First fragment */
457         if (flags & IEEE80211_TXCTL_FIRST_FRAGMENT)
458                 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
459
460         /* Multicast */
461         if (is_multicast_ether_addr(header->addr1))
462                 cs->control |= ZD_CS_MULTICAST;
463
464         /* PS-POLL */
465         if ((fctl & (IEEE80211_FCTL_FTYPE|IEEE80211_FCTL_STYPE)) ==
466             (IEEE80211_FTYPE_CTL|IEEE80211_STYPE_PSPOLL))
467                 cs->control |= ZD_CS_PS_POLL_FRAME;
468
469         if (flags & IEEE80211_TXCTL_USE_RTS_CTS)
470                 cs->control |= ZD_CS_RTS;
471
472         if (flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
473                 cs->control |= ZD_CS_SELF_CTS;
474
475         /* FIXME: Management frame? */
476 }
477
478 static int fill_ctrlset(struct zd_mac *mac,
479                         struct sk_buff *skb,
480                         struct ieee80211_tx_control *control)
481 {
482         int r;
483         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
484         unsigned int frag_len = skb->len + FCS_LEN;
485         unsigned int packet_length;
486         struct zd_ctrlset *cs = (struct zd_ctrlset *)
487                 skb_push(skb, sizeof(struct zd_ctrlset));
488
489         ZD_ASSERT(frag_len <= 0xffff);
490
491         cs->modulation = control->tx_rate->hw_value;
492         if (control->flags & IEEE80211_TXCTL_SHORT_PREAMBLE)
493                 cs->modulation = control->tx_rate->hw_value_short;
494
495         cs->tx_length = cpu_to_le16(frag_len);
496
497         cs_set_control(mac, cs, hdr, control->flags);
498
499         packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
500         ZD_ASSERT(packet_length <= 0xffff);
501         /* ZD1211B: Computing the length difference this way, gives us
502          * flexibility to compute the packet length.
503          */
504         cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
505                         packet_length - frag_len : packet_length);
506
507         /*
508          * CURRENT LENGTH:
509          * - transmit frame length in microseconds
510          * - seems to be derived from frame length
511          * - see Cal_Us_Service() in zdinlinef.h
512          * - if macp->bTxBurstEnable is enabled, then multiply by 4
513          *  - bTxBurstEnable is never set in the vendor driver
514          *
515          * SERVICE:
516          * - "for PLCP configuration"
517          * - always 0 except in some situations at 802.11b 11M
518          * - see line 53 of zdinlinef.h
519          */
520         cs->service = 0;
521         r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
522                                  le16_to_cpu(cs->tx_length));
523         if (r < 0)
524                 return r;
525         cs->current_length = cpu_to_le16(r);
526         cs->next_frame_length = 0;
527
528         return 0;
529 }
530
531 /**
532  * zd_op_tx - transmits a network frame to the device
533  *
534  * @dev: mac80211 hardware device
535  * @skb: socket buffer
536  * @control: the control structure
537  *
538  * This function transmit an IEEE 802.11 network frame to the device. The
539  * control block of the skbuff will be initialized. If necessary the incoming
540  * mac80211 queues will be stopped.
541  */
542 static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
543                      struct ieee80211_tx_control *control)
544 {
545         struct zd_mac *mac = zd_hw_mac(hw);
546         int r;
547
548         r = fill_ctrlset(mac, skb, control);
549         if (r)
550                 return r;
551
552         r = init_tx_skb_control_block(skb, hw, control);
553         if (r)
554                 return r;
555         r = zd_usb_tx(&mac->chip.usb, skb);
556         if (r) {
557                 clear_tx_skb_control_block(skb);
558                 return r;
559         }
560         return 0;
561 }
562
563 /**
564  * filter_ack - filters incoming packets for acknowledgements
565  * @dev: the mac80211 device
566  * @rx_hdr: received header
567  * @stats: the status for the received packet
568  *
569  * This functions looks for ACK packets and tries to match them with the
570  * frames in the tx queue. If a match is found the frame will be dequeued and
571  * the upper layers is informed about the successful transmission. If
572  * mac80211 queues have been stopped and the number of frames still to be
573  * transmitted is low the queues will be opened again.
574  *
575  * Returns 1 if the frame was an ACK, 0 if it was ignored.
576  */
577 static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
578                       struct ieee80211_rx_status *stats)
579 {
580         u16 fc = le16_to_cpu(rx_hdr->frame_control);
581         struct sk_buff *skb;
582         struct sk_buff_head *q;
583         unsigned long flags;
584
585         if ((fc & (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) !=
586             (IEEE80211_FTYPE_CTL | IEEE80211_STYPE_ACK))
587                 return 0;
588
589         q = &zd_hw_mac(hw)->ack_wait_queue;
590         spin_lock_irqsave(&q->lock, flags);
591         for (skb = q->next; skb != (struct sk_buff *)q; skb = skb->next) {
592                 struct ieee80211_hdr *tx_hdr;
593
594                 tx_hdr = (struct ieee80211_hdr *)skb->data;
595                 if (likely(!compare_ether_addr(tx_hdr->addr2, rx_hdr->addr1)))
596                 {
597                         struct ieee80211_tx_status status;
598
599                         memset(&status, 0, sizeof(status));
600                         status.flags = IEEE80211_TX_STATUS_ACK;
601                         status.ack_signal = stats->ssi;
602                         __skb_unlink(skb, q);
603                         tx_status(hw, skb, &status, 1);
604                         goto out;
605                 }
606         }
607 out:
608         spin_unlock_irqrestore(&q->lock, flags);
609         return 1;
610 }
611
612 int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
613 {
614         struct zd_mac *mac = zd_hw_mac(hw);
615         struct ieee80211_rx_status stats;
616         const struct rx_status *status;
617         struct sk_buff *skb;
618         int bad_frame = 0;
619         u16 fc;
620         bool is_qos, is_4addr, need_padding;
621         int i;
622         u8 rate;
623
624         if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
625                      FCS_LEN + sizeof(struct rx_status))
626                 return -EINVAL;
627
628         memset(&stats, 0, sizeof(stats));
629
630         /* Note about pass_failed_fcs and pass_ctrl access below:
631          * mac locking intentionally omitted here, as this is the only unlocked
632          * reader and the only writer is configure_filter. Plus, if there were
633          * any races accessing these variables, it wouldn't really matter.
634          * If mac80211 ever provides a way for us to access filter flags
635          * from outside configure_filter, we could improve on this. Also, this
636          * situation may change once we implement some kind of DMA-into-skb
637          * RX path. */
638
639         /* Caller has to ensure that length >= sizeof(struct rx_status). */
640         status = (struct rx_status *)
641                 (buffer + (length - sizeof(struct rx_status)));
642         if (status->frame_status & ZD_RX_ERROR) {
643                 if (mac->pass_failed_fcs &&
644                                 (status->frame_status & ZD_RX_CRC32_ERROR)) {
645                         stats.flag |= RX_FLAG_FAILED_FCS_CRC;
646                         bad_frame = 1;
647                 } else {
648                         return -EINVAL;
649                 }
650         }
651
652         stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
653         stats.band = IEEE80211_BAND_2GHZ;
654         stats.ssi = status->signal_strength;
655         stats.signal = zd_rx_qual_percent(buffer,
656                                           length - sizeof(struct rx_status),
657                                           status);
658
659         rate = zd_rx_rate(buffer, status);
660
661         /* todo: return index in the big switches in zd_rx_rate instead */
662         for (i = 0; i < mac->band.n_bitrates; i++)
663                 if (rate == mac->band.bitrates[i].hw_value)
664                         stats.rate_idx = i;
665
666         length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
667         buffer += ZD_PLCP_HEADER_SIZE;
668
669         /* Except for bad frames, filter each frame to see if it is an ACK, in
670          * which case our internal TX tracking is updated. Normally we then
671          * bail here as there's no need to pass ACKs on up to the stack, but
672          * there is also the case where the stack has requested us to pass
673          * control frames on up (pass_ctrl) which we must consider. */
674         if (!bad_frame &&
675                         filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
676                         && !mac->pass_ctrl)
677                 return 0;
678
679         fc = le16_to_cpu(*((__le16 *) buffer));
680
681         is_qos = ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
682                  ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_QOS_DATA);
683         is_4addr = (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
684                    (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS);
685         need_padding = is_qos ^ is_4addr;
686
687         skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
688         if (skb == NULL)
689                 return -ENOMEM;
690         if (need_padding) {
691                 /* Make sure the the payload data is 4 byte aligned. */
692                 skb_reserve(skb, 2);
693         }
694
695         memcpy(skb_put(skb, length), buffer, length);
696
697         ieee80211_rx_irqsafe(hw, skb, &stats);
698         return 0;
699 }
700
701 static int zd_op_add_interface(struct ieee80211_hw *hw,
702                                 struct ieee80211_if_init_conf *conf)
703 {
704         struct zd_mac *mac = zd_hw_mac(hw);
705
706         /* using IEEE80211_IF_TYPE_INVALID to indicate no mode selected */
707         if (mac->type != IEEE80211_IF_TYPE_INVALID)
708                 return -EOPNOTSUPP;
709
710         switch (conf->type) {
711         case IEEE80211_IF_TYPE_MNTR:
712         case IEEE80211_IF_TYPE_STA:
713                 mac->type = conf->type;
714                 break;
715         default:
716                 return -EOPNOTSUPP;
717         }
718
719         return zd_write_mac_addr(&mac->chip, conf->mac_addr);
720 }
721
722 static void zd_op_remove_interface(struct ieee80211_hw *hw,
723                                     struct ieee80211_if_init_conf *conf)
724 {
725         struct zd_mac *mac = zd_hw_mac(hw);
726         mac->type = IEEE80211_IF_TYPE_INVALID;
727         zd_write_mac_addr(&mac->chip, NULL);
728 }
729
730 static int zd_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
731 {
732         struct zd_mac *mac = zd_hw_mac(hw);
733         return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
734 }
735
736 static int zd_op_config_interface(struct ieee80211_hw *hw,
737                                   struct ieee80211_vif *vif,
738                                    struct ieee80211_if_conf *conf)
739 {
740         struct zd_mac *mac = zd_hw_mac(hw);
741
742         spin_lock_irq(&mac->lock);
743         mac->associated = is_valid_ether_addr(conf->bssid);
744         spin_unlock_irq(&mac->lock);
745
746         /* TODO: do hardware bssid filtering */
747         return 0;
748 }
749
750 static void set_multicast_hash_handler(struct work_struct *work)
751 {
752         struct zd_mac *mac =
753                 container_of(work, struct zd_mac, set_multicast_hash_work);
754         struct zd_mc_hash hash;
755
756         spin_lock_irq(&mac->lock);
757         hash = mac->multicast_hash;
758         spin_unlock_irq(&mac->lock);
759
760         zd_chip_set_multicast_hash(&mac->chip, &hash);
761 }
762
763 static void set_rx_filter_handler(struct work_struct *work)
764 {
765         struct zd_mac *mac =
766                 container_of(work, struct zd_mac, set_rx_filter_work);
767         int r;
768
769         dev_dbg_f(zd_mac_dev(mac), "\n");
770         r = set_rx_filter(mac);
771         if (r)
772                 dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r);
773 }
774
775 #define SUPPORTED_FIF_FLAGS \
776         (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
777         FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
778 static void zd_op_configure_filter(struct ieee80211_hw *hw,
779                         unsigned int changed_flags,
780                         unsigned int *new_flags,
781                         int mc_count, struct dev_mc_list *mclist)
782 {
783         struct zd_mc_hash hash;
784         struct zd_mac *mac = zd_hw_mac(hw);
785         unsigned long flags;
786         int i;
787
788         /* Only deal with supported flags */
789         changed_flags &= SUPPORTED_FIF_FLAGS;
790         *new_flags &= SUPPORTED_FIF_FLAGS;
791
792         /* changed_flags is always populated but this driver
793          * doesn't support all FIF flags so its possible we don't
794          * need to do anything */
795         if (!changed_flags)
796                 return;
797
798         if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI)) {
799                 zd_mc_add_all(&hash);
800         } else {
801                 DECLARE_MAC_BUF(macbuf);
802
803                 zd_mc_clear(&hash);
804                 for (i = 0; i < mc_count; i++) {
805                         if (!mclist)
806                                 break;
807                         dev_dbg_f(zd_mac_dev(mac), "mc addr %s\n",
808                                   print_mac(macbuf, mclist->dmi_addr));
809                         zd_mc_add_addr(&hash, mclist->dmi_addr);
810                         mclist = mclist->next;
811                 }
812         }
813
814         spin_lock_irqsave(&mac->lock, flags);
815         mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
816         mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
817         mac->multicast_hash = hash;
818         spin_unlock_irqrestore(&mac->lock, flags);
819         queue_work(zd_workqueue, &mac->set_multicast_hash_work);
820
821         if (changed_flags & FIF_CONTROL)
822                 queue_work(zd_workqueue, &mac->set_rx_filter_work);
823
824         /* no handling required for FIF_OTHER_BSS as we don't currently
825          * do BSSID filtering */
826         /* FIXME: in future it would be nice to enable the probe response
827          * filter (so that the driver doesn't see them) until
828          * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
829          * have to schedule work to enable prbresp reception, which might
830          * happen too late. For now we'll just listen and forward them all the
831          * time. */
832 }
833
834 static void set_rts_cts_work(struct work_struct *work)
835 {
836         struct zd_mac *mac =
837                 container_of(work, struct zd_mac, set_rts_cts_work);
838         unsigned long flags;
839         unsigned int short_preamble;
840
841         mutex_lock(&mac->chip.mutex);
842
843         spin_lock_irqsave(&mac->lock, flags);
844         mac->updating_rts_rate = 0;
845         short_preamble = mac->short_preamble;
846         spin_unlock_irqrestore(&mac->lock, flags);
847
848         zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
849         mutex_unlock(&mac->chip.mutex);
850 }
851
852 static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
853                                    struct ieee80211_vif *vif,
854                                    struct ieee80211_bss_conf *bss_conf,
855                                    u32 changes)
856 {
857         struct zd_mac *mac = zd_hw_mac(hw);
858         unsigned long flags;
859
860         dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
861
862         if (changes & BSS_CHANGED_ERP_PREAMBLE) {
863                 spin_lock_irqsave(&mac->lock, flags);
864                 mac->short_preamble = bss_conf->use_short_preamble;
865                 if (!mac->updating_rts_rate) {
866                         mac->updating_rts_rate = 1;
867                         /* FIXME: should disable TX here, until work has
868                          * completed and RTS_CTS reg is updated */
869                         queue_work(zd_workqueue, &mac->set_rts_cts_work);
870                 }
871                 spin_unlock_irqrestore(&mac->lock, flags);
872         }
873 }
874
875 static const struct ieee80211_ops zd_ops = {
876         .tx                     = zd_op_tx,
877         .start                  = zd_op_start,
878         .stop                   = zd_op_stop,
879         .add_interface          = zd_op_add_interface,
880         .remove_interface       = zd_op_remove_interface,
881         .config                 = zd_op_config,
882         .config_interface       = zd_op_config_interface,
883         .configure_filter       = zd_op_configure_filter,
884         .bss_info_changed       = zd_op_bss_info_changed,
885 };
886
887 struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
888 {
889         struct zd_mac *mac;
890         struct ieee80211_hw *hw;
891
892         hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
893         if (!hw) {
894                 dev_dbg_f(&intf->dev, "out of memory\n");
895                 return NULL;
896         }
897
898         mac = zd_hw_mac(hw);
899
900         memset(mac, 0, sizeof(*mac));
901         spin_lock_init(&mac->lock);
902         mac->hw = hw;
903
904         mac->type = IEEE80211_IF_TYPE_INVALID;
905
906         memcpy(mac->channels, zd_channels, sizeof(zd_channels));
907         memcpy(mac->rates, zd_rates, sizeof(zd_rates));
908         mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
909         mac->band.bitrates = mac->rates;
910         mac->band.n_channels = ARRAY_SIZE(zd_channels);
911         mac->band.channels = mac->channels;
912
913         hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
914
915         hw->flags = IEEE80211_HW_RX_INCLUDES_FCS;
916         hw->max_rssi = 100;
917         hw->max_signal = 100;
918
919         hw->queues = 1;
920         hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
921
922         skb_queue_head_init(&mac->ack_wait_queue);
923
924         zd_chip_init(&mac->chip, hw, intf);
925         housekeeping_init(mac);
926         INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
927         INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
928         INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler);
929
930         SET_IEEE80211_DEV(hw, &intf->dev);
931         return hw;
932 }
933
934 #define LINK_LED_WORK_DELAY HZ
935
936 static void link_led_handler(struct work_struct *work)
937 {
938         struct zd_mac *mac =
939                 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
940         struct zd_chip *chip = &mac->chip;
941         int is_associated;
942         int r;
943
944         spin_lock_irq(&mac->lock);
945         is_associated = mac->associated;
946         spin_unlock_irq(&mac->lock);
947
948         r = zd_chip_control_leds(chip,
949                                  is_associated ? LED_ASSOCIATED : LED_SCANNING);
950         if (r)
951                 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
952
953         queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
954                            LINK_LED_WORK_DELAY);
955 }
956
957 static void housekeeping_init(struct zd_mac *mac)
958 {
959         INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
960 }
961
962 static void housekeeping_enable(struct zd_mac *mac)
963 {
964         dev_dbg_f(zd_mac_dev(mac), "\n");
965         queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
966                            0);
967 }
968
969 static void housekeeping_disable(struct zd_mac *mac)
970 {
971         dev_dbg_f(zd_mac_dev(mac), "\n");
972         cancel_rearming_delayed_workqueue(zd_workqueue,
973                 &mac->housekeeping.link_led_work);
974         zd_chip_control_leds(&mac->chip, LED_OFF);
975 }