]> err.no Git - linux-2.6/blob - drivers/net/wireless/zd1211rw/zd_mac.c
[PATCH] zd1211rw: Fixed array size issue in reset_mode
[linux-2.6] / drivers / net / wireless / zd1211rw / zd_mac.c
1 /* zd_mac.c
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17
18 #include <linux/netdevice.h>
19 #include <linux/etherdevice.h>
20 #include <linux/wireless.h>
21 #include <linux/usb.h>
22 #include <linux/jiffies.h>
23 #include <net/ieee80211_radiotap.h>
24
25 #include "zd_def.h"
26 #include "zd_chip.h"
27 #include "zd_mac.h"
28 #include "zd_ieee80211.h"
29 #include "zd_netdev.h"
30 #include "zd_rf.h"
31 #include "zd_util.h"
32
33 static void ieee_init(struct ieee80211_device *ieee);
34 static void softmac_init(struct ieee80211softmac_device *sm);
35 static void set_rts_cts_work(struct work_struct *work);
36 static void set_basic_rates_work(struct work_struct *work);
37
38 static void housekeeping_init(struct zd_mac *mac);
39 static void housekeeping_enable(struct zd_mac *mac);
40 static void housekeeping_disable(struct zd_mac *mac);
41
42 static void set_multicast_hash_handler(struct work_struct *work);
43
44 static void do_rx(unsigned long mac_ptr);
45
46 int zd_mac_init(struct zd_mac *mac,
47                 struct net_device *netdev,
48                 struct usb_interface *intf)
49 {
50         struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
51
52         memset(mac, 0, sizeof(*mac));
53         spin_lock_init(&mac->lock);
54         mac->netdev = netdev;
55         INIT_DELAYED_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
56         INIT_DELAYED_WORK(&mac->set_basic_rates_work, set_basic_rates_work);
57
58         skb_queue_head_init(&mac->rx_queue);
59         tasklet_init(&mac->rx_tasklet, do_rx, (unsigned long)mac);
60         tasklet_disable(&mac->rx_tasklet);
61
62         ieee_init(ieee);
63         softmac_init(ieee80211_priv(netdev));
64         zd_chip_init(&mac->chip, netdev, intf);
65         housekeeping_init(mac);
66         INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
67         return 0;
68 }
69
70 static int reset_channel(struct zd_mac *mac)
71 {
72         int r;
73         unsigned long flags;
74         const struct channel_range *range;
75
76         spin_lock_irqsave(&mac->lock, flags);
77         range = zd_channel_range(mac->regdomain);
78         if (!range->start) {
79                 r = -EINVAL;
80                 goto out;
81         }
82         mac->requested_channel = range->start;
83         r = 0;
84 out:
85         spin_unlock_irqrestore(&mac->lock, flags);
86         return r;
87 }
88
89 int zd_mac_init_hw(struct zd_mac *mac, u8 device_type)
90 {
91         int r;
92         struct zd_chip *chip = &mac->chip;
93         u8 addr[ETH_ALEN];
94         u8 default_regdomain;
95
96         r = zd_chip_enable_int(chip);
97         if (r)
98                 goto out;
99         r = zd_chip_init_hw(chip, device_type);
100         if (r)
101                 goto disable_int;
102
103         zd_get_e2p_mac_addr(chip, addr);
104         r = zd_write_mac_addr(chip, addr);
105         if (r)
106                 goto disable_int;
107         ZD_ASSERT(!irqs_disabled());
108         spin_lock_irq(&mac->lock);
109         memcpy(mac->netdev->dev_addr, addr, ETH_ALEN);
110         spin_unlock_irq(&mac->lock);
111
112         r = zd_read_regdomain(chip, &default_regdomain);
113         if (r)
114                 goto disable_int;
115         if (!zd_regdomain_supported(default_regdomain)) {
116                 dev_dbg_f(zd_mac_dev(mac),
117                           "Regulatory Domain %#04x is not supported.\n",
118                           default_regdomain);
119                 r = -EINVAL;
120                 goto disable_int;
121         }
122         spin_lock_irq(&mac->lock);
123         mac->regdomain = mac->default_regdomain = default_regdomain;
124         spin_unlock_irq(&mac->lock);
125         r = reset_channel(mac);
126         if (r)
127                 goto disable_int;
128
129         /* We must inform the device that we are doing encryption/decryption in
130          * software at the moment. */
131         r = zd_set_encryption_type(chip, ENC_SNIFFER);
132         if (r)
133                 goto disable_int;
134
135         r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
136         if (r)
137                 goto disable_int;
138
139         r = 0;
140 disable_int:
141         zd_chip_disable_int(chip);
142 out:
143         return r;
144 }
145
146 void zd_mac_clear(struct zd_mac *mac)
147 {
148         flush_workqueue(zd_workqueue);
149         skb_queue_purge(&mac->rx_queue);
150         tasklet_kill(&mac->rx_tasklet);
151         zd_chip_clear(&mac->chip);
152         ZD_ASSERT(!spin_is_locked(&mac->lock));
153         ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
154 }
155
156 static int reset_mode(struct zd_mac *mac)
157 {
158         struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
159         struct zd_ioreq32 ioreqs[] = {
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
907         for (i = 0; i < txb->nr_frags; i++) {
908                 struct sk_buff *skb = txb->fragments[i];
909
910                 r = fill_ctrlset(mac, txb, i);
911                 if (r)
912                         return r;
913                 r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
914                 if (r)
915                         return r;
916         }
917
918         /* FIXME: shouldn't this be handled by the upper layers? */
919         mac->netdev->trans_start = jiffies;
920
921         ieee80211_txb_free(txb);
922         return 0;
923 }
924
925 struct zd_rt_hdr {
926         struct ieee80211_radiotap_header rt_hdr;
927         u8  rt_flags;
928         u8  rt_rate;
929         u16 rt_channel;
930         u16 rt_chbitmask;
931 } __attribute__((packed));
932
933 static void fill_rt_header(void *buffer, struct zd_mac *mac,
934                            const struct ieee80211_rx_stats *stats,
935                            const struct rx_status *status)
936 {
937         struct zd_rt_hdr *hdr = buffer;
938
939         hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
940         hdr->rt_hdr.it_pad = 0;
941         hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
942         hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
943                                  (1 << IEEE80211_RADIOTAP_CHANNEL) |
944                                  (1 << IEEE80211_RADIOTAP_RATE));
945
946         hdr->rt_flags = 0;
947         if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
948                 hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
949
950         hdr->rt_rate = stats->rate / 5;
951
952         /* FIXME: 802.11a */
953         hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
954                                              _zd_chip_get_channel(&mac->chip)));
955         hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
956                 ((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
957                 ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
958 }
959
960 /* Returns 1 if the data packet is for us and 0 otherwise. */
961 static int is_data_packet_for_us(struct ieee80211_device *ieee,
962                                  struct ieee80211_hdr_4addr *hdr)
963 {
964         struct net_device *netdev = ieee->dev;
965         u16 fc = le16_to_cpu(hdr->frame_ctl);
966
967         ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
968
969         switch (ieee->iw_mode) {
970         case IW_MODE_ADHOC:
971                 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
972                     memcmp(hdr->addr3, ieee->bssid, ETH_ALEN) != 0)
973                         return 0;
974                 break;
975         case IW_MODE_AUTO:
976         case IW_MODE_INFRA:
977                 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
978                     IEEE80211_FCTL_FROMDS ||
979                     memcmp(hdr->addr2, ieee->bssid, ETH_ALEN) != 0)
980                         return 0;
981                 break;
982         default:
983                 ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
984                 return 0;
985         }
986
987         return memcmp(hdr->addr1, netdev->dev_addr, ETH_ALEN) == 0 ||
988                (is_multicast_ether_addr(hdr->addr1) &&
989                 memcmp(hdr->addr3, netdev->dev_addr, ETH_ALEN) != 0) ||
990                (netdev->flags & IFF_PROMISC);
991 }
992
993 /* Filters received packets. The function returns 1 if the packet should be
994  * forwarded to ieee80211_rx(). If the packet should be ignored the function
995  * returns 0. If an invalid packet is found the function returns -EINVAL.
996  *
997  * The function calls ieee80211_rx_mgt() directly.
998  *
999  * It has been based on ieee80211_rx_any.
1000  */
1001 static int filter_rx(struct ieee80211_device *ieee,
1002                      const u8 *buffer, unsigned int length,
1003                      struct ieee80211_rx_stats *stats)
1004 {
1005         struct ieee80211_hdr_4addr *hdr;
1006         u16 fc;
1007
1008         if (ieee->iw_mode == IW_MODE_MONITOR)
1009                 return 1;
1010
1011         hdr = (struct ieee80211_hdr_4addr *)buffer;
1012         fc = le16_to_cpu(hdr->frame_ctl);
1013         if ((fc & IEEE80211_FCTL_VERS) != 0)
1014                 return -EINVAL;
1015
1016         switch (WLAN_FC_GET_TYPE(fc)) {
1017         case IEEE80211_FTYPE_MGMT:
1018                 if (length < sizeof(struct ieee80211_hdr_3addr))
1019                         return -EINVAL;
1020                 ieee80211_rx_mgt(ieee, hdr, stats);
1021                 return 0;
1022         case IEEE80211_FTYPE_CTL:
1023                 return 0;
1024         case IEEE80211_FTYPE_DATA:
1025                 /* Ignore invalid short buffers */
1026                 if (length < sizeof(struct ieee80211_hdr_3addr))
1027                         return -EINVAL;
1028                 return is_data_packet_for_us(ieee, hdr);
1029         }
1030
1031         return -EINVAL;
1032 }
1033
1034 static void update_qual_rssi(struct zd_mac *mac,
1035                              const u8 *buffer, unsigned int length,
1036                              u8 qual_percent, u8 rssi_percent)
1037 {
1038         unsigned long flags;
1039         struct ieee80211_hdr_3addr *hdr;
1040         int i;
1041
1042         hdr = (struct ieee80211_hdr_3addr *)buffer;
1043         if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
1044                 return;
1045         if (memcmp(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid, ETH_ALEN) != 0)
1046                 return;
1047
1048         spin_lock_irqsave(&mac->lock, flags);
1049         i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
1050         mac->qual_buffer[i] = qual_percent;
1051         mac->rssi_buffer[i] = rssi_percent;
1052         mac->stats_count++;
1053         spin_unlock_irqrestore(&mac->lock, flags);
1054 }
1055
1056 static int fill_rx_stats(struct ieee80211_rx_stats *stats,
1057                          const struct rx_status **pstatus,
1058                          struct zd_mac *mac,
1059                          const u8 *buffer, unsigned int length)
1060 {
1061         const struct rx_status *status;
1062
1063         *pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
1064         if (status->frame_status & ZD_RX_ERROR) {
1065                 /* FIXME: update? */
1066                 return -EINVAL;
1067         }
1068         memset(stats, 0, sizeof(struct ieee80211_rx_stats));
1069         stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
1070                                + sizeof(struct rx_status));
1071         /* FIXME: 802.11a */
1072         stats->freq = IEEE80211_24GHZ_BAND;
1073         stats->received_channel = _zd_chip_get_channel(&mac->chip);
1074         stats->rssi = zd_rx_strength_percent(status->signal_strength);
1075         stats->signal = zd_rx_qual_percent(buffer,
1076                                           length - sizeof(struct rx_status),
1077                                           status);
1078         stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
1079         stats->rate = zd_rx_rate(buffer, status);
1080         if (stats->rate)
1081                 stats->mask |= IEEE80211_STATMASK_RATE;
1082
1083         return 0;
1084 }
1085
1086 static void zd_mac_rx(struct zd_mac *mac, struct sk_buff *skb)
1087 {
1088         int r;
1089         struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1090         struct ieee80211_rx_stats stats;
1091         const struct rx_status *status;
1092
1093         if (skb->len < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
1094                        IEEE80211_FCS_LEN + sizeof(struct rx_status))
1095         {
1096                 dev_dbg_f(zd_mac_dev(mac), "Packet with length %u to small.\n",
1097                          skb->len);
1098                 goto free_skb;
1099         }
1100
1101         r = fill_rx_stats(&stats, &status, mac, skb->data, skb->len);
1102         if (r) {
1103                 /* Only packets with rx errors are included here. */
1104                 goto free_skb;
1105         }
1106
1107         __skb_pull(skb, ZD_PLCP_HEADER_SIZE);
1108         __skb_trim(skb, skb->len -
1109                         (IEEE80211_FCS_LEN + sizeof(struct rx_status)));
1110
1111         update_qual_rssi(mac, skb->data, skb->len, stats.signal,
1112                          status->signal_strength);
1113
1114         r = filter_rx(ieee, skb->data, skb->len, &stats);
1115         if (r <= 0) {
1116                 if (r < 0)
1117                         dev_dbg_f(zd_mac_dev(mac), "Error in packet.\n");
1118                 goto free_skb;
1119         }
1120
1121         if (ieee->iw_mode == IW_MODE_MONITOR)
1122                 fill_rt_header(skb_push(skb, sizeof(struct zd_rt_hdr)), mac,
1123                                &stats, status);
1124
1125         r = ieee80211_rx(ieee, skb, &stats);
1126         if (r)
1127                 return;
1128 free_skb:
1129         /* We are always in a soft irq. */
1130         dev_kfree_skb(skb);
1131 }
1132
1133 static void do_rx(unsigned long mac_ptr)
1134 {
1135         struct zd_mac *mac = (struct zd_mac *)mac_ptr;
1136         struct sk_buff *skb;
1137
1138         while ((skb = skb_dequeue(&mac->rx_queue)) != NULL)
1139                 zd_mac_rx(mac, skb);
1140 }
1141
1142 int zd_mac_rx_irq(struct zd_mac *mac, const u8 *buffer, unsigned int length)
1143 {
1144         struct sk_buff *skb;
1145
1146         skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
1147         if (!skb) {
1148                 dev_warn(zd_mac_dev(mac), "Could not allocate skb.\n");
1149                 return -ENOMEM;
1150         }
1151         skb_reserve(skb, sizeof(struct zd_rt_hdr));
1152         memcpy(__skb_put(skb, length), buffer, length);
1153         skb_queue_tail(&mac->rx_queue, skb);
1154         tasklet_schedule(&mac->rx_tasklet);
1155         return 0;
1156 }
1157
1158 static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
1159                      int pri)
1160 {
1161         return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
1162 }
1163
1164 static void set_security(struct net_device *netdev,
1165                          struct ieee80211_security *sec)
1166 {
1167         struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
1168         struct ieee80211_security *secinfo = &ieee->sec;
1169         int keyidx;
1170
1171         dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
1172
1173         for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
1174                 if (sec->flags & (1<<keyidx)) {
1175                         secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
1176                         secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
1177                         memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
1178                                SCM_KEY_LEN);
1179                 }
1180
1181         if (sec->flags & SEC_ACTIVE_KEY) {
1182                 secinfo->active_key = sec->active_key;
1183                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1184                         "   .active_key = %d\n", sec->active_key);
1185         }
1186         if (sec->flags & SEC_UNICAST_GROUP) {
1187                 secinfo->unicast_uses_group = sec->unicast_uses_group;
1188                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1189                         "   .unicast_uses_group = %d\n",
1190                         sec->unicast_uses_group);
1191         }
1192         if (sec->flags & SEC_LEVEL) {
1193                 secinfo->level = sec->level;
1194                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1195                         "   .level = %d\n", sec->level);
1196         }
1197         if (sec->flags & SEC_ENABLED) {
1198                 secinfo->enabled = sec->enabled;
1199                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1200                         "   .enabled = %d\n", sec->enabled);
1201         }
1202         if (sec->flags & SEC_ENCRYPT) {
1203                 secinfo->encrypt = sec->encrypt;
1204                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1205                         "   .encrypt = %d\n", sec->encrypt);
1206         }
1207         if (sec->flags & SEC_AUTH_MODE) {
1208                 secinfo->auth_mode = sec->auth_mode;
1209                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1210                         "   .auth_mode = %d\n", sec->auth_mode);
1211         }
1212 }
1213
1214 static void ieee_init(struct ieee80211_device *ieee)
1215 {
1216         ieee->mode = IEEE_B | IEEE_G;
1217         ieee->freq_band = IEEE80211_24GHZ_BAND;
1218         ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
1219         ieee->tx_headroom = sizeof(struct zd_ctrlset);
1220         ieee->set_security = set_security;
1221         ieee->hard_start_xmit = netdev_tx;
1222
1223         /* Software encryption/decryption for now */
1224         ieee->host_build_iv = 0;
1225         ieee->host_encrypt = 1;
1226         ieee->host_decrypt = 1;
1227
1228         /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
1229          * correctly support AUTO */
1230         ieee->iw_mode = IW_MODE_INFRA;
1231 }
1232
1233 static void softmac_init(struct ieee80211softmac_device *sm)
1234 {
1235         sm->set_channel = set_channel;
1236         sm->bssinfo_change = bssinfo_change;
1237 }
1238
1239 struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
1240 {
1241         struct zd_mac *mac = zd_netdev_mac(ndev);
1242         struct iw_statistics *iw_stats = &mac->iw_stats;
1243         unsigned int i, count, qual_total, rssi_total;
1244
1245         memset(iw_stats, 0, sizeof(struct iw_statistics));
1246         /* We are not setting the status, because ieee->state is not updated
1247          * at all and this driver doesn't track authentication state.
1248          */
1249         spin_lock_irq(&mac->lock);
1250         count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
1251                 mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
1252         qual_total = rssi_total = 0;
1253         for (i = 0; i < count; i++) {
1254                 qual_total += mac->qual_buffer[i];
1255                 rssi_total += mac->rssi_buffer[i];
1256         }
1257         spin_unlock_irq(&mac->lock);
1258         iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
1259         if (count > 0) {
1260                 iw_stats->qual.qual = qual_total / count;
1261                 iw_stats->qual.level = rssi_total / count;
1262                 iw_stats->qual.updated |=
1263                         IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
1264         } else {
1265                 iw_stats->qual.updated |=
1266                         IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
1267         }
1268         /* TODO: update counter */
1269         return iw_stats;
1270 }
1271
1272 #define LINK_LED_WORK_DELAY HZ
1273
1274 static void link_led_handler(struct work_struct *work)
1275 {
1276         struct zd_mac *mac =
1277                 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
1278         struct zd_chip *chip = &mac->chip;
1279         struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
1280         int is_associated;
1281         int r;
1282
1283         spin_lock_irq(&mac->lock);
1284         is_associated = sm->associnfo.associated != 0;
1285         spin_unlock_irq(&mac->lock);
1286
1287         r = zd_chip_control_leds(chip,
1288                                  is_associated ? LED_ASSOCIATED : LED_SCANNING);
1289         if (r)
1290                 dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1291
1292         queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1293                            LINK_LED_WORK_DELAY);
1294 }
1295
1296 static void housekeeping_init(struct zd_mac *mac)
1297 {
1298         INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
1299 }
1300
1301 static void housekeeping_enable(struct zd_mac *mac)
1302 {
1303         dev_dbg_f(zd_mac_dev(mac), "\n");
1304         queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1305                            0);
1306 }
1307
1308 static void housekeeping_disable(struct zd_mac *mac)
1309 {
1310         dev_dbg_f(zd_mac_dev(mac), "\n");
1311         cancel_rearming_delayed_workqueue(zd_workqueue,
1312                 &mac->housekeeping.link_led_work);
1313         zd_chip_control_leds(&mac->chip, LED_OFF);
1314 }