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