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