Merge branch 'upstream' of master.kernel.org:/pub/scm/linux/kernel/git/linville/wirel...
[sfrench/cifs-2.6.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                 dev_dbg_f(zd_mac_dev(mac),
117                           "Regulatory Domain %#04x is not supported.\n",
118                           default_regdomain);
119                 r = -EINVAL;
120                 goto disable_int;
121         }
122         spin_lock_irq(&mac->lock);
123         mac->regdomain = mac->default_regdomain = default_regdomain;
124         spin_unlock_irq(&mac->lock);
125         r = reset_channel(mac);
126         if (r)
127                 goto disable_int;
128
129         /* We must inform the device that we are doing encryption/decryption in
130          * software at the moment. */
131         r = zd_set_encryption_type(chip, ENC_SNIFFER);
132         if (r)
133                 goto disable_int;
134
135         r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
136         if (r)
137                 goto disable_int;
138
139         r = 0;
140 disable_int:
141         zd_chip_disable_int(chip);
142 out:
143         return r;
144 }
145
146 void zd_mac_clear(struct zd_mac *mac)
147 {
148         flush_workqueue(zd_workqueue);
149         skb_queue_purge(&mac->rx_queue);
150         tasklet_kill(&mac->rx_tasklet);
151         zd_chip_clear(&mac->chip);
152         ZD_ASSERT(!spin_is_locked(&mac->lock));
153         ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
154 }
155
156 static int reset_mode(struct zd_mac *mac)
157 {
158         struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
159         struct zd_ioreq32 ioreqs[] = {
160                 { CR_RX_FILTER, STA_RX_FILTER },
161                 { CR_SNIFFER_ON, 0U },
162         };
163
164         if (ieee->iw_mode == IW_MODE_MONITOR) {
165                 ioreqs[0].value = 0xffffffff;
166                 ioreqs[1].value = 0x1;
167         }
168
169         return zd_iowrite32a(&mac->chip, ioreqs, ARRAY_SIZE(ioreqs));
170 }
171
172 int zd_mac_open(struct net_device *netdev)
173 {
174         struct zd_mac *mac = zd_netdev_mac(netdev);
175         struct zd_chip *chip = &mac->chip;
176         int r;
177
178         tasklet_enable(&mac->rx_tasklet);
179
180         r = zd_chip_enable_int(chip);
181         if (r < 0)
182                 goto out;
183
184         r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
185         if (r < 0)
186                 goto disable_int;
187         r = reset_mode(mac);
188         if (r)
189                 goto disable_int;
190         r = zd_chip_switch_radio_on(chip);
191         if (r < 0)
192                 goto disable_int;
193         r = zd_chip_set_channel(chip, mac->requested_channel);
194         if (r < 0)
195                 goto disable_radio;
196         r = zd_chip_enable_rx(chip);
197         if (r < 0)
198                 goto disable_radio;
199         r = zd_chip_enable_hwint(chip);
200         if (r < 0)
201                 goto disable_rx;
202
203         housekeeping_enable(mac);
204         ieee80211softmac_start(netdev);
205         return 0;
206 disable_rx:
207         zd_chip_disable_rx(chip);
208 disable_radio:
209         zd_chip_switch_radio_off(chip);
210 disable_int:
211         zd_chip_disable_int(chip);
212 out:
213         return r;
214 }
215
216 int zd_mac_stop(struct net_device *netdev)
217 {
218         struct zd_mac *mac = zd_netdev_mac(netdev);
219         struct zd_chip *chip = &mac->chip;
220
221         netif_stop_queue(netdev);
222
223         /*
224          * The order here deliberately is a little different from the open()
225          * method, since we need to make sure there is no opportunity for RX
226          * frames to be processed by softmac after we have stopped it.
227          */
228
229         zd_chip_disable_rx(chip);
230         skb_queue_purge(&mac->rx_queue);
231         tasklet_disable(&mac->rx_tasklet);
232         housekeeping_disable(mac);
233         ieee80211softmac_stop(netdev);
234
235         /* Ensure no work items are running or queued from this point */
236         cancel_delayed_work(&mac->set_rts_cts_work);
237         cancel_delayed_work(&mac->set_basic_rates_work);
238         flush_workqueue(zd_workqueue);
239         mac->updating_rts_rate = 0;
240         mac->updating_basic_rates = 0;
241
242         zd_chip_disable_hwint(chip);
243         zd_chip_switch_radio_off(chip);
244         zd_chip_disable_int(chip);
245
246         return 0;
247 }
248
249 int zd_mac_set_mac_address(struct net_device *netdev, void *p)
250 {
251         int r;
252         unsigned long flags;
253         struct sockaddr *addr = p;
254         struct zd_mac *mac = zd_netdev_mac(netdev);
255         struct zd_chip *chip = &mac->chip;
256
257         if (!is_valid_ether_addr(addr->sa_data))
258                 return -EADDRNOTAVAIL;
259
260         dev_dbg_f(zd_mac_dev(mac),
261                   "Setting MAC to " MAC_FMT "\n", MAC_ARG(addr->sa_data));
262
263         r = zd_write_mac_addr(chip, addr->sa_data);
264         if (r)
265                 return r;
266
267         spin_lock_irqsave(&mac->lock, flags);
268         memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
269         spin_unlock_irqrestore(&mac->lock, flags);
270
271         return 0;
272 }
273
274 static void set_multicast_hash_handler(struct work_struct *work)
275 {
276         struct zd_mac *mac = container_of(work, struct zd_mac,
277                                           set_multicast_hash_work);
278         struct zd_mc_hash hash;
279
280         spin_lock_irq(&mac->lock);
281         hash = mac->multicast_hash;
282         spin_unlock_irq(&mac->lock);
283
284         zd_chip_set_multicast_hash(&mac->chip, &hash);
285 }
286
287 void zd_mac_set_multicast_list(struct net_device *dev)
288 {
289         struct zd_mc_hash hash;
290         struct zd_mac *mac = zd_netdev_mac(dev);
291         struct dev_mc_list *mc;
292         unsigned long flags;
293
294         if (dev->flags & (IFF_PROMISC|IFF_ALLMULTI)) {
295                 zd_mc_add_all(&hash);
296         } else {
297                 zd_mc_clear(&hash);
298                 for (mc = dev->mc_list; mc; mc = mc->next) {
299                         dev_dbg_f(zd_mac_dev(mac), "mc addr " MAC_FMT "\n",
300                                   MAC_ARG(mc->dmi_addr));
301                         zd_mc_add_addr(&hash, mc->dmi_addr);
302                 }
303         }
304
305         spin_lock_irqsave(&mac->lock, flags);
306         mac->multicast_hash = hash;
307         spin_unlock_irqrestore(&mac->lock, flags);
308         queue_work(zd_workqueue, &mac->set_multicast_hash_work);
309 }
310
311 int zd_mac_set_regdomain(struct zd_mac *mac, u8 regdomain)
312 {
313         int r;
314         u8 channel;
315
316         ZD_ASSERT(!irqs_disabled());
317         spin_lock_irq(&mac->lock);
318         if (regdomain == 0) {
319                 regdomain = mac->default_regdomain;
320         }
321         if (!zd_regdomain_supported(regdomain)) {
322                 spin_unlock_irq(&mac->lock);
323                 return -EINVAL;
324         }
325         mac->regdomain = regdomain;
326         channel = mac->requested_channel;
327         spin_unlock_irq(&mac->lock);
328
329         r = zd_geo_init(zd_mac_to_ieee80211(mac), regdomain);
330         if (r)
331                 return r;
332         if (!zd_regdomain_supports_channel(regdomain, channel)) {
333                 r = reset_channel(mac);
334                 if (r)
335                         return r;
336         }
337
338         return 0;
339 }
340
341 u8 zd_mac_get_regdomain(struct zd_mac *mac)
342 {
343         unsigned long flags;
344         u8 regdomain;
345
346         spin_lock_irqsave(&mac->lock, flags);
347         regdomain = mac->regdomain;
348         spin_unlock_irqrestore(&mac->lock, flags);
349         return regdomain;
350 }
351
352 /* Fallback to lowest rate, if rate is unknown. */
353 static u8 rate_to_zd_rate(u8 rate)
354 {
355         switch (rate) {
356         case IEEE80211_CCK_RATE_2MB:
357                 return ZD_CCK_RATE_2M;
358         case IEEE80211_CCK_RATE_5MB:
359                 return ZD_CCK_RATE_5_5M;
360         case IEEE80211_CCK_RATE_11MB:
361                 return ZD_CCK_RATE_11M;
362         case IEEE80211_OFDM_RATE_6MB:
363                 return ZD_OFDM_RATE_6M;
364         case IEEE80211_OFDM_RATE_9MB:
365                 return ZD_OFDM_RATE_9M;
366         case IEEE80211_OFDM_RATE_12MB:
367                 return ZD_OFDM_RATE_12M;
368         case IEEE80211_OFDM_RATE_18MB:
369                 return ZD_OFDM_RATE_18M;
370         case IEEE80211_OFDM_RATE_24MB:
371                 return ZD_OFDM_RATE_24M;
372         case IEEE80211_OFDM_RATE_36MB:
373                 return ZD_OFDM_RATE_36M;
374         case IEEE80211_OFDM_RATE_48MB:
375                 return ZD_OFDM_RATE_48M;
376         case IEEE80211_OFDM_RATE_54MB:
377                 return ZD_OFDM_RATE_54M;
378         }
379         return ZD_CCK_RATE_1M;
380 }
381
382 static u16 rate_to_cr_rate(u8 rate)
383 {
384         switch (rate) {
385         case IEEE80211_CCK_RATE_2MB:
386                 return CR_RATE_1M;
387         case IEEE80211_CCK_RATE_5MB:
388                 return CR_RATE_5_5M;
389         case IEEE80211_CCK_RATE_11MB:
390                 return CR_RATE_11M;
391         case IEEE80211_OFDM_RATE_6MB:
392                 return CR_RATE_6M;
393         case IEEE80211_OFDM_RATE_9MB:
394                 return CR_RATE_9M;
395         case IEEE80211_OFDM_RATE_12MB:
396                 return CR_RATE_12M;
397         case IEEE80211_OFDM_RATE_18MB:
398                 return CR_RATE_18M;
399         case IEEE80211_OFDM_RATE_24MB:
400                 return CR_RATE_24M;
401         case IEEE80211_OFDM_RATE_36MB:
402                 return CR_RATE_36M;
403         case IEEE80211_OFDM_RATE_48MB:
404                 return CR_RATE_48M;
405         case IEEE80211_OFDM_RATE_54MB:
406                 return CR_RATE_54M;
407         }
408         return CR_RATE_1M;
409 }
410
411 static void try_enable_tx(struct zd_mac *mac)
412 {
413         unsigned long flags;
414
415         spin_lock_irqsave(&mac->lock, flags);
416         if (mac->updating_rts_rate == 0 && mac->updating_basic_rates == 0)
417                 netif_wake_queue(mac->netdev);
418         spin_unlock_irqrestore(&mac->lock, flags);
419 }
420
421 static void set_rts_cts_work(struct work_struct *work)
422 {
423         struct zd_mac *mac =
424                 container_of(work, struct zd_mac, set_rts_cts_work.work);
425         unsigned long flags;
426         u8 rts_rate;
427         unsigned int short_preamble;
428
429         mutex_lock(&mac->chip.mutex);
430
431         spin_lock_irqsave(&mac->lock, flags);
432         mac->updating_rts_rate = 0;
433         rts_rate = mac->rts_rate;
434         short_preamble = mac->short_preamble;
435         spin_unlock_irqrestore(&mac->lock, flags);
436
437         zd_chip_set_rts_cts_rate_locked(&mac->chip, rts_rate, short_preamble);
438         mutex_unlock(&mac->chip.mutex);
439
440         try_enable_tx(mac);
441 }
442
443 static void set_basic_rates_work(struct work_struct *work)
444 {
445         struct zd_mac *mac =
446                 container_of(work, struct zd_mac, set_basic_rates_work.work);
447         unsigned long flags;
448         u16 basic_rates;
449
450         mutex_lock(&mac->chip.mutex);
451
452         spin_lock_irqsave(&mac->lock, flags);
453         mac->updating_basic_rates = 0;
454         basic_rates = mac->basic_rates;
455         spin_unlock_irqrestore(&mac->lock, flags);
456
457         zd_chip_set_basic_rates_locked(&mac->chip, basic_rates);
458         mutex_unlock(&mac->chip.mutex);
459
460         try_enable_tx(mac);
461 }
462
463 static void bssinfo_change(struct net_device *netdev, u32 changes)
464 {
465         struct zd_mac *mac = zd_netdev_mac(netdev);
466         struct ieee80211softmac_device *softmac = ieee80211_priv(netdev);
467         struct ieee80211softmac_bss_info *bssinfo = &softmac->bssinfo;
468         int need_set_rts_cts = 0;
469         int need_set_rates = 0;
470         u16 basic_rates;
471         unsigned long flags;
472
473         dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
474
475         if (changes & IEEE80211SOFTMAC_BSSINFOCHG_SHORT_PREAMBLE) {
476                 spin_lock_irqsave(&mac->lock, flags);
477                 mac->short_preamble = bssinfo->short_preamble;
478                 spin_unlock_irqrestore(&mac->lock, flags);
479                 need_set_rts_cts = 1;
480         }
481
482         if (changes & IEEE80211SOFTMAC_BSSINFOCHG_RATES) {
483                 /* Set RTS rate to highest available basic rate */
484                 u8 hi_rate = ieee80211softmac_highest_supported_rate(softmac,
485                         &bssinfo->supported_rates, 1);
486                 hi_rate = rate_to_zd_rate(hi_rate);
487
488                 spin_lock_irqsave(&mac->lock, flags);
489                 if (hi_rate != mac->rts_rate) {
490                         mac->rts_rate = hi_rate;
491                         need_set_rts_cts = 1;
492                 }
493                 spin_unlock_irqrestore(&mac->lock, flags);
494
495                 /* Set basic rates */
496                 need_set_rates = 1;
497                 if (bssinfo->supported_rates.count == 0) {
498                         /* Allow the device to be flexible */
499                         basic_rates = CR_RATES_80211B | CR_RATES_80211G;
500                 } else {
501                         int i = 0;
502                         basic_rates = 0;
503
504                         for (i = 0; i < bssinfo->supported_rates.count; i++) {
505                                 u16 rate = bssinfo->supported_rates.rates[i];
506                                 if ((rate & IEEE80211_BASIC_RATE_MASK) == 0)
507                                         continue;
508
509                                 rate &= ~IEEE80211_BASIC_RATE_MASK;
510                                 basic_rates |= rate_to_cr_rate(rate);
511                         }
512                 }
513                 spin_lock_irqsave(&mac->lock, flags);
514                 mac->basic_rates = basic_rates;
515                 spin_unlock_irqrestore(&mac->lock, flags);
516         }
517
518         /* Schedule any changes we made above */
519
520         spin_lock_irqsave(&mac->lock, flags);
521         if (need_set_rts_cts && !mac->updating_rts_rate) {
522                 mac->updating_rts_rate = 1;
523                 netif_stop_queue(mac->netdev);
524                 queue_delayed_work(zd_workqueue, &mac->set_rts_cts_work, 0);
525         }
526         if (need_set_rates && !mac->updating_basic_rates) {
527                 mac->updating_basic_rates = 1;
528                 netif_stop_queue(mac->netdev);
529                 queue_delayed_work(zd_workqueue, &mac->set_basic_rates_work,
530                                    0);
531         }
532         spin_unlock_irqrestore(&mac->lock, flags);
533 }
534
535 static void set_channel(struct net_device *netdev, u8 channel)
536 {
537         struct zd_mac *mac = zd_netdev_mac(netdev);
538
539         dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
540
541         zd_chip_set_channel(&mac->chip, channel);
542 }
543
544 int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
545 {
546         unsigned long lock_flags;
547         struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
548
549         if (ieee->iw_mode == IW_MODE_INFRA)
550                 return -EPERM;
551
552         spin_lock_irqsave(&mac->lock, lock_flags);
553         if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
554                 spin_unlock_irqrestore(&mac->lock, lock_flags);
555                 return -EINVAL;
556         }
557         mac->requested_channel = channel;
558         spin_unlock_irqrestore(&mac->lock, lock_flags);
559         if (netif_running(mac->netdev))
560                 return zd_chip_set_channel(&mac->chip, channel);
561         else
562                 return 0;
563 }
564
565 u8 zd_mac_get_channel(struct zd_mac *mac)
566 {
567         u8 channel = zd_chip_get_channel(&mac->chip);
568
569         dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
570         return channel;
571 }
572
573 /* If wrong rate is given, we are falling back to the slowest rate: 1MBit/s */
574 static u8 zd_rate_typed(u8 zd_rate)
575 {
576         static const u8 typed_rates[16] = {
577                 [ZD_CCK_RATE_1M]        = ZD_CS_CCK|ZD_CCK_RATE_1M,
578                 [ZD_CCK_RATE_2M]        = ZD_CS_CCK|ZD_CCK_RATE_2M,
579                 [ZD_CCK_RATE_5_5M]      = ZD_CS_CCK|ZD_CCK_RATE_5_5M,
580                 [ZD_CCK_RATE_11M]       = ZD_CS_CCK|ZD_CCK_RATE_11M,
581                 [ZD_OFDM_RATE_6M]       = ZD_CS_OFDM|ZD_OFDM_RATE_6M,
582                 [ZD_OFDM_RATE_9M]       = ZD_CS_OFDM|ZD_OFDM_RATE_9M,
583                 [ZD_OFDM_RATE_12M]      = ZD_CS_OFDM|ZD_OFDM_RATE_12M,
584                 [ZD_OFDM_RATE_18M]      = ZD_CS_OFDM|ZD_OFDM_RATE_18M,
585                 [ZD_OFDM_RATE_24M]      = ZD_CS_OFDM|ZD_OFDM_RATE_24M,
586                 [ZD_OFDM_RATE_36M]      = ZD_CS_OFDM|ZD_OFDM_RATE_36M,
587                 [ZD_OFDM_RATE_48M]      = ZD_CS_OFDM|ZD_OFDM_RATE_48M,
588                 [ZD_OFDM_RATE_54M]      = ZD_CS_OFDM|ZD_OFDM_RATE_54M,
589         };
590
591         ZD_ASSERT(ZD_CS_RATE_MASK == 0x0f);
592         return typed_rates[zd_rate & ZD_CS_RATE_MASK];
593 }
594
595 int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
596 {
597         struct ieee80211_device *ieee;
598
599         switch (mode) {
600         case IW_MODE_AUTO:
601         case IW_MODE_ADHOC:
602         case IW_MODE_INFRA:
603                 mac->netdev->type = ARPHRD_ETHER;
604                 break;
605         case IW_MODE_MONITOR:
606                 mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
607                 break;
608         default:
609                 dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
610                 return -EINVAL;
611         }
612
613         ieee = zd_mac_to_ieee80211(mac);
614         ZD_ASSERT(!irqs_disabled());
615         spin_lock_irq(&ieee->lock);
616         ieee->iw_mode = mode;
617         spin_unlock_irq(&ieee->lock);
618
619         if (netif_running(mac->netdev))
620                 return reset_mode(mac);
621
622         return 0;
623 }
624
625 int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
626 {
627         unsigned long flags;
628         struct ieee80211_device *ieee;
629
630         ieee = zd_mac_to_ieee80211(mac);
631         spin_lock_irqsave(&ieee->lock, flags);
632         *mode = ieee->iw_mode;
633         spin_unlock_irqrestore(&ieee->lock, flags);
634         return 0;
635 }
636
637 int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
638 {
639         int i;
640         const struct channel_range *channel_range;
641         u8 regdomain;
642
643         memset(range, 0, sizeof(*range));
644
645         /* FIXME: Not so important and depends on the mode. For 802.11g
646          * usually this value is used. It seems to be that Bit/s number is
647          * given here.
648          */
649         range->throughput = 27 * 1000 * 1000;
650
651         range->max_qual.qual = 100;
652         range->max_qual.level = 100;
653
654         /* FIXME: Needs still to be tuned. */
655         range->avg_qual.qual = 71;
656         range->avg_qual.level = 80;
657
658         /* FIXME: depends on standard? */
659         range->min_rts = 256;
660         range->max_rts = 2346;
661
662         range->min_frag = MIN_FRAG_THRESHOLD;
663         range->max_frag = MAX_FRAG_THRESHOLD;
664
665         range->max_encoding_tokens = WEP_KEYS;
666         range->num_encoding_sizes = 2;
667         range->encoding_size[0] = 5;
668         range->encoding_size[1] = WEP_KEY_LEN;
669
670         range->we_version_compiled = WIRELESS_EXT;
671         range->we_version_source = 20;
672
673         range->enc_capa = IW_ENC_CAPA_WPA |  IW_ENC_CAPA_WPA2 |
674                           IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
675
676         ZD_ASSERT(!irqs_disabled());
677         spin_lock_irq(&mac->lock);
678         regdomain = mac->regdomain;
679         spin_unlock_irq(&mac->lock);
680         channel_range = zd_channel_range(regdomain);
681
682         range->num_channels = channel_range->end - channel_range->start;
683         range->old_num_channels = range->num_channels;
684         range->num_frequency = range->num_channels;
685         range->old_num_frequency = range->num_frequency;
686
687         for (i = 0; i < range->num_frequency; i++) {
688                 struct iw_freq *freq = &range->freq[i];
689                 freq->i = channel_range->start + i;
690                 zd_channel_to_freq(freq, freq->i);
691         }
692
693         return 0;
694 }
695
696 static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
697 {
698         static const u8 rate_divisor[] = {
699                 [ZD_CCK_RATE_1M]        =  1,
700                 [ZD_CCK_RATE_2M]        =  2,
701                 [ZD_CCK_RATE_5_5M]      = 11, /* bits must be doubled */
702                 [ZD_CCK_RATE_11M]       = 11,
703                 [ZD_OFDM_RATE_6M]       =  6,
704                 [ZD_OFDM_RATE_9M]       =  9,
705                 [ZD_OFDM_RATE_12M]      = 12,
706                 [ZD_OFDM_RATE_18M]      = 18,
707                 [ZD_OFDM_RATE_24M]      = 24,
708                 [ZD_OFDM_RATE_36M]      = 36,
709                 [ZD_OFDM_RATE_48M]      = 48,
710                 [ZD_OFDM_RATE_54M]      = 54,
711         };
712
713         u32 bits = (u32)tx_length * 8;
714         u32 divisor;
715
716         divisor = rate_divisor[zd_rate];
717         if (divisor == 0)
718                 return -EINVAL;
719
720         switch (zd_rate) {
721         case ZD_CCK_RATE_5_5M:
722                 bits = (2*bits) + 10; /* round up to the next integer */
723                 break;
724         case ZD_CCK_RATE_11M:
725                 if (service) {
726                         u32 t = bits % 11;
727                         *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
728                         if (0 < t && t <= 3) {
729                                 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
730                         }
731                 }
732                 bits += 10; /* round up to the next integer */
733                 break;
734         }
735
736         return bits/divisor;
737 }
738
739 enum {
740         R2M_SHORT_PREAMBLE = 0x01,
741         R2M_11A            = 0x02,
742 };
743
744 static u8 zd_rate_to_modulation(u8 zd_rate, int flags)
745 {
746         u8 modulation;
747
748         modulation = zd_rate_typed(zd_rate);
749         if (flags & R2M_SHORT_PREAMBLE) {
750                 switch (ZD_CS_RATE(modulation)) {
751                 case ZD_CCK_RATE_2M:
752                 case ZD_CCK_RATE_5_5M:
753                 case ZD_CCK_RATE_11M:
754                         modulation |= ZD_CS_CCK_PREA_SHORT;
755                         return modulation;
756                 }
757         }
758         if (flags & R2M_11A) {
759                 if (ZD_CS_TYPE(modulation) == ZD_CS_OFDM)
760                         modulation |= ZD_CS_OFDM_MODE_11A;
761         }
762         return modulation;
763 }
764
765 static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
766                               struct ieee80211_hdr_4addr *hdr)
767 {
768         struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
769         u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
770         u8 rate, zd_rate;
771         int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
772         int is_multicast = is_multicast_ether_addr(hdr->addr1);
773         int short_preamble = ieee80211softmac_short_preamble_ok(softmac,
774                 is_multicast, is_mgt);
775         int flags = 0;
776
777         /* FIXME: 802.11a? */
778         rate = ieee80211softmac_suggest_txrate(softmac, is_multicast, is_mgt);
779
780         if (short_preamble)
781                 flags |= R2M_SHORT_PREAMBLE;
782
783         zd_rate = rate_to_zd_rate(rate);
784         cs->modulation = zd_rate_to_modulation(zd_rate, flags);
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 (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_CS_TYPE(cs->modulation) == ZD_CS_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(mac->chip.is_zd1211b ?
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_CS_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_CS_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                     memcmp(hdr->addr3, ieee->bssid, ETH_ALEN) != 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                     memcmp(hdr->addr2, ieee->bssid, ETH_ALEN) != 0)
985                         return 0;
986                 break;
987         default:
988                 ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
989                 return 0;
990         }
991
992         return memcmp(hdr->addr1, netdev->dev_addr, ETH_ALEN) == 0 ||
993                (is_multicast_ether_addr(hdr->addr1) &&
994                 memcmp(hdr->addr3, netdev->dev_addr, ETH_ALEN) != 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 (memcmp(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid, ETH_ALEN) != 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 = zd_tail(buffer, length, sizeof(struct rx_status));
1069         if (status->frame_status & ZD_RX_ERROR) {
1070                 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1071                 ieee->stats.rx_errors++;
1072                 if (status->frame_status & ZD_RX_TIMEOUT_ERROR)
1073                         ieee->stats.rx_missed_errors++;
1074                 else if (status->frame_status & ZD_RX_FIFO_OVERRUN_ERROR)
1075                         ieee->stats.rx_fifo_errors++;
1076                 else if (status->frame_status & ZD_RX_DECRYPTION_ERROR)
1077                         ieee->ieee_stats.rx_discards_undecryptable++;
1078                 else if (status->frame_status & ZD_RX_CRC32_ERROR) {
1079                         ieee->stats.rx_crc_errors++;
1080                         ieee->ieee_stats.rx_fcs_errors++;
1081                 }
1082                 else if (status->frame_status & ZD_RX_CRC16_ERROR)
1083                         ieee->stats.rx_crc_errors++;
1084                 return -EINVAL;
1085         }
1086
1087         memset(stats, 0, sizeof(struct ieee80211_rx_stats));
1088         stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
1089                                + sizeof(struct rx_status));
1090         /* FIXME: 802.11a */
1091         stats->freq = IEEE80211_24GHZ_BAND;
1092         stats->received_channel = _zd_chip_get_channel(&mac->chip);
1093         stats->rssi = zd_rx_strength_percent(status->signal_strength);
1094         stats->signal = zd_rx_qual_percent(buffer,
1095                                           length - sizeof(struct rx_status),
1096                                           status);
1097         stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
1098         stats->rate = zd_rx_rate(buffer, status);
1099         if (stats->rate)
1100                 stats->mask |= IEEE80211_STATMASK_RATE;
1101
1102         return 0;
1103 }
1104
1105 static void zd_mac_rx(struct zd_mac *mac, struct sk_buff *skb)
1106 {
1107         int r;
1108         struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1109         struct ieee80211_rx_stats stats;
1110         const struct rx_status *status;
1111
1112         if (skb->len < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
1113                        IEEE80211_FCS_LEN + sizeof(struct rx_status))
1114         {
1115                 ieee->stats.rx_errors++;
1116                 ieee->stats.rx_length_errors++;
1117                 goto free_skb;
1118         }
1119
1120         r = fill_rx_stats(&stats, &status, mac, skb->data, skb->len);
1121         if (r) {
1122                 /* Only packets with rx errors are included here.
1123                  * The error stats have already been set in fill_rx_stats.
1124                  */
1125                 goto free_skb;
1126         }
1127
1128         __skb_pull(skb, ZD_PLCP_HEADER_SIZE);
1129         __skb_trim(skb, skb->len -
1130                         (IEEE80211_FCS_LEN + sizeof(struct rx_status)));
1131
1132         update_qual_rssi(mac, skb->data, skb->len, stats.signal,
1133                          status->signal_strength);
1134
1135         r = filter_rx(ieee, skb->data, skb->len, &stats);
1136         if (r <= 0) {
1137                 if (r < 0) {
1138                         ieee->stats.rx_errors++;
1139                         dev_dbg_f(zd_mac_dev(mac), "Error in packet.\n");
1140                 }
1141                 goto free_skb;
1142         }
1143
1144         if (ieee->iw_mode == IW_MODE_MONITOR)
1145                 fill_rt_header(skb_push(skb, sizeof(struct zd_rt_hdr)), mac,
1146                                &stats, status);
1147
1148         r = ieee80211_rx(ieee, skb, &stats);
1149         if (r)
1150                 return;
1151 free_skb:
1152         /* We are always in a soft irq. */
1153         dev_kfree_skb(skb);
1154 }
1155
1156 static void do_rx(unsigned long mac_ptr)
1157 {
1158         struct zd_mac *mac = (struct zd_mac *)mac_ptr;
1159         struct sk_buff *skb;
1160
1161         while ((skb = skb_dequeue(&mac->rx_queue)) != NULL)
1162                 zd_mac_rx(mac, skb);
1163 }
1164
1165 int zd_mac_rx_irq(struct zd_mac *mac, const u8 *buffer, unsigned int length)
1166 {
1167         struct sk_buff *skb;
1168
1169         skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
1170         if (!skb) {
1171                 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1172                 dev_warn(zd_mac_dev(mac), "Could not allocate skb.\n");
1173                 ieee->stats.rx_dropped++;
1174                 return -ENOMEM;
1175         }
1176         skb_reserve(skb, sizeof(struct zd_rt_hdr));
1177         memcpy(__skb_put(skb, length), buffer, length);
1178         skb_queue_tail(&mac->rx_queue, skb);
1179         tasklet_schedule(&mac->rx_tasklet);
1180         return 0;
1181 }
1182
1183 static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
1184                      int pri)
1185 {
1186         return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
1187 }
1188
1189 static void set_security(struct net_device *netdev,
1190                          struct ieee80211_security *sec)
1191 {
1192         struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
1193         struct ieee80211_security *secinfo = &ieee->sec;
1194         int keyidx;
1195
1196         dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
1197
1198         for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
1199                 if (sec->flags & (1<<keyidx)) {
1200                         secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
1201                         secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
1202                         memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
1203                                SCM_KEY_LEN);
1204                 }
1205
1206         if (sec->flags & SEC_ACTIVE_KEY) {
1207                 secinfo->active_key = sec->active_key;
1208                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1209                         "   .active_key = %d\n", sec->active_key);
1210         }
1211         if (sec->flags & SEC_UNICAST_GROUP) {
1212                 secinfo->unicast_uses_group = sec->unicast_uses_group;
1213                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1214                         "   .unicast_uses_group = %d\n",
1215                         sec->unicast_uses_group);
1216         }
1217         if (sec->flags & SEC_LEVEL) {
1218                 secinfo->level = sec->level;
1219                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1220                         "   .level = %d\n", sec->level);
1221         }
1222         if (sec->flags & SEC_ENABLED) {
1223                 secinfo->enabled = sec->enabled;
1224                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1225                         "   .enabled = %d\n", sec->enabled);
1226         }
1227         if (sec->flags & SEC_ENCRYPT) {
1228                 secinfo->encrypt = sec->encrypt;
1229                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1230                         "   .encrypt = %d\n", sec->encrypt);
1231         }
1232         if (sec->flags & SEC_AUTH_MODE) {
1233                 secinfo->auth_mode = sec->auth_mode;
1234                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1235                         "   .auth_mode = %d\n", sec->auth_mode);
1236         }
1237 }
1238
1239 static void ieee_init(struct ieee80211_device *ieee)
1240 {
1241         ieee->mode = IEEE_B | IEEE_G;
1242         ieee->freq_band = IEEE80211_24GHZ_BAND;
1243         ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
1244         ieee->tx_headroom = sizeof(struct zd_ctrlset);
1245         ieee->set_security = set_security;
1246         ieee->hard_start_xmit = netdev_tx;
1247
1248         /* Software encryption/decryption for now */
1249         ieee->host_build_iv = 0;
1250         ieee->host_encrypt = 1;
1251         ieee->host_decrypt = 1;
1252
1253         /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
1254          * correctly support AUTO */
1255         ieee->iw_mode = IW_MODE_INFRA;
1256 }
1257
1258 static void softmac_init(struct ieee80211softmac_device *sm)
1259 {
1260         sm->set_channel = set_channel;
1261         sm->bssinfo_change = bssinfo_change;
1262 }
1263
1264 struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
1265 {
1266         struct zd_mac *mac = zd_netdev_mac(ndev);
1267         struct iw_statistics *iw_stats = &mac->iw_stats;
1268         unsigned int i, count, qual_total, rssi_total;
1269
1270         memset(iw_stats, 0, sizeof(struct iw_statistics));
1271         /* We are not setting the status, because ieee->state is not updated
1272          * at all and this driver doesn't track authentication state.
1273          */
1274         spin_lock_irq(&mac->lock);
1275         count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
1276                 mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
1277         qual_total = rssi_total = 0;
1278         for (i = 0; i < count; i++) {
1279                 qual_total += mac->qual_buffer[i];
1280                 rssi_total += mac->rssi_buffer[i];
1281         }
1282         spin_unlock_irq(&mac->lock);
1283         iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
1284         if (count > 0) {
1285                 iw_stats->qual.qual = qual_total / count;
1286                 iw_stats->qual.level = rssi_total / count;
1287                 iw_stats->qual.updated |=
1288                         IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
1289         } else {
1290                 iw_stats->qual.updated |=
1291                         IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
1292         }
1293         /* TODO: update counter */
1294         return iw_stats;
1295 }
1296
1297 #define LINK_LED_WORK_DELAY HZ
1298
1299 static void link_led_handler(struct work_struct *work)
1300 {
1301         struct zd_mac *mac =
1302                 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
1303         struct zd_chip *chip = &mac->chip;
1304         struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
1305         int is_associated;
1306         int r;
1307
1308         spin_lock_irq(&mac->lock);
1309         is_associated = sm->associnfo.associated != 0;
1310         spin_unlock_irq(&mac->lock);
1311
1312         r = zd_chip_control_leds(chip,
1313                                  is_associated ? LED_ASSOCIATED : LED_SCANNING);
1314         if (r)
1315                 dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1316
1317         queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1318                            LINK_LED_WORK_DELAY);
1319 }
1320
1321 static void housekeeping_init(struct zd_mac *mac)
1322 {
1323         INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
1324 }
1325
1326 static void housekeeping_enable(struct zd_mac *mac)
1327 {
1328         dev_dbg_f(zd_mac_dev(mac), "\n");
1329         queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1330                            0);
1331 }
1332
1333 static void housekeeping_disable(struct zd_mac *mac)
1334 {
1335         dev_dbg_f(zd_mac_dev(mac), "\n");
1336         cancel_rearming_delayed_workqueue(zd_workqueue,
1337                 &mac->housekeeping.link_led_work);
1338         zd_chip_control_leds(&mac->chip, LED_OFF);
1339 }