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