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