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