[PATCH] zd1211rw: monitor all packets
[linux-2.6] / drivers / net / wireless / zd1211rw / zd_mac.c
1 /* zd_mac.c
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17
18 #include <linux/netdevice.h>
19 #include <linux/etherdevice.h>
20 #include <linux/wireless.h>
21 #include <linux/usb.h>
22 #include <linux/jiffies.h>
23 #include <net/ieee80211_radiotap.h>
24
25 #include "zd_def.h"
26 #include "zd_chip.h"
27 #include "zd_mac.h"
28 #include "zd_ieee80211.h"
29 #include "zd_netdev.h"
30 #include "zd_rf.h"
31 #include "zd_util.h"
32
33 static void ieee_init(struct ieee80211_device *ieee);
34 static void softmac_init(struct ieee80211softmac_device *sm);
35 static void set_rts_cts_work(struct work_struct *work);
36 static void set_basic_rates_work(struct work_struct *work);
37
38 static void housekeeping_init(struct zd_mac *mac);
39 static void housekeeping_enable(struct zd_mac *mac);
40 static void housekeeping_disable(struct zd_mac *mac);
41
42 static void set_multicast_hash_handler(struct work_struct *work);
43
44 static void do_rx(unsigned long mac_ptr);
45
46 int zd_mac_init(struct zd_mac *mac,
47                 struct net_device *netdev,
48                 struct usb_interface *intf)
49 {
50         struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
51
52         memset(mac, 0, sizeof(*mac));
53         spin_lock_init(&mac->lock);
54         mac->netdev = netdev;
55         INIT_DELAYED_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
56         INIT_DELAYED_WORK(&mac->set_basic_rates_work, set_basic_rates_work);
57
58         skb_queue_head_init(&mac->rx_queue);
59         tasklet_init(&mac->rx_tasklet, do_rx, (unsigned long)mac);
60         tasklet_disable(&mac->rx_tasklet);
61
62         ieee_init(ieee);
63         softmac_init(ieee80211_priv(netdev));
64         zd_chip_init(&mac->chip, netdev, intf);
65         housekeeping_init(mac);
66         INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
67         return 0;
68 }
69
70 static int reset_channel(struct zd_mac *mac)
71 {
72         int r;
73         unsigned long flags;
74         const struct channel_range *range;
75
76         spin_lock_irqsave(&mac->lock, flags);
77         range = zd_channel_range(mac->regdomain);
78         if (!range->start) {
79                 r = -EINVAL;
80                 goto out;
81         }
82         mac->requested_channel = range->start;
83         r = 0;
84 out:
85         spin_unlock_irqrestore(&mac->lock, flags);
86         return r;
87 }
88
89 int zd_mac_preinit_hw(struct zd_mac *mac)
90 {
91         int r;
92         u8 addr[ETH_ALEN];
93
94         r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
95         if (r)
96                 return r;
97
98         memcpy(mac->netdev->dev_addr, addr, ETH_ALEN);
99         return 0;
100 }
101
102 int zd_mac_init_hw(struct zd_mac *mac)
103 {
104         int r;
105         struct zd_chip *chip = &mac->chip;
106         u8 default_regdomain;
107
108         r = zd_chip_enable_int(chip);
109         if (r)
110                 goto out;
111         r = zd_chip_init_hw(chip);
112         if (r)
113                 goto disable_int;
114
115         ZD_ASSERT(!irqs_disabled());
116
117         r = zd_read_regdomain(chip, &default_regdomain);
118         if (r)
119                 goto disable_int;
120         if (!zd_regdomain_supported(default_regdomain)) {
121                 /* The vendor driver overrides the regulatory domain and
122                  * allowed channel registers and unconditionally restricts
123                  * available channels to 1-11 everywhere. Match their
124                  * questionable behaviour only for regdomains which we don't
125                  * recognise. */
126                 dev_warn(zd_mac_dev(mac),  "Unrecognised regulatory domain: "
127                         "%#04x. Defaulting to FCC.\n", default_regdomain);
128                 default_regdomain = ZD_REGDOMAIN_FCC;
129         }
130         spin_lock_irq(&mac->lock);
131         mac->regdomain = mac->default_regdomain = default_regdomain;
132         spin_unlock_irq(&mac->lock);
133         r = reset_channel(mac);
134         if (r)
135                 goto disable_int;
136
137         /* We must inform the device that we are doing encryption/decryption in
138          * software at the moment. */
139         r = zd_set_encryption_type(chip, ENC_SNIFFER);
140         if (r)
141                 goto disable_int;
142
143         r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
144         if (r)
145                 goto disable_int;
146
147         r = 0;
148 disable_int:
149         zd_chip_disable_int(chip);
150 out:
151         return r;
152 }
153
154 void zd_mac_clear(struct zd_mac *mac)
155 {
156         flush_workqueue(zd_workqueue);
157         skb_queue_purge(&mac->rx_queue);
158         tasklet_kill(&mac->rx_tasklet);
159         zd_chip_clear(&mac->chip);
160         ZD_ASSERT(!spin_is_locked(&mac->lock));
161         ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
162 }
163
164 static int set_rx_filter(struct zd_mac *mac)
165 {
166         struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
167         u32 filter = (ieee->iw_mode == IW_MODE_MONITOR) ? ~0 : STA_RX_FILTER;
168         return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
169 }
170
171 static int set_sniffer(struct zd_mac *mac)
172 {
173         struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
174         return zd_iowrite32(&mac->chip, CR_SNIFFER_ON,
175                 ieee->iw_mode == IW_MODE_MONITOR ? 1 : 0);
176         return 0;
177 }
178
179 static int set_mc_hash(struct zd_mac *mac)
180 {
181         struct zd_mc_hash hash;
182         struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
183
184         zd_mc_clear(&hash);
185         if (ieee->iw_mode == IW_MODE_MONITOR)
186                 zd_mc_add_all(&hash);
187
188         return zd_chip_set_multicast_hash(&mac->chip, &hash);
189 }
190
191 int zd_mac_open(struct net_device *netdev)
192 {
193         struct zd_mac *mac = zd_netdev_mac(netdev);
194         struct zd_chip *chip = &mac->chip;
195         struct zd_usb *usb = &chip->usb;
196         int r;
197
198         if (!usb->initialized) {
199                 r = zd_usb_init_hw(usb);
200                 if (r)
201                         goto out;
202         }
203
204         tasklet_enable(&mac->rx_tasklet);
205
206         r = zd_chip_enable_int(chip);
207         if (r < 0)
208                 goto out;
209
210         r = zd_write_mac_addr(chip, netdev->dev_addr);
211         if (r)
212                 goto disable_int;
213
214         r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
215         if (r < 0)
216                 goto disable_int;
217         r = set_rx_filter(mac);
218         if (r)
219                 goto disable_int;
220         r = set_sniffer(mac);
221         if (r)
222                 goto disable_int;
223         r = set_mc_hash(mac);
224         if (r)
225                 goto disable_int;
226         r = zd_chip_switch_radio_on(chip);
227         if (r < 0)
228                 goto disable_int;
229         r = zd_chip_set_channel(chip, mac->requested_channel);
230         if (r < 0)
231                 goto disable_radio;
232         r = zd_chip_enable_rx(chip);
233         if (r < 0)
234                 goto disable_radio;
235         r = zd_chip_enable_hwint(chip);
236         if (r < 0)
237                 goto disable_rx;
238
239         housekeeping_enable(mac);
240         ieee80211softmac_start(netdev);
241         return 0;
242 disable_rx:
243         zd_chip_disable_rx(chip);
244 disable_radio:
245         zd_chip_switch_radio_off(chip);
246 disable_int:
247         zd_chip_disable_int(chip);
248 out:
249         return r;
250 }
251
252 int zd_mac_stop(struct net_device *netdev)
253 {
254         struct zd_mac *mac = zd_netdev_mac(netdev);
255         struct zd_chip *chip = &mac->chip;
256
257         netif_stop_queue(netdev);
258
259         /*
260          * The order here deliberately is a little different from the open()
261          * method, since we need to make sure there is no opportunity for RX
262          * frames to be processed by softmac after we have stopped it.
263          */
264
265         zd_chip_disable_rx(chip);
266         skb_queue_purge(&mac->rx_queue);
267         tasklet_disable(&mac->rx_tasklet);
268         housekeeping_disable(mac);
269         ieee80211softmac_stop(netdev);
270
271         /* Ensure no work items are running or queued from this point */
272         cancel_delayed_work(&mac->set_rts_cts_work);
273         cancel_delayed_work(&mac->set_basic_rates_work);
274         flush_workqueue(zd_workqueue);
275         mac->updating_rts_rate = 0;
276         mac->updating_basic_rates = 0;
277
278         zd_chip_disable_hwint(chip);
279         zd_chip_switch_radio_off(chip);
280         zd_chip_disable_int(chip);
281
282         return 0;
283 }
284
285 int zd_mac_set_mac_address(struct net_device *netdev, void *p)
286 {
287         int r;
288         unsigned long flags;
289         struct sockaddr *addr = p;
290         struct zd_mac *mac = zd_netdev_mac(netdev);
291         struct zd_chip *chip = &mac->chip;
292
293         if (!is_valid_ether_addr(addr->sa_data))
294                 return -EADDRNOTAVAIL;
295
296         dev_dbg_f(zd_mac_dev(mac),
297                   "Setting MAC to " MAC_FMT "\n", MAC_ARG(addr->sa_data));
298
299         if (netdev->flags & IFF_UP) {
300                 r = zd_write_mac_addr(chip, addr->sa_data);
301                 if (r)
302                         return r;
303         }
304
305         spin_lock_irqsave(&mac->lock, flags);
306         memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
307         spin_unlock_irqrestore(&mac->lock, flags);
308
309         return 0;
310 }
311
312 static void set_multicast_hash_handler(struct work_struct *work)
313 {
314         struct zd_mac *mac = container_of(work, struct zd_mac,
315                                           set_multicast_hash_work);
316         struct zd_mc_hash hash;
317
318         spin_lock_irq(&mac->lock);
319         hash = mac->multicast_hash;
320         spin_unlock_irq(&mac->lock);
321
322         zd_chip_set_multicast_hash(&mac->chip, &hash);
323 }
324
325 void zd_mac_set_multicast_list(struct net_device *dev)
326 {
327         struct zd_mac *mac = zd_netdev_mac(dev);
328         struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
329         struct zd_mc_hash hash;
330         struct dev_mc_list *mc;
331         unsigned long flags;
332
333         if (dev->flags & (IFF_PROMISC|IFF_ALLMULTI) ||
334                         ieee->iw_mode == IW_MODE_MONITOR) {
335                 zd_mc_add_all(&hash);
336         } else {
337                 zd_mc_clear(&hash);
338                 for (mc = dev->mc_list; mc; mc = mc->next) {
339                         dev_dbg_f(zd_mac_dev(mac), "mc addr " MAC_FMT "\n",
340                                   MAC_ARG(mc->dmi_addr));
341                         zd_mc_add_addr(&hash, mc->dmi_addr);
342                 }
343         }
344
345         spin_lock_irqsave(&mac->lock, flags);
346         mac->multicast_hash = hash;
347         spin_unlock_irqrestore(&mac->lock, flags);
348         queue_work(zd_workqueue, &mac->set_multicast_hash_work);
349 }
350
351 int zd_mac_set_regdomain(struct zd_mac *mac, u8 regdomain)
352 {
353         int r;
354         u8 channel;
355
356         ZD_ASSERT(!irqs_disabled());
357         spin_lock_irq(&mac->lock);
358         if (regdomain == 0) {
359                 regdomain = mac->default_regdomain;
360         }
361         if (!zd_regdomain_supported(regdomain)) {
362                 spin_unlock_irq(&mac->lock);
363                 return -EINVAL;
364         }
365         mac->regdomain = regdomain;
366         channel = mac->requested_channel;
367         spin_unlock_irq(&mac->lock);
368
369         r = zd_geo_init(zd_mac_to_ieee80211(mac), regdomain);
370         if (r)
371                 return r;
372         if (!zd_regdomain_supports_channel(regdomain, channel)) {
373                 r = reset_channel(mac);
374                 if (r)
375                         return r;
376         }
377
378         return 0;
379 }
380
381 u8 zd_mac_get_regdomain(struct zd_mac *mac)
382 {
383         unsigned long flags;
384         u8 regdomain;
385
386         spin_lock_irqsave(&mac->lock, flags);
387         regdomain = mac->regdomain;
388         spin_unlock_irqrestore(&mac->lock, flags);
389         return regdomain;
390 }
391
392 /* Fallback to lowest rate, if rate is unknown. */
393 static u8 rate_to_zd_rate(u8 rate)
394 {
395         switch (rate) {
396         case IEEE80211_CCK_RATE_2MB:
397                 return ZD_CCK_RATE_2M;
398         case IEEE80211_CCK_RATE_5MB:
399                 return ZD_CCK_RATE_5_5M;
400         case IEEE80211_CCK_RATE_11MB:
401                 return ZD_CCK_RATE_11M;
402         case IEEE80211_OFDM_RATE_6MB:
403                 return ZD_OFDM_RATE_6M;
404         case IEEE80211_OFDM_RATE_9MB:
405                 return ZD_OFDM_RATE_9M;
406         case IEEE80211_OFDM_RATE_12MB:
407                 return ZD_OFDM_RATE_12M;
408         case IEEE80211_OFDM_RATE_18MB:
409                 return ZD_OFDM_RATE_18M;
410         case IEEE80211_OFDM_RATE_24MB:
411                 return ZD_OFDM_RATE_24M;
412         case IEEE80211_OFDM_RATE_36MB:
413                 return ZD_OFDM_RATE_36M;
414         case IEEE80211_OFDM_RATE_48MB:
415                 return ZD_OFDM_RATE_48M;
416         case IEEE80211_OFDM_RATE_54MB:
417                 return ZD_OFDM_RATE_54M;
418         }
419         return ZD_CCK_RATE_1M;
420 }
421
422 static u16 rate_to_cr_rate(u8 rate)
423 {
424         switch (rate) {
425         case IEEE80211_CCK_RATE_2MB:
426                 return CR_RATE_1M;
427         case IEEE80211_CCK_RATE_5MB:
428                 return CR_RATE_5_5M;
429         case IEEE80211_CCK_RATE_11MB:
430                 return CR_RATE_11M;
431         case IEEE80211_OFDM_RATE_6MB:
432                 return CR_RATE_6M;
433         case IEEE80211_OFDM_RATE_9MB:
434                 return CR_RATE_9M;
435         case IEEE80211_OFDM_RATE_12MB:
436                 return CR_RATE_12M;
437         case IEEE80211_OFDM_RATE_18MB:
438                 return CR_RATE_18M;
439         case IEEE80211_OFDM_RATE_24MB:
440                 return CR_RATE_24M;
441         case IEEE80211_OFDM_RATE_36MB:
442                 return CR_RATE_36M;
443         case IEEE80211_OFDM_RATE_48MB:
444                 return CR_RATE_48M;
445         case IEEE80211_OFDM_RATE_54MB:
446                 return CR_RATE_54M;
447         }
448         return CR_RATE_1M;
449 }
450
451 static void try_enable_tx(struct zd_mac *mac)
452 {
453         unsigned long flags;
454
455         spin_lock_irqsave(&mac->lock, flags);
456         if (mac->updating_rts_rate == 0 && mac->updating_basic_rates == 0)
457                 netif_wake_queue(mac->netdev);
458         spin_unlock_irqrestore(&mac->lock, flags);
459 }
460
461 static void set_rts_cts_work(struct work_struct *work)
462 {
463         struct zd_mac *mac =
464                 container_of(work, struct zd_mac, set_rts_cts_work.work);
465         unsigned long flags;
466         u8 rts_rate;
467         unsigned int short_preamble;
468
469         mutex_lock(&mac->chip.mutex);
470
471         spin_lock_irqsave(&mac->lock, flags);
472         mac->updating_rts_rate = 0;
473         rts_rate = mac->rts_rate;
474         short_preamble = mac->short_preamble;
475         spin_unlock_irqrestore(&mac->lock, flags);
476
477         zd_chip_set_rts_cts_rate_locked(&mac->chip, rts_rate, short_preamble);
478         mutex_unlock(&mac->chip.mutex);
479
480         try_enable_tx(mac);
481 }
482
483 static void set_basic_rates_work(struct work_struct *work)
484 {
485         struct zd_mac *mac =
486                 container_of(work, struct zd_mac, set_basic_rates_work.work);
487         unsigned long flags;
488         u16 basic_rates;
489
490         mutex_lock(&mac->chip.mutex);
491
492         spin_lock_irqsave(&mac->lock, flags);
493         mac->updating_basic_rates = 0;
494         basic_rates = mac->basic_rates;
495         spin_unlock_irqrestore(&mac->lock, flags);
496
497         zd_chip_set_basic_rates_locked(&mac->chip, basic_rates);
498         mutex_unlock(&mac->chip.mutex);
499
500         try_enable_tx(mac);
501 }
502
503 static void bssinfo_change(struct net_device *netdev, u32 changes)
504 {
505         struct zd_mac *mac = zd_netdev_mac(netdev);
506         struct ieee80211softmac_device *softmac = ieee80211_priv(netdev);
507         struct ieee80211softmac_bss_info *bssinfo = &softmac->bssinfo;
508         int need_set_rts_cts = 0;
509         int need_set_rates = 0;
510         u16 basic_rates;
511         unsigned long flags;
512
513         dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
514
515         if (changes & IEEE80211SOFTMAC_BSSINFOCHG_SHORT_PREAMBLE) {
516                 spin_lock_irqsave(&mac->lock, flags);
517                 mac->short_preamble = bssinfo->short_preamble;
518                 spin_unlock_irqrestore(&mac->lock, flags);
519                 need_set_rts_cts = 1;
520         }
521
522         if (changes & IEEE80211SOFTMAC_BSSINFOCHG_RATES) {
523                 /* Set RTS rate to highest available basic rate */
524                 u8 hi_rate = ieee80211softmac_highest_supported_rate(softmac,
525                         &bssinfo->supported_rates, 1);
526                 hi_rate = rate_to_zd_rate(hi_rate);
527
528                 spin_lock_irqsave(&mac->lock, flags);
529                 if (hi_rate != mac->rts_rate) {
530                         mac->rts_rate = hi_rate;
531                         need_set_rts_cts = 1;
532                 }
533                 spin_unlock_irqrestore(&mac->lock, flags);
534
535                 /* Set basic rates */
536                 need_set_rates = 1;
537                 if (bssinfo->supported_rates.count == 0) {
538                         /* Allow the device to be flexible */
539                         basic_rates = CR_RATES_80211B | CR_RATES_80211G;
540                 } else {
541                         int i = 0;
542                         basic_rates = 0;
543
544                         for (i = 0; i < bssinfo->supported_rates.count; i++) {
545                                 u16 rate = bssinfo->supported_rates.rates[i];
546                                 if ((rate & IEEE80211_BASIC_RATE_MASK) == 0)
547                                         continue;
548
549                                 rate &= ~IEEE80211_BASIC_RATE_MASK;
550                                 basic_rates |= rate_to_cr_rate(rate);
551                         }
552                 }
553                 spin_lock_irqsave(&mac->lock, flags);
554                 mac->basic_rates = basic_rates;
555                 spin_unlock_irqrestore(&mac->lock, flags);
556         }
557
558         /* Schedule any changes we made above */
559
560         spin_lock_irqsave(&mac->lock, flags);
561         if (need_set_rts_cts && !mac->updating_rts_rate) {
562                 mac->updating_rts_rate = 1;
563                 netif_stop_queue(mac->netdev);
564                 queue_delayed_work(zd_workqueue, &mac->set_rts_cts_work, 0);
565         }
566         if (need_set_rates && !mac->updating_basic_rates) {
567                 mac->updating_basic_rates = 1;
568                 netif_stop_queue(mac->netdev);
569                 queue_delayed_work(zd_workqueue, &mac->set_basic_rates_work,
570                                    0);
571         }
572         spin_unlock_irqrestore(&mac->lock, flags);
573 }
574
575 static void set_channel(struct net_device *netdev, u8 channel)
576 {
577         struct zd_mac *mac = zd_netdev_mac(netdev);
578
579         dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
580
581         zd_chip_set_channel(&mac->chip, channel);
582 }
583
584 int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
585 {
586         unsigned long lock_flags;
587         struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
588
589         if (ieee->iw_mode == IW_MODE_INFRA)
590                 return -EPERM;
591
592         spin_lock_irqsave(&mac->lock, lock_flags);
593         if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
594                 spin_unlock_irqrestore(&mac->lock, lock_flags);
595                 return -EINVAL;
596         }
597         mac->requested_channel = channel;
598         spin_unlock_irqrestore(&mac->lock, lock_flags);
599         if (netif_running(mac->netdev))
600                 return zd_chip_set_channel(&mac->chip, channel);
601         else
602                 return 0;
603 }
604
605 u8 zd_mac_get_channel(struct zd_mac *mac)
606 {
607         u8 channel = zd_chip_get_channel(&mac->chip);
608
609         dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
610         return channel;
611 }
612
613 /* If wrong rate is given, we are falling back to the slowest rate: 1MBit/s */
614 static u8 zd_rate_typed(u8 zd_rate)
615 {
616         static const u8 typed_rates[16] = {
617                 [ZD_CCK_RATE_1M]        = ZD_CS_CCK|ZD_CCK_RATE_1M,
618                 [ZD_CCK_RATE_2M]        = ZD_CS_CCK|ZD_CCK_RATE_2M,
619                 [ZD_CCK_RATE_5_5M]      = ZD_CS_CCK|ZD_CCK_RATE_5_5M,
620                 [ZD_CCK_RATE_11M]       = ZD_CS_CCK|ZD_CCK_RATE_11M,
621                 [ZD_OFDM_RATE_6M]       = ZD_CS_OFDM|ZD_OFDM_RATE_6M,
622                 [ZD_OFDM_RATE_9M]       = ZD_CS_OFDM|ZD_OFDM_RATE_9M,
623                 [ZD_OFDM_RATE_12M]      = ZD_CS_OFDM|ZD_OFDM_RATE_12M,
624                 [ZD_OFDM_RATE_18M]      = ZD_CS_OFDM|ZD_OFDM_RATE_18M,
625                 [ZD_OFDM_RATE_24M]      = ZD_CS_OFDM|ZD_OFDM_RATE_24M,
626                 [ZD_OFDM_RATE_36M]      = ZD_CS_OFDM|ZD_OFDM_RATE_36M,
627                 [ZD_OFDM_RATE_48M]      = ZD_CS_OFDM|ZD_OFDM_RATE_48M,
628                 [ZD_OFDM_RATE_54M]      = ZD_CS_OFDM|ZD_OFDM_RATE_54M,
629         };
630
631         ZD_ASSERT(ZD_CS_RATE_MASK == 0x0f);
632         return typed_rates[zd_rate & ZD_CS_RATE_MASK];
633 }
634
635 int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
636 {
637         struct ieee80211_device *ieee;
638
639         switch (mode) {
640         case IW_MODE_AUTO:
641         case IW_MODE_ADHOC:
642         case IW_MODE_INFRA:
643                 mac->netdev->type = ARPHRD_ETHER;
644                 break;
645         case IW_MODE_MONITOR:
646                 mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
647                 break;
648         default:
649                 dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
650                 return -EINVAL;
651         }
652
653         ieee = zd_mac_to_ieee80211(mac);
654         ZD_ASSERT(!irqs_disabled());
655         spin_lock_irq(&ieee->lock);
656         ieee->iw_mode = mode;
657         spin_unlock_irq(&ieee->lock);
658
659         if (netif_running(mac->netdev)) {
660                 int r = set_rx_filter(mac);
661                 if (r)
662                         return r;
663                 return set_sniffer(mac);
664         }
665
666         return 0;
667 }
668
669 int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
670 {
671         unsigned long flags;
672         struct ieee80211_device *ieee;
673
674         ieee = zd_mac_to_ieee80211(mac);
675         spin_lock_irqsave(&ieee->lock, flags);
676         *mode = ieee->iw_mode;
677         spin_unlock_irqrestore(&ieee->lock, flags);
678         return 0;
679 }
680
681 int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
682 {
683         int i;
684         const struct channel_range *channel_range;
685         u8 regdomain;
686
687         memset(range, 0, sizeof(*range));
688
689         /* FIXME: Not so important and depends on the mode. For 802.11g
690          * usually this value is used. It seems to be that Bit/s number is
691          * given here.
692          */
693         range->throughput = 27 * 1000 * 1000;
694
695         range->max_qual.qual = 100;
696         range->max_qual.level = 100;
697
698         /* FIXME: Needs still to be tuned. */
699         range->avg_qual.qual = 71;
700         range->avg_qual.level = 80;
701
702         /* FIXME: depends on standard? */
703         range->min_rts = 256;
704         range->max_rts = 2346;
705
706         range->min_frag = MIN_FRAG_THRESHOLD;
707         range->max_frag = MAX_FRAG_THRESHOLD;
708
709         range->max_encoding_tokens = WEP_KEYS;
710         range->num_encoding_sizes = 2;
711         range->encoding_size[0] = 5;
712         range->encoding_size[1] = WEP_KEY_LEN;
713
714         range->we_version_compiled = WIRELESS_EXT;
715         range->we_version_source = 20;
716
717         range->enc_capa = IW_ENC_CAPA_WPA |  IW_ENC_CAPA_WPA2 |
718                           IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
719
720         ZD_ASSERT(!irqs_disabled());
721         spin_lock_irq(&mac->lock);
722         regdomain = mac->regdomain;
723         spin_unlock_irq(&mac->lock);
724         channel_range = zd_channel_range(regdomain);
725
726         range->num_channels = channel_range->end - channel_range->start;
727         range->old_num_channels = range->num_channels;
728         range->num_frequency = range->num_channels;
729         range->old_num_frequency = range->num_frequency;
730
731         for (i = 0; i < range->num_frequency; i++) {
732                 struct iw_freq *freq = &range->freq[i];
733                 freq->i = channel_range->start + i;
734                 zd_channel_to_freq(freq, freq->i);
735         }
736
737         return 0;
738 }
739
740 static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
741 {
742         static const u8 rate_divisor[] = {
743                 [ZD_CCK_RATE_1M]        =  1,
744                 [ZD_CCK_RATE_2M]        =  2,
745                 [ZD_CCK_RATE_5_5M]      = 11, /* bits must be doubled */
746                 [ZD_CCK_RATE_11M]       = 11,
747                 [ZD_OFDM_RATE_6M]       =  6,
748                 [ZD_OFDM_RATE_9M]       =  9,
749                 [ZD_OFDM_RATE_12M]      = 12,
750                 [ZD_OFDM_RATE_18M]      = 18,
751                 [ZD_OFDM_RATE_24M]      = 24,
752                 [ZD_OFDM_RATE_36M]      = 36,
753                 [ZD_OFDM_RATE_48M]      = 48,
754                 [ZD_OFDM_RATE_54M]      = 54,
755         };
756
757         u32 bits = (u32)tx_length * 8;
758         u32 divisor;
759
760         divisor = rate_divisor[zd_rate];
761         if (divisor == 0)
762                 return -EINVAL;
763
764         switch (zd_rate) {
765         case ZD_CCK_RATE_5_5M:
766                 bits = (2*bits) + 10; /* round up to the next integer */
767                 break;
768         case ZD_CCK_RATE_11M:
769                 if (service) {
770                         u32 t = bits % 11;
771                         *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
772                         if (0 < t && t <= 3) {
773                                 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
774                         }
775                 }
776                 bits += 10; /* round up to the next integer */
777                 break;
778         }
779
780         return bits/divisor;
781 }
782
783 enum {
784         R2M_SHORT_PREAMBLE = 0x01,
785         R2M_11A            = 0x02,
786 };
787
788 static u8 zd_rate_to_modulation(u8 zd_rate, int flags)
789 {
790         u8 modulation;
791
792         modulation = zd_rate_typed(zd_rate);
793         if (flags & R2M_SHORT_PREAMBLE) {
794                 switch (ZD_CS_RATE(modulation)) {
795                 case ZD_CCK_RATE_2M:
796                 case ZD_CCK_RATE_5_5M:
797                 case ZD_CCK_RATE_11M:
798                         modulation |= ZD_CS_CCK_PREA_SHORT;
799                         return modulation;
800                 }
801         }
802         if (flags & R2M_11A) {
803                 if (ZD_CS_TYPE(modulation) == ZD_CS_OFDM)
804                         modulation |= ZD_CS_OFDM_MODE_11A;
805         }
806         return modulation;
807 }
808
809 static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
810                               struct ieee80211_hdr_4addr *hdr)
811 {
812         struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
813         u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
814         u8 rate, zd_rate;
815         int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
816         int is_multicast = is_multicast_ether_addr(hdr->addr1);
817         int short_preamble = ieee80211softmac_short_preamble_ok(softmac,
818                 is_multicast, is_mgt);
819         int flags = 0;
820
821         /* FIXME: 802.11a? */
822         rate = ieee80211softmac_suggest_txrate(softmac, is_multicast, is_mgt);
823
824         if (short_preamble)
825                 flags |= R2M_SHORT_PREAMBLE;
826
827         zd_rate = rate_to_zd_rate(rate);
828         cs->modulation = zd_rate_to_modulation(zd_rate, flags);
829 }
830
831 static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
832                            struct ieee80211_hdr_4addr *header)
833 {
834         struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
835         unsigned int tx_length = le16_to_cpu(cs->tx_length);
836         u16 fctl = le16_to_cpu(header->frame_ctl);
837         u16 ftype = WLAN_FC_GET_TYPE(fctl);
838         u16 stype = WLAN_FC_GET_STYPE(fctl);
839
840         /*
841          * CONTROL TODO:
842          * - if backoff needed, enable bit 0
843          * - if burst (backoff not needed) disable bit 0
844          */
845
846         cs->control = 0;
847
848         /* First fragment */
849         if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
850                 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
851
852         /* Multicast */
853         if (is_multicast_ether_addr(header->addr1))
854                 cs->control |= ZD_CS_MULTICAST;
855
856         /* PS-POLL */
857         if (ftype == IEEE80211_FTYPE_CTL && stype == IEEE80211_STYPE_PSPOLL)
858                 cs->control |= ZD_CS_PS_POLL_FRAME;
859
860         /* Unicast data frames over the threshold should have RTS */
861         if (!is_multicast_ether_addr(header->addr1) &&
862                 ftype != IEEE80211_FTYPE_MGMT &&
863                     tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
864                 cs->control |= ZD_CS_RTS;
865
866         /* Use CTS-to-self protection if required */
867         if (ZD_CS_TYPE(cs->modulation) == ZD_CS_OFDM &&
868                         ieee80211softmac_protection_needed(softmac)) {
869                 /* FIXME: avoid sending RTS *and* self-CTS, is that correct? */
870                 cs->control &= ~ZD_CS_RTS;
871                 cs->control |= ZD_CS_SELF_CTS;
872         }
873
874         /* FIXME: Management frame? */
875 }
876
877 static int fill_ctrlset(struct zd_mac *mac,
878                         struct ieee80211_txb *txb,
879                         int frag_num)
880 {
881         int r;
882         struct sk_buff *skb = txb->fragments[frag_num];
883         struct ieee80211_hdr_4addr *hdr =
884                 (struct ieee80211_hdr_4addr *) skb->data;
885         unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
886         unsigned int next_frag_len;
887         unsigned int packet_length;
888         struct zd_ctrlset *cs = (struct zd_ctrlset *)
889                 skb_push(skb, sizeof(struct zd_ctrlset));
890
891         if (frag_num+1  < txb->nr_frags) {
892                 next_frag_len = txb->fragments[frag_num+1]->len +
893                                 IEEE80211_FCS_LEN;
894         } else {
895                 next_frag_len = 0;
896         }
897         ZD_ASSERT(frag_len <= 0xffff);
898         ZD_ASSERT(next_frag_len <= 0xffff);
899
900         cs_set_modulation(mac, cs, hdr);
901
902         cs->tx_length = cpu_to_le16(frag_len);
903
904         cs_set_control(mac, cs, hdr);
905
906         packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
907         ZD_ASSERT(packet_length <= 0xffff);
908         /* ZD1211B: Computing the length difference this way, gives us
909          * flexibility to compute the packet length.
910          */
911         cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
912                         packet_length - frag_len : packet_length);
913
914         /*
915          * CURRENT LENGTH:
916          * - transmit frame length in microseconds
917          * - seems to be derived from frame length
918          * - see Cal_Us_Service() in zdinlinef.h
919          * - if macp->bTxBurstEnable is enabled, then multiply by 4
920          *  - bTxBurstEnable is never set in the vendor driver
921          *
922          * SERVICE:
923          * - "for PLCP configuration"
924          * - always 0 except in some situations at 802.11b 11M
925          * - see line 53 of zdinlinef.h
926          */
927         cs->service = 0;
928         r = zd_calc_tx_length_us(&cs->service, ZD_CS_RATE(cs->modulation),
929                                  le16_to_cpu(cs->tx_length));
930         if (r < 0)
931                 return r;
932         cs->current_length = cpu_to_le16(r);
933
934         if (next_frag_len == 0) {
935                 cs->next_frame_length = 0;
936         } else {
937                 r = zd_calc_tx_length_us(NULL, ZD_CS_RATE(cs->modulation),
938                                          next_frag_len);
939                 if (r < 0)
940                         return r;
941                 cs->next_frame_length = cpu_to_le16(r);
942         }
943
944         return 0;
945 }
946
947 static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
948 {
949         int i, r;
950         struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
951
952         for (i = 0; i < txb->nr_frags; i++) {
953                 struct sk_buff *skb = txb->fragments[i];
954
955                 r = fill_ctrlset(mac, txb, i);
956                 if (r) {
957                         ieee->stats.tx_dropped++;
958                         return r;
959                 }
960                 r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
961                 if (r) {
962                         ieee->stats.tx_dropped++;
963                         return r;
964                 }
965         }
966
967         /* FIXME: shouldn't this be handled by the upper layers? */
968         mac->netdev->trans_start = jiffies;
969
970         ieee80211_txb_free(txb);
971         return 0;
972 }
973
974 struct zd_rt_hdr {
975         struct ieee80211_radiotap_header rt_hdr;
976         u8  rt_flags;
977         u8  rt_rate;
978         u16 rt_channel;
979         u16 rt_chbitmask;
980 } __attribute__((packed));
981
982 static void fill_rt_header(void *buffer, struct zd_mac *mac,
983                            const struct ieee80211_rx_stats *stats,
984                            const struct rx_status *status)
985 {
986         struct zd_rt_hdr *hdr = buffer;
987
988         hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
989         hdr->rt_hdr.it_pad = 0;
990         hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
991         hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
992                                  (1 << IEEE80211_RADIOTAP_CHANNEL) |
993                                  (1 << IEEE80211_RADIOTAP_RATE));
994
995         hdr->rt_flags = 0;
996         if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
997                 hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
998
999         hdr->rt_rate = stats->rate / 5;
1000
1001         /* FIXME: 802.11a */
1002         hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
1003                                              _zd_chip_get_channel(&mac->chip)));
1004         hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
1005                 ((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
1006                 ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
1007 }
1008
1009 /* Returns 1 if the data packet is for us and 0 otherwise. */
1010 static int is_data_packet_for_us(struct ieee80211_device *ieee,
1011                                  struct ieee80211_hdr_4addr *hdr)
1012 {
1013         struct net_device *netdev = ieee->dev;
1014         u16 fc = le16_to_cpu(hdr->frame_ctl);
1015
1016         ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
1017
1018         switch (ieee->iw_mode) {
1019         case IW_MODE_ADHOC:
1020                 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
1021                     compare_ether_addr(hdr->addr3, ieee->bssid) != 0)
1022                         return 0;
1023                 break;
1024         case IW_MODE_AUTO:
1025         case IW_MODE_INFRA:
1026                 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
1027                     IEEE80211_FCTL_FROMDS ||
1028                     compare_ether_addr(hdr->addr2, ieee->bssid) != 0)
1029                         return 0;
1030                 break;
1031         default:
1032                 ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
1033                 return 0;
1034         }
1035
1036         return compare_ether_addr(hdr->addr1, netdev->dev_addr) == 0 ||
1037                (is_multicast_ether_addr(hdr->addr1) &&
1038                 compare_ether_addr(hdr->addr3, netdev->dev_addr) != 0) ||
1039                (netdev->flags & IFF_PROMISC);
1040 }
1041
1042 /* Filters received packets. The function returns 1 if the packet should be
1043  * forwarded to ieee80211_rx(). If the packet should be ignored the function
1044  * returns 0. If an invalid packet is found the function returns -EINVAL.
1045  *
1046  * The function calls ieee80211_rx_mgt() directly.
1047  *
1048  * It has been based on ieee80211_rx_any.
1049  */
1050 static int filter_rx(struct ieee80211_device *ieee,
1051                      const u8 *buffer, unsigned int length,
1052                      struct ieee80211_rx_stats *stats)
1053 {
1054         struct ieee80211_hdr_4addr *hdr;
1055         u16 fc;
1056
1057         if (ieee->iw_mode == IW_MODE_MONITOR)
1058                 return 1;
1059
1060         hdr = (struct ieee80211_hdr_4addr *)buffer;
1061         fc = le16_to_cpu(hdr->frame_ctl);
1062         if ((fc & IEEE80211_FCTL_VERS) != 0)
1063                 return -EINVAL;
1064
1065         switch (WLAN_FC_GET_TYPE(fc)) {
1066         case IEEE80211_FTYPE_MGMT:
1067                 if (length < sizeof(struct ieee80211_hdr_3addr))
1068                         return -EINVAL;
1069                 ieee80211_rx_mgt(ieee, hdr, stats);
1070                 return 0;
1071         case IEEE80211_FTYPE_CTL:
1072                 return 0;
1073         case IEEE80211_FTYPE_DATA:
1074                 /* Ignore invalid short buffers */
1075                 if (length < sizeof(struct ieee80211_hdr_3addr))
1076                         return -EINVAL;
1077                 return is_data_packet_for_us(ieee, hdr);
1078         }
1079
1080         return -EINVAL;
1081 }
1082
1083 static void update_qual_rssi(struct zd_mac *mac,
1084                              const u8 *buffer, unsigned int length,
1085                              u8 qual_percent, u8 rssi_percent)
1086 {
1087         unsigned long flags;
1088         struct ieee80211_hdr_3addr *hdr;
1089         int i;
1090
1091         hdr = (struct ieee80211_hdr_3addr *)buffer;
1092         if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
1093                 return;
1094         if (compare_ether_addr(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid) != 0)
1095                 return;
1096
1097         spin_lock_irqsave(&mac->lock, flags);
1098         i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
1099         mac->qual_buffer[i] = qual_percent;
1100         mac->rssi_buffer[i] = rssi_percent;
1101         mac->stats_count++;
1102         spin_unlock_irqrestore(&mac->lock, flags);
1103 }
1104
1105 static int fill_rx_stats(struct ieee80211_rx_stats *stats,
1106                          const struct rx_status **pstatus,
1107                          struct zd_mac *mac,
1108                          const u8 *buffer, unsigned int length)
1109 {
1110         const struct rx_status *status;
1111
1112         *pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
1113         if (status->frame_status & ZD_RX_ERROR) {
1114                 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1115                 ieee->stats.rx_errors++;
1116                 if (status->frame_status & ZD_RX_TIMEOUT_ERROR)
1117                         ieee->stats.rx_missed_errors++;
1118                 else if (status->frame_status & ZD_RX_FIFO_OVERRUN_ERROR)
1119                         ieee->stats.rx_fifo_errors++;
1120                 else if (status->frame_status & ZD_RX_DECRYPTION_ERROR)
1121                         ieee->ieee_stats.rx_discards_undecryptable++;
1122                 else if (status->frame_status & ZD_RX_CRC32_ERROR) {
1123                         ieee->stats.rx_crc_errors++;
1124                         ieee->ieee_stats.rx_fcs_errors++;
1125                 }
1126                 else if (status->frame_status & ZD_RX_CRC16_ERROR)
1127                         ieee->stats.rx_crc_errors++;
1128                 return -EINVAL;
1129         }
1130
1131         memset(stats, 0, sizeof(struct ieee80211_rx_stats));
1132         stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
1133                                + sizeof(struct rx_status));
1134         /* FIXME: 802.11a */
1135         stats->freq = IEEE80211_24GHZ_BAND;
1136         stats->received_channel = _zd_chip_get_channel(&mac->chip);
1137         stats->rssi = zd_rx_strength_percent(status->signal_strength);
1138         stats->signal = zd_rx_qual_percent(buffer,
1139                                           length - sizeof(struct rx_status),
1140                                           status);
1141         stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
1142         stats->rate = zd_rx_rate(buffer, status);
1143         if (stats->rate)
1144                 stats->mask |= IEEE80211_STATMASK_RATE;
1145
1146         return 0;
1147 }
1148
1149 static void zd_mac_rx(struct zd_mac *mac, struct sk_buff *skb)
1150 {
1151         int r;
1152         struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1153         struct ieee80211_rx_stats stats;
1154         const struct rx_status *status;
1155
1156         if (skb->len < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
1157                        IEEE80211_FCS_LEN + sizeof(struct rx_status))
1158         {
1159                 ieee->stats.rx_errors++;
1160                 ieee->stats.rx_length_errors++;
1161                 goto free_skb;
1162         }
1163
1164         r = fill_rx_stats(&stats, &status, mac, skb->data, skb->len);
1165         if (r) {
1166                 /* Only packets with rx errors are included here.
1167                  * The error stats have already been set in fill_rx_stats.
1168                  */
1169                 goto free_skb;
1170         }
1171
1172         __skb_pull(skb, ZD_PLCP_HEADER_SIZE);
1173         __skb_trim(skb, skb->len -
1174                         (IEEE80211_FCS_LEN + sizeof(struct rx_status)));
1175
1176         update_qual_rssi(mac, skb->data, skb->len, stats.signal,
1177                          status->signal_strength);
1178
1179         r = filter_rx(ieee, skb->data, skb->len, &stats);
1180         if (r <= 0) {
1181                 if (r < 0) {
1182                         ieee->stats.rx_errors++;
1183                         dev_dbg_f(zd_mac_dev(mac), "Error in packet.\n");
1184                 }
1185                 goto free_skb;
1186         }
1187
1188         if (ieee->iw_mode == IW_MODE_MONITOR)
1189                 fill_rt_header(skb_push(skb, sizeof(struct zd_rt_hdr)), mac,
1190                                &stats, status);
1191
1192         r = ieee80211_rx(ieee, skb, &stats);
1193         if (r)
1194                 return;
1195 free_skb:
1196         /* We are always in a soft irq. */
1197         dev_kfree_skb(skb);
1198 }
1199
1200 static void do_rx(unsigned long mac_ptr)
1201 {
1202         struct zd_mac *mac = (struct zd_mac *)mac_ptr;
1203         struct sk_buff *skb;
1204
1205         while ((skb = skb_dequeue(&mac->rx_queue)) != NULL)
1206                 zd_mac_rx(mac, skb);
1207 }
1208
1209 int zd_mac_rx_irq(struct zd_mac *mac, const u8 *buffer, unsigned int length)
1210 {
1211         struct sk_buff *skb;
1212
1213         skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
1214         if (!skb) {
1215                 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1216                 dev_warn(zd_mac_dev(mac), "Could not allocate skb.\n");
1217                 ieee->stats.rx_dropped++;
1218                 return -ENOMEM;
1219         }
1220         skb_reserve(skb, sizeof(struct zd_rt_hdr));
1221         memcpy(__skb_put(skb, length), buffer, length);
1222         skb_queue_tail(&mac->rx_queue, skb);
1223         tasklet_schedule(&mac->rx_tasklet);
1224         return 0;
1225 }
1226
1227 static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
1228                      int pri)
1229 {
1230         return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
1231 }
1232
1233 static void set_security(struct net_device *netdev,
1234                          struct ieee80211_security *sec)
1235 {
1236         struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
1237         struct ieee80211_security *secinfo = &ieee->sec;
1238         int keyidx;
1239
1240         dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
1241
1242         for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
1243                 if (sec->flags & (1<<keyidx)) {
1244                         secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
1245                         secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
1246                         memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
1247                                SCM_KEY_LEN);
1248                 }
1249
1250         if (sec->flags & SEC_ACTIVE_KEY) {
1251                 secinfo->active_key = sec->active_key;
1252                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1253                         "   .active_key = %d\n", sec->active_key);
1254         }
1255         if (sec->flags & SEC_UNICAST_GROUP) {
1256                 secinfo->unicast_uses_group = sec->unicast_uses_group;
1257                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1258                         "   .unicast_uses_group = %d\n",
1259                         sec->unicast_uses_group);
1260         }
1261         if (sec->flags & SEC_LEVEL) {
1262                 secinfo->level = sec->level;
1263                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1264                         "   .level = %d\n", sec->level);
1265         }
1266         if (sec->flags & SEC_ENABLED) {
1267                 secinfo->enabled = sec->enabled;
1268                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1269                         "   .enabled = %d\n", sec->enabled);
1270         }
1271         if (sec->flags & SEC_ENCRYPT) {
1272                 secinfo->encrypt = sec->encrypt;
1273                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1274                         "   .encrypt = %d\n", sec->encrypt);
1275         }
1276         if (sec->flags & SEC_AUTH_MODE) {
1277                 secinfo->auth_mode = sec->auth_mode;
1278                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1279                         "   .auth_mode = %d\n", sec->auth_mode);
1280         }
1281 }
1282
1283 static void ieee_init(struct ieee80211_device *ieee)
1284 {
1285         ieee->mode = IEEE_B | IEEE_G;
1286         ieee->freq_band = IEEE80211_24GHZ_BAND;
1287         ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
1288         ieee->tx_headroom = sizeof(struct zd_ctrlset);
1289         ieee->set_security = set_security;
1290         ieee->hard_start_xmit = netdev_tx;
1291
1292         /* Software encryption/decryption for now */
1293         ieee->host_build_iv = 0;
1294         ieee->host_encrypt = 1;
1295         ieee->host_decrypt = 1;
1296
1297         /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
1298          * correctly support AUTO */
1299         ieee->iw_mode = IW_MODE_INFRA;
1300 }
1301
1302 static void softmac_init(struct ieee80211softmac_device *sm)
1303 {
1304         sm->set_channel = set_channel;
1305         sm->bssinfo_change = bssinfo_change;
1306 }
1307
1308 struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
1309 {
1310         struct zd_mac *mac = zd_netdev_mac(ndev);
1311         struct iw_statistics *iw_stats = &mac->iw_stats;
1312         unsigned int i, count, qual_total, rssi_total;
1313
1314         memset(iw_stats, 0, sizeof(struct iw_statistics));
1315         /* We are not setting the status, because ieee->state is not updated
1316          * at all and this driver doesn't track authentication state.
1317          */
1318         spin_lock_irq(&mac->lock);
1319         count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
1320                 mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
1321         qual_total = rssi_total = 0;
1322         for (i = 0; i < count; i++) {
1323                 qual_total += mac->qual_buffer[i];
1324                 rssi_total += mac->rssi_buffer[i];
1325         }
1326         spin_unlock_irq(&mac->lock);
1327         iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
1328         if (count > 0) {
1329                 iw_stats->qual.qual = qual_total / count;
1330                 iw_stats->qual.level = rssi_total / count;
1331                 iw_stats->qual.updated |=
1332                         IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
1333         } else {
1334                 iw_stats->qual.updated |=
1335                         IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
1336         }
1337         /* TODO: update counter */
1338         return iw_stats;
1339 }
1340
1341 #define LINK_LED_WORK_DELAY HZ
1342
1343 static void link_led_handler(struct work_struct *work)
1344 {
1345         struct zd_mac *mac =
1346                 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
1347         struct zd_chip *chip = &mac->chip;
1348         struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
1349         int is_associated;
1350         int r;
1351
1352         spin_lock_irq(&mac->lock);
1353         is_associated = sm->associnfo.associated != 0;
1354         spin_unlock_irq(&mac->lock);
1355
1356         r = zd_chip_control_leds(chip,
1357                                  is_associated ? LED_ASSOCIATED : LED_SCANNING);
1358         if (r)
1359                 dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1360
1361         queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1362                            LINK_LED_WORK_DELAY);
1363 }
1364
1365 static void housekeeping_init(struct zd_mac *mac)
1366 {
1367         INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
1368 }
1369
1370 static void housekeeping_enable(struct zd_mac *mac)
1371 {
1372         dev_dbg_f(zd_mac_dev(mac), "\n");
1373         queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1374                            0);
1375 }
1376
1377 static void housekeeping_disable(struct zd_mac *mac)
1378 {
1379         dev_dbg_f(zd_mac_dev(mac), "\n");
1380         cancel_rearming_delayed_workqueue(zd_workqueue,
1381                 &mac->housekeeping.link_led_work);
1382         zd_chip_control_leds(&mac->chip, LED_OFF);
1383 }