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