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