1cf1fdab3c05ce981dcaf4c57db48ee6a18dbbc7
[linux-2.6.git] / drivers / net / wireless / zd1211rw / zd_mac.c
1 /* zd_mac.c
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17
18 #include <linux/netdevice.h>
19 #include <linux/etherdevice.h>
20 #include <linux/wireless.h>
21 #include <linux/usb.h>
22 #include <linux/jiffies.h>
23 #include <net/ieee80211_radiotap.h>
24
25 #include "zd_def.h"
26 #include "zd_chip.h"
27 #include "zd_mac.h"
28 #include "zd_ieee80211.h"
29 #include "zd_netdev.h"
30 #include "zd_rf.h"
31 #include "zd_util.h"
32
33 static void ieee_init(struct ieee80211_device *ieee);
34 static void softmac_init(struct ieee80211softmac_device *sm);
35
36 int zd_mac_init(struct zd_mac *mac,
37                 struct net_device *netdev,
38                 struct usb_interface *intf)
39 {
40         struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
41
42         memset(mac, 0, sizeof(*mac));
43         spin_lock_init(&mac->lock);
44         mac->netdev = netdev;
45
46         ieee_init(ieee);
47         softmac_init(ieee80211_priv(netdev));
48         zd_chip_init(&mac->chip, netdev, intf);
49         return 0;
50 }
51
52 static int reset_channel(struct zd_mac *mac)
53 {
54         int r;
55         unsigned long flags;
56         const struct channel_range *range;
57
58         spin_lock_irqsave(&mac->lock, flags);
59         range = zd_channel_range(mac->regdomain);
60         if (!range->start) {
61                 r = -EINVAL;
62                 goto out;
63         }
64         mac->requested_channel = range->start;
65         r = 0;
66 out:
67         spin_unlock_irqrestore(&mac->lock, flags);
68         return r;
69 }
70
71 int zd_mac_init_hw(struct zd_mac *mac, u8 device_type)
72 {
73         int r;
74         struct zd_chip *chip = &mac->chip;
75         u8 addr[ETH_ALEN];
76         u8 default_regdomain;
77
78         r = zd_chip_enable_int(chip);
79         if (r)
80                 goto out;
81         r = zd_chip_init_hw(chip, device_type);
82         if (r)
83                 goto disable_int;
84
85         zd_get_e2p_mac_addr(chip, addr);
86         r = zd_write_mac_addr(chip, addr);
87         if (r)
88                 goto disable_int;
89         ZD_ASSERT(!irqs_disabled());
90         spin_lock_irq(&mac->lock);
91         memcpy(mac->netdev->dev_addr, addr, ETH_ALEN);
92         spin_unlock_irq(&mac->lock);
93
94         r = zd_read_regdomain(chip, &default_regdomain);
95         if (r)
96                 goto disable_int;
97         if (!zd_regdomain_supported(default_regdomain)) {
98                 dev_dbg_f(zd_mac_dev(mac),
99                           "Regulatory Domain %#04x is not supported.\n",
100                           default_regdomain);
101                 r = -EINVAL;
102                 goto disable_int;
103         }
104         spin_lock_irq(&mac->lock);
105         mac->regdomain = mac->default_regdomain = default_regdomain;
106         spin_unlock_irq(&mac->lock);
107         r = reset_channel(mac);
108         if (r)
109                 goto disable_int;
110
111         r = zd_set_encryption_type(chip, NO_WEP);
112         if (r)
113                 goto disable_int;
114
115         r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
116         if (r)
117                 goto disable_int;
118
119         r = 0;
120 disable_int:
121         zd_chip_disable_int(chip);
122 out:
123         return r;
124 }
125
126 void zd_mac_clear(struct zd_mac *mac)
127 {
128         /* Aquire the lock. */
129         spin_lock(&mac->lock);
130         spin_unlock(&mac->lock);
131         zd_chip_clear(&mac->chip);
132         memset(mac, 0, sizeof(*mac));
133 }
134
135 static int reset_mode(struct zd_mac *mac)
136 {
137         struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
138         struct zd_ioreq32 ioreqs[3] = {
139                 { CR_RX_FILTER, RX_FILTER_BEACON | RX_FILTER_PROBE_RESPONSE |
140                                 RX_FILTER_AUTH | RX_FILTER_ASSOC_RESPONSE |
141                                 RX_FILTER_REASSOC_RESPONSE |
142                                 RX_FILTER_DISASSOC },
143                 { CR_SNIFFER_ON, 0U },
144                 { CR_ENCRYPTION_TYPE, NO_WEP },
145         };
146
147         if (ieee->iw_mode == IW_MODE_MONITOR) {
148                 ioreqs[0].value = 0xffffffff;
149                 ioreqs[1].value = 0x1;
150                 ioreqs[2].value = ENC_SNIFFER;
151         }
152
153         return zd_iowrite32a(&mac->chip, ioreqs, 3);
154 }
155
156 int zd_mac_open(struct net_device *netdev)
157 {
158         struct zd_mac *mac = zd_netdev_mac(netdev);
159         struct zd_chip *chip = &mac->chip;
160         int r;
161
162         r = zd_chip_enable_int(chip);
163         if (r < 0)
164                 goto out;
165
166         r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
167         if (r < 0)
168                 goto disable_int;
169         r = reset_mode(mac);
170         if (r)
171                 goto disable_int;
172         r = zd_chip_switch_radio_on(chip);
173         if (r < 0)
174                 goto disable_int;
175         r = zd_chip_set_channel(chip, mac->requested_channel);
176         if (r < 0)
177                 goto disable_radio;
178         r = zd_chip_enable_rx(chip);
179         if (r < 0)
180                 goto disable_radio;
181         r = zd_chip_enable_hwint(chip);
182         if (r < 0)
183                 goto disable_rx;
184
185         ieee80211softmac_start(netdev);
186         return 0;
187 disable_rx:
188         zd_chip_disable_rx(chip);
189 disable_radio:
190         zd_chip_switch_radio_off(chip);
191 disable_int:
192         zd_chip_disable_int(chip);
193 out:
194         return r;
195 }
196
197 int zd_mac_stop(struct net_device *netdev)
198 {
199         struct zd_mac *mac = zd_netdev_mac(netdev);
200         struct zd_chip *chip = &mac->chip;
201
202         netif_stop_queue(netdev);
203
204         /*
205          * The order here deliberately is a little different from the open()
206          * method, since we need to make sure there is no opportunity for RX
207          * frames to be processed by softmac after we have stopped it.
208          */
209
210         zd_chip_disable_rx(chip);
211         ieee80211softmac_stop(netdev);
212
213         zd_chip_disable_hwint(chip);
214         zd_chip_switch_radio_off(chip);
215         zd_chip_disable_int(chip);
216
217         return 0;
218 }
219
220 int zd_mac_set_mac_address(struct net_device *netdev, void *p)
221 {
222         int r;
223         unsigned long flags;
224         struct sockaddr *addr = p;
225         struct zd_mac *mac = zd_netdev_mac(netdev);
226         struct zd_chip *chip = &mac->chip;
227
228         if (!is_valid_ether_addr(addr->sa_data))
229                 return -EADDRNOTAVAIL;
230
231         dev_dbg_f(zd_mac_dev(mac),
232                   "Setting MAC to " MAC_FMT "\n", MAC_ARG(addr->sa_data));
233
234         r = zd_write_mac_addr(chip, addr->sa_data);
235         if (r)
236                 return r;
237
238         spin_lock_irqsave(&mac->lock, flags);
239         memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
240         spin_unlock_irqrestore(&mac->lock, flags);
241
242         return 0;
243 }
244
245 int zd_mac_set_regdomain(struct zd_mac *mac, u8 regdomain)
246 {
247         int r;
248         u8 channel;
249
250         ZD_ASSERT(!irqs_disabled());
251         spin_lock_irq(&mac->lock);
252         if (regdomain == 0) {
253                 regdomain = mac->default_regdomain;
254         }
255         if (!zd_regdomain_supported(regdomain)) {
256                 spin_unlock_irq(&mac->lock);
257                 return -EINVAL;
258         }
259         mac->regdomain = regdomain;
260         channel = mac->requested_channel;
261         spin_unlock_irq(&mac->lock);
262
263         r = zd_geo_init(zd_mac_to_ieee80211(mac), regdomain);
264         if (r)
265                 return r;
266         if (!zd_regdomain_supports_channel(regdomain, channel)) {
267                 r = reset_channel(mac);
268                 if (r)
269                         return r;
270         }
271
272         return 0;
273 }
274
275 u8 zd_mac_get_regdomain(struct zd_mac *mac)
276 {
277         unsigned long flags;
278         u8 regdomain;
279
280         spin_lock_irqsave(&mac->lock, flags);
281         regdomain = mac->regdomain;
282         spin_unlock_irqrestore(&mac->lock, flags);
283         return regdomain;
284 }
285
286 static void set_channel(struct net_device *netdev, u8 channel)
287 {
288         struct zd_mac *mac = zd_netdev_mac(netdev);
289
290         dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
291
292         zd_chip_set_channel(&mac->chip, channel);
293 }
294
295 /* TODO: Should not work in Managed mode. */
296 int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
297 {
298         unsigned long lock_flags;
299         struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
300
301         if (ieee->iw_mode == IW_MODE_INFRA)
302                 return -EPERM;
303
304         spin_lock_irqsave(&mac->lock, lock_flags);
305         if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
306                 spin_unlock_irqrestore(&mac->lock, lock_flags);
307                 return -EINVAL;
308         }
309         mac->requested_channel = channel;
310         spin_unlock_irqrestore(&mac->lock, lock_flags);
311         if (netif_running(mac->netdev))
312                 return zd_chip_set_channel(&mac->chip, channel);
313         else
314                 return 0;
315 }
316
317 int zd_mac_get_channel(struct zd_mac *mac, u8 *channel, u8 *flags)
318 {
319         struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
320
321         *channel = zd_chip_get_channel(&mac->chip);
322         if (ieee->iw_mode != IW_MODE_INFRA) {
323                 spin_lock_irq(&mac->lock);
324                 *flags = *channel == mac->requested_channel ?
325                         MAC_FIXED_CHANNEL : 0;
326                 spin_unlock(&mac->lock);
327         } else {
328                 *flags = 0;
329         }
330         dev_dbg_f(zd_mac_dev(mac), "channel %u flags %u\n", *channel, *flags);
331         return 0;
332 }
333
334 /* If wrong rate is given, we are falling back to the slowest rate: 1MBit/s */
335 static u8 cs_typed_rate(u8 cs_rate)
336 {
337         static const u8 typed_rates[16] = {
338                 [ZD_CS_CCK_RATE_1M]     = ZD_CS_CCK|ZD_CS_CCK_RATE_1M,
339                 [ZD_CS_CCK_RATE_2M]     = ZD_CS_CCK|ZD_CS_CCK_RATE_2M,
340                 [ZD_CS_CCK_RATE_5_5M]   = ZD_CS_CCK|ZD_CS_CCK_RATE_5_5M,
341                 [ZD_CS_CCK_RATE_11M]    = ZD_CS_CCK|ZD_CS_CCK_RATE_11M,
342                 [ZD_OFDM_RATE_6M]       = ZD_CS_OFDM|ZD_OFDM_RATE_6M,
343                 [ZD_OFDM_RATE_9M]       = ZD_CS_OFDM|ZD_OFDM_RATE_9M,
344                 [ZD_OFDM_RATE_12M]      = ZD_CS_OFDM|ZD_OFDM_RATE_12M,
345                 [ZD_OFDM_RATE_18M]      = ZD_CS_OFDM|ZD_OFDM_RATE_18M,
346                 [ZD_OFDM_RATE_24M]      = ZD_CS_OFDM|ZD_OFDM_RATE_24M,
347                 [ZD_OFDM_RATE_36M]      = ZD_CS_OFDM|ZD_OFDM_RATE_36M,
348                 [ZD_OFDM_RATE_48M]      = ZD_CS_OFDM|ZD_OFDM_RATE_48M,
349                 [ZD_OFDM_RATE_54M]      = ZD_CS_OFDM|ZD_OFDM_RATE_54M,
350         };
351
352         ZD_ASSERT(ZD_CS_RATE_MASK == 0x0f);
353         return typed_rates[cs_rate & ZD_CS_RATE_MASK];
354 }
355
356 /* Fallback to lowest rate, if rate is unknown. */
357 static u8 rate_to_cs_rate(u8 rate)
358 {
359         switch (rate) {
360         case IEEE80211_CCK_RATE_2MB:
361                 return ZD_CS_CCK_RATE_2M;
362         case IEEE80211_CCK_RATE_5MB:
363                 return ZD_CS_CCK_RATE_5_5M;
364         case IEEE80211_CCK_RATE_11MB:
365                 return ZD_CS_CCK_RATE_11M;
366         case IEEE80211_OFDM_RATE_6MB:
367                 return ZD_OFDM_RATE_6M;
368         case IEEE80211_OFDM_RATE_9MB:
369                 return ZD_OFDM_RATE_9M;
370         case IEEE80211_OFDM_RATE_12MB:
371                 return ZD_OFDM_RATE_12M;
372         case IEEE80211_OFDM_RATE_18MB:
373                 return ZD_OFDM_RATE_18M;
374         case IEEE80211_OFDM_RATE_24MB:
375                 return ZD_OFDM_RATE_24M;
376         case IEEE80211_OFDM_RATE_36MB:
377                 return ZD_OFDM_RATE_36M;
378         case IEEE80211_OFDM_RATE_48MB:
379                 return ZD_OFDM_RATE_48M;
380         case IEEE80211_OFDM_RATE_54MB:
381                 return ZD_OFDM_RATE_54M;
382         }
383         return ZD_CS_CCK_RATE_1M;
384 }
385
386 int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
387 {
388         struct ieee80211_device *ieee;
389
390         switch (mode) {
391         case IW_MODE_AUTO:
392         case IW_MODE_ADHOC:
393         case IW_MODE_INFRA:
394                 mac->netdev->type = ARPHRD_ETHER;
395                 break;
396         case IW_MODE_MONITOR:
397                 mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
398                 break;
399         default:
400                 dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
401                 return -EINVAL;
402         }
403
404         ieee = zd_mac_to_ieee80211(mac);
405         ZD_ASSERT(!irqs_disabled());
406         spin_lock_irq(&ieee->lock);
407         ieee->iw_mode = mode;
408         spin_unlock_irq(&ieee->lock);
409
410         if (netif_running(mac->netdev))
411                 return reset_mode(mac);
412
413         return 0;
414 }
415
416 int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
417 {
418         unsigned long flags;
419         struct ieee80211_device *ieee;
420
421         ieee = zd_mac_to_ieee80211(mac);
422         spin_lock_irqsave(&ieee->lock, flags);
423         *mode = ieee->iw_mode;
424         spin_unlock_irqrestore(&ieee->lock, flags);
425         return 0;
426 }
427
428 int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
429 {
430         int i;
431         const struct channel_range *channel_range;
432         u8 regdomain;
433
434         memset(range, 0, sizeof(*range));
435
436         /* FIXME: Not so important and depends on the mode. For 802.11g
437          * usually this value is used. It seems to be that Bit/s number is
438          * given here.
439          */
440         range->throughput = 27 * 1000 * 1000;
441
442         range->max_qual.qual = 100;
443         range->max_qual.level = 100;
444
445         /* FIXME: Needs still to be tuned. */
446         range->avg_qual.qual = 71;
447         range->avg_qual.level = 80;
448
449         /* FIXME: depends on standard? */
450         range->min_rts = 256;
451         range->max_rts = 2346;
452
453         range->min_frag = MIN_FRAG_THRESHOLD;
454         range->max_frag = MAX_FRAG_THRESHOLD;
455
456         range->max_encoding_tokens = WEP_KEYS;
457         range->num_encoding_sizes = 2;
458         range->encoding_size[0] = 5;
459         range->encoding_size[1] = WEP_KEY_LEN;
460
461         range->we_version_compiled = WIRELESS_EXT;
462         range->we_version_source = 20;
463
464         ZD_ASSERT(!irqs_disabled());
465         spin_lock_irq(&mac->lock);
466         regdomain = mac->regdomain;
467         spin_unlock_irq(&mac->lock);
468         channel_range = zd_channel_range(regdomain);
469
470         range->num_channels = channel_range->end - channel_range->start;
471         range->old_num_channels = range->num_channels;
472         range->num_frequency = range->num_channels;
473         range->old_num_frequency = range->num_frequency;
474
475         for (i = 0; i < range->num_frequency; i++) {
476                 struct iw_freq *freq = &range->freq[i];
477                 freq->i = channel_range->start + i;
478                 zd_channel_to_freq(freq, freq->i);
479         }
480
481         return 0;
482 }
483
484 static int zd_calc_tx_length_us(u8 *service, u8 cs_rate, u16 tx_length)
485 {
486         static const u8 rate_divisor[] = {
487                 [ZD_CS_CCK_RATE_1M]     =  1,
488                 [ZD_CS_CCK_RATE_2M]     =  2,
489                 [ZD_CS_CCK_RATE_5_5M]   = 11, /* bits must be doubled */
490                 [ZD_CS_CCK_RATE_11M]    = 11,
491                 [ZD_OFDM_RATE_6M]       =  6,
492                 [ZD_OFDM_RATE_9M]       =  9,
493                 [ZD_OFDM_RATE_12M]      = 12,
494                 [ZD_OFDM_RATE_18M]      = 18,
495                 [ZD_OFDM_RATE_24M]      = 24,
496                 [ZD_OFDM_RATE_36M]      = 36,
497                 [ZD_OFDM_RATE_48M]      = 48,
498                 [ZD_OFDM_RATE_54M]      = 54,
499         };
500
501         u32 bits = (u32)tx_length * 8;
502         u32 divisor;
503
504         divisor = rate_divisor[cs_rate];
505         if (divisor == 0)
506                 return -EINVAL;
507
508         switch (cs_rate) {
509         case ZD_CS_CCK_RATE_5_5M:
510                 bits = (2*bits) + 10; /* round up to the next integer */
511                 break;
512         case ZD_CS_CCK_RATE_11M:
513                 if (service) {
514                         u32 t = bits % 11;
515                         *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
516                         if (0 < t && t <= 3) {
517                                 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
518                         }
519                 }
520                 bits += 10; /* round up to the next integer */
521                 break;
522         }
523
524         return bits/divisor;
525 }
526
527 enum {
528         R2M_SHORT_PREAMBLE = 0x01,
529         R2M_11A            = 0x02,
530 };
531
532 static u8 cs_rate_to_modulation(u8 cs_rate, int flags)
533 {
534         u8 modulation;
535
536         modulation = cs_typed_rate(cs_rate);
537         if (flags & R2M_SHORT_PREAMBLE) {
538                 switch (ZD_CS_RATE(modulation)) {
539                 case ZD_CS_CCK_RATE_2M:
540                 case ZD_CS_CCK_RATE_5_5M:
541                 case ZD_CS_CCK_RATE_11M:
542                         modulation |= ZD_CS_CCK_PREA_SHORT;
543                         return modulation;
544                 }
545         }
546         if (flags & R2M_11A) {
547                 if (ZD_CS_TYPE(modulation) == ZD_CS_OFDM)
548                         modulation |= ZD_CS_OFDM_MODE_11A;
549         }
550         return modulation;
551 }
552
553 static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
554                               struct ieee80211_hdr_4addr *hdr)
555 {
556         struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
557         u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
558         u8 rate, cs_rate;
559         int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
560
561         /* FIXME: 802.11a? short preamble? */
562         rate = ieee80211softmac_suggest_txrate(softmac,
563                 is_multicast_ether_addr(hdr->addr1), is_mgt);
564
565         cs_rate = rate_to_cs_rate(rate);
566         cs->modulation = cs_rate_to_modulation(cs_rate, 0);
567 }
568
569 static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
570                            struct ieee80211_hdr_4addr *header)
571 {
572         unsigned int tx_length = le16_to_cpu(cs->tx_length);
573         u16 fctl = le16_to_cpu(header->frame_ctl);
574         u16 ftype = WLAN_FC_GET_TYPE(fctl);
575         u16 stype = WLAN_FC_GET_STYPE(fctl);
576
577         /*
578          * CONTROL:
579          * - start at 0x00
580          * - if fragment 0, enable bit 0
581          * - if backoff needed, enable bit 0
582          * - if burst (backoff not needed) disable bit 0
583          * - if multicast, enable bit 1
584          * - if PS-POLL frame, enable bit 2
585          * - if in INDEPENDENT_BSS mode and zd1205_DestPowerSave, then enable
586          *   bit 4 (FIXME: wtf)
587          * - if frag_len > RTS threshold, set bit 5 as long if it isnt
588          *   multicast or mgt
589          * - if bit 5 is set, and we are in OFDM mode, unset bit 5 and set bit
590          *   7
591          */
592
593         cs->control = 0;
594
595         /* First fragment */
596         if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
597                 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
598
599         /* Multicast */
600         if (is_multicast_ether_addr(header->addr1))
601                 cs->control |= ZD_CS_MULTICAST;
602
603         /* PS-POLL */
604         if (stype == IEEE80211_STYPE_PSPOLL)
605                 cs->control |= ZD_CS_PS_POLL_FRAME;
606
607         if (!is_multicast_ether_addr(header->addr1) &&
608             ftype != IEEE80211_FTYPE_MGMT &&
609             tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
610         {
611                 /* FIXME: check the logic */
612                 if (ZD_CS_TYPE(cs->modulation) == ZD_CS_OFDM) {
613                         /* 802.11g */
614                         cs->control |= ZD_CS_SELF_CTS;
615                 } else { /* 802.11b */
616                         cs->control |= ZD_CS_RTS;
617                 }
618         }
619
620         /* FIXME: Management frame? */
621 }
622
623 static int fill_ctrlset(struct zd_mac *mac,
624                         struct ieee80211_txb *txb,
625                         int frag_num)
626 {
627         int r;
628         struct sk_buff *skb = txb->fragments[frag_num];
629         struct ieee80211_hdr_4addr *hdr =
630                 (struct ieee80211_hdr_4addr *) skb->data;
631         unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
632         unsigned int next_frag_len;
633         unsigned int packet_length;
634         struct zd_ctrlset *cs = (struct zd_ctrlset *)
635                 skb_push(skb, sizeof(struct zd_ctrlset));
636
637         if (frag_num+1  < txb->nr_frags) {
638                 next_frag_len = txb->fragments[frag_num+1]->len +
639                                 IEEE80211_FCS_LEN;
640         } else {
641                 next_frag_len = 0;
642         }
643         ZD_ASSERT(frag_len <= 0xffff);
644         ZD_ASSERT(next_frag_len <= 0xffff);
645
646         cs_set_modulation(mac, cs, hdr);
647
648         cs->tx_length = cpu_to_le16(frag_len);
649
650         cs_set_control(mac, cs, hdr);
651
652         packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
653         ZD_ASSERT(packet_length <= 0xffff);
654         /* ZD1211B: Computing the length difference this way, gives us
655          * flexibility to compute the packet length.
656          */
657         cs->packet_length = cpu_to_le16(mac->chip.is_zd1211b ?
658                         packet_length - frag_len : packet_length);
659
660         /*
661          * CURRENT LENGTH:
662          * - transmit frame length in microseconds
663          * - seems to be derived from frame length
664          * - see Cal_Us_Service() in zdinlinef.h
665          * - if macp->bTxBurstEnable is enabled, then multiply by 4
666          *  - bTxBurstEnable is never set in the vendor driver
667          *
668          * SERVICE:
669          * - "for PLCP configuration"
670          * - always 0 except in some situations at 802.11b 11M
671          * - see line 53 of zdinlinef.h
672          */
673         cs->service = 0;
674         r = zd_calc_tx_length_us(&cs->service, ZD_CS_RATE(cs->modulation),
675                                  le16_to_cpu(cs->tx_length));
676         if (r < 0)
677                 return r;
678         cs->current_length = cpu_to_le16(r);
679
680         if (next_frag_len == 0) {
681                 cs->next_frame_length = 0;
682         } else {
683                 r = zd_calc_tx_length_us(NULL, ZD_CS_RATE(cs->modulation),
684                                          next_frag_len);
685                 if (r < 0)
686                         return r;
687                 cs->next_frame_length = cpu_to_le16(r);
688         }
689
690         return 0;
691 }
692
693 static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
694 {
695         int i, r;
696
697         for (i = 0; i < txb->nr_frags; i++) {
698                 struct sk_buff *skb = txb->fragments[i];
699
700                 r = fill_ctrlset(mac, txb, i);
701                 if (r)
702                         return r;
703                 r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
704                 if (r)
705                         return r;
706         }
707
708         /* FIXME: shouldn't this be handled by the upper layers? */
709         mac->netdev->trans_start = jiffies;
710
711         ieee80211_txb_free(txb);
712         return 0;
713 }
714
715 struct zd_rt_hdr {
716         struct ieee80211_radiotap_header rt_hdr;
717         u8  rt_flags;
718         u8  rt_rate;
719         u16 rt_channel;
720         u16 rt_chbitmask;
721 } __attribute__((packed));
722
723 static void fill_rt_header(void *buffer, struct zd_mac *mac,
724                            const struct ieee80211_rx_stats *stats,
725                            const struct rx_status *status)
726 {
727         struct zd_rt_hdr *hdr = buffer;
728
729         hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
730         hdr->rt_hdr.it_pad = 0;
731         hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
732         hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
733                                  (1 << IEEE80211_RADIOTAP_CHANNEL) |
734                                  (1 << IEEE80211_RADIOTAP_RATE));
735
736         hdr->rt_flags = 0;
737         if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
738                 hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
739
740         hdr->rt_rate = stats->rate / 5;
741
742         /* FIXME: 802.11a */
743         hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
744                                              _zd_chip_get_channel(&mac->chip)));
745         hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
746                 ((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
747                 ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
748 }
749
750 /* Returns 1 if the data packet is for us and 0 otherwise. */
751 static int is_data_packet_for_us(struct ieee80211_device *ieee,
752                                  struct ieee80211_hdr_4addr *hdr)
753 {
754         struct net_device *netdev = ieee->dev;
755         u16 fc = le16_to_cpu(hdr->frame_ctl);
756
757         ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
758
759         switch (ieee->iw_mode) {
760         case IW_MODE_ADHOC:
761                 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
762                     memcmp(hdr->addr3, ieee->bssid, ETH_ALEN) != 0)
763                         return 0;
764                 break;
765         case IW_MODE_AUTO:
766         case IW_MODE_INFRA:
767                 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
768                     IEEE80211_FCTL_FROMDS ||
769                     memcmp(hdr->addr2, ieee->bssid, ETH_ALEN) != 0)
770                         return 0;
771                 break;
772         default:
773                 ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
774                 return 0;
775         }
776
777         return memcmp(hdr->addr1, netdev->dev_addr, ETH_ALEN) == 0 ||
778                is_multicast_ether_addr(hdr->addr1) ||
779                (netdev->flags & IFF_PROMISC);
780 }
781
782 /* Filters receiving packets. If it returns 1 send it to ieee80211_rx, if 0
783  * return. If an error is detected -EINVAL is returned. ieee80211_rx_mgt() is
784  * called here.
785  *
786  * It has been based on ieee80211_rx_any.
787  */
788 static int filter_rx(struct ieee80211_device *ieee,
789                      const u8 *buffer, unsigned int length,
790                      struct ieee80211_rx_stats *stats)
791 {
792         struct ieee80211_hdr_4addr *hdr;
793         u16 fc;
794
795         if (ieee->iw_mode == IW_MODE_MONITOR)
796                 return 1;
797
798         hdr = (struct ieee80211_hdr_4addr *)buffer;
799         fc = le16_to_cpu(hdr->frame_ctl);
800         if ((fc & IEEE80211_FCTL_VERS) != 0)
801                 return -EINVAL;
802
803         switch (WLAN_FC_GET_TYPE(fc)) {
804         case IEEE80211_FTYPE_MGMT:
805                 if (length < sizeof(struct ieee80211_hdr_3addr))
806                         return -EINVAL;
807                 ieee80211_rx_mgt(ieee, hdr, stats);
808                 return 0;
809         case IEEE80211_FTYPE_CTL:
810                 /* Ignore invalid short buffers */
811                 return 0;
812         case IEEE80211_FTYPE_DATA:
813                 if (length < sizeof(struct ieee80211_hdr_3addr))
814                         return -EINVAL;
815                 return is_data_packet_for_us(ieee, hdr);
816         }
817
818         return -EINVAL;
819 }
820
821 static void update_qual_rssi(struct zd_mac *mac, u8 qual_percent, u8 rssi)
822 {
823         unsigned long flags;
824
825         spin_lock_irqsave(&mac->lock, flags);
826         mac->qual_average = (7 * mac->qual_average + qual_percent) / 8;
827         mac->rssi_average = (7 * mac->rssi_average + rssi) / 8;
828         spin_unlock_irqrestore(&mac->lock, flags);
829 }
830
831 static int fill_rx_stats(struct ieee80211_rx_stats *stats,
832                          const struct rx_status **pstatus,
833                          struct zd_mac *mac,
834                          const u8 *buffer, unsigned int length)
835 {
836         const struct rx_status *status;
837
838         *pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
839         if (status->frame_status & ZD_RX_ERROR) {
840                 /* FIXME: update? */
841                 return -EINVAL;
842         }
843         memset(stats, 0, sizeof(struct ieee80211_rx_stats));
844         stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
845                                + sizeof(struct rx_status));
846         /* FIXME: 802.11a */
847         stats->freq = IEEE80211_24GHZ_BAND;
848         stats->received_channel = _zd_chip_get_channel(&mac->chip);
849         stats->rssi = zd_rx_strength_percent(status->signal_strength);
850         stats->signal = zd_rx_qual_percent(buffer,
851                                           length - sizeof(struct rx_status),
852                                           status);
853         stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
854         stats->rate = zd_rx_rate(buffer, status);
855         if (stats->rate)
856                 stats->mask |= IEEE80211_STATMASK_RATE;
857
858         update_qual_rssi(mac, stats->signal, stats->rssi);
859         return 0;
860 }
861
862 int zd_mac_rx(struct zd_mac *mac, const u8 *buffer, unsigned int length)
863 {
864         int r;
865         struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
866         struct ieee80211_rx_stats stats;
867         const struct rx_status *status;
868         struct sk_buff *skb;
869
870         if (length < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
871                      IEEE80211_FCS_LEN + sizeof(struct rx_status))
872                 return -EINVAL;
873
874         r = fill_rx_stats(&stats, &status, mac, buffer, length);
875         if (r)
876                 return r;
877
878         length -= ZD_PLCP_HEADER_SIZE+IEEE80211_FCS_LEN+
879                   sizeof(struct rx_status);
880         buffer += ZD_PLCP_HEADER_SIZE;
881
882         r = filter_rx(ieee, buffer, length, &stats);
883         if (r <= 0)
884                 return r;
885
886         skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
887         if (!skb)
888                 return -ENOMEM;
889         if (ieee->iw_mode == IW_MODE_MONITOR)
890                 fill_rt_header(skb_put(skb, sizeof(struct zd_rt_hdr)), mac,
891                                &stats, status);
892         memcpy(skb_put(skb, length), buffer, length);
893
894         r = ieee80211_rx(ieee, skb, &stats);
895         if (!r) {
896                 ZD_ASSERT(in_irq());
897                 dev_kfree_skb_irq(skb);
898         }
899         return 0;
900 }
901
902 static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
903                      int pri)
904 {
905         return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
906 }
907
908 static void set_security(struct net_device *netdev,
909                          struct ieee80211_security *sec)
910 {
911         struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
912         struct ieee80211_security *secinfo = &ieee->sec;
913         int keyidx;
914
915         dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
916
917         for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
918                 if (sec->flags & (1<<keyidx)) {
919                         secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
920                         secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
921                         memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
922                                SCM_KEY_LEN);
923                 }
924
925         if (sec->flags & SEC_ACTIVE_KEY) {
926                 secinfo->active_key = sec->active_key;
927                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
928                         "   .active_key = %d\n", sec->active_key);
929         }
930         if (sec->flags & SEC_UNICAST_GROUP) {
931                 secinfo->unicast_uses_group = sec->unicast_uses_group;
932                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
933                         "   .unicast_uses_group = %d\n",
934                         sec->unicast_uses_group);
935         }
936         if (sec->flags & SEC_LEVEL) {
937                 secinfo->level = sec->level;
938                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
939                         "   .level = %d\n", sec->level);
940         }
941         if (sec->flags & SEC_ENABLED) {
942                 secinfo->enabled = sec->enabled;
943                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
944                         "   .enabled = %d\n", sec->enabled);
945         }
946         if (sec->flags & SEC_ENCRYPT) {
947                 secinfo->encrypt = sec->encrypt;
948                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
949                         "   .encrypt = %d\n", sec->encrypt);
950         }
951         if (sec->flags & SEC_AUTH_MODE) {
952                 secinfo->auth_mode = sec->auth_mode;
953                 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
954                         "   .auth_mode = %d\n", sec->auth_mode);
955         }
956 }
957
958 static void ieee_init(struct ieee80211_device *ieee)
959 {
960         ieee->mode = IEEE_B | IEEE_G;
961         ieee->freq_band = IEEE80211_24GHZ_BAND;
962         ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
963         ieee->tx_headroom = sizeof(struct zd_ctrlset);
964         ieee->set_security = set_security;
965         ieee->hard_start_xmit = netdev_tx;
966
967         /* Software encryption/decryption for now */
968         ieee->host_build_iv = 0;
969         ieee->host_encrypt = 1;
970         ieee->host_decrypt = 1;
971
972         /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
973          * correctly support AUTO */
974         ieee->iw_mode = IW_MODE_INFRA;
975 }
976
977 static void softmac_init(struct ieee80211softmac_device *sm)
978 {
979         sm->set_channel = set_channel;
980 }
981
982 struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
983 {
984         struct zd_mac *mac = zd_netdev_mac(ndev);
985         struct iw_statistics *iw_stats = &mac->iw_stats;
986
987         memset(iw_stats, 0, sizeof(struct iw_statistics));
988         /* We are not setting the status, because ieee->state is not updated
989          * at all and this driver doesn't track authentication state.
990          */
991         spin_lock_irq(&mac->lock);
992         iw_stats->qual.qual = mac->qual_average;
993         iw_stats->qual.level = mac->rssi_average;
994         iw_stats->qual.updated = IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED|
995                                  IW_QUAL_NOISE_INVALID;
996         spin_unlock_irq(&mac->lock);
997         /* TODO: update counter */
998         return iw_stats;
999 }
1000
1001 #ifdef DEBUG
1002 static const char* decryption_types[] = {
1003         [ZD_RX_NO_WEP] = "none",
1004         [ZD_RX_WEP64] = "WEP64",
1005         [ZD_RX_TKIP] = "TKIP",
1006         [ZD_RX_AES] = "AES",
1007         [ZD_RX_WEP128] = "WEP128",
1008         [ZD_RX_WEP256] = "WEP256",
1009 };
1010
1011 static const char *decryption_type_string(u8 type)
1012 {
1013         const char *s;
1014
1015         if (type < ARRAY_SIZE(decryption_types)) {
1016                 s = decryption_types[type];
1017         } else {
1018                 s = NULL;
1019         }
1020         return s ? s : "unknown";
1021 }
1022
1023 static int is_ofdm(u8 frame_status)
1024 {
1025         return (frame_status & ZD_RX_OFDM);
1026 }
1027
1028 void zd_dump_rx_status(const struct rx_status *status)
1029 {
1030         const char* modulation;
1031         u8 quality;
1032
1033         if (is_ofdm(status->frame_status)) {
1034                 modulation = "ofdm";
1035                 quality = status->signal_quality_ofdm;
1036         } else {
1037                 modulation = "cck";
1038                 quality = status->signal_quality_cck;
1039         }
1040         pr_debug("rx status %s strength %#04x qual %#04x decryption %s\n",
1041                 modulation, status->signal_strength, quality,
1042                 decryption_type_string(status->decryption_type));
1043         if (status->frame_status & ZD_RX_ERROR) {
1044                 pr_debug("rx error %s%s%s%s%s%s\n",
1045                         (status->frame_status & ZD_RX_TIMEOUT_ERROR) ?
1046                                 "timeout " : "",
1047                         (status->frame_status & ZD_RX_FIFO_OVERRUN_ERROR) ?
1048                                 "fifo " : "",
1049                         (status->frame_status & ZD_RX_DECRYPTION_ERROR) ?
1050                                 "decryption " : "",
1051                         (status->frame_status & ZD_RX_CRC32_ERROR) ?
1052                                 "crc32 " : "",
1053                         (status->frame_status & ZD_RX_NO_ADDR1_MATCH_ERROR) ?
1054                                 "addr1 " : "",
1055                         (status->frame_status & ZD_RX_CRC16_ERROR) ?
1056                                 "crc16" : "");
1057         }
1058 }
1059 #endif /* DEBUG */