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