blob: a9bd80a08613518311ec69a3a0823a9a29eb1aa6 [file] [log] [blame]
Daniel Drakee85d0912006-06-02 17:11:32 +01001/* 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
33static void ieee_init(struct ieee80211_device *ieee);
34static void softmac_init(struct ieee80211softmac_device *sm);
35
36int 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
52static 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;
66out:
67 spin_unlock_irqrestore(&mac->lock, flags);
68 return r;
69}
70
71int 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
Daniel Drake40da08b2006-08-01 23:43:32 +0200111 /* 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);
Daniel Drakee85d0912006-06-02 17:11:32 +0100114 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;
122disable_int:
123 zd_chip_disable_int(chip);
124out:
125 return r;
126}
127
128void 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
137static int reset_mode(struct zd_mac *mac)
138{
139 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
140 struct zd_ioreq32 ioreqs[3] = {
Ulrich Kunitzfde627b2006-08-01 23:43:35 +0200141 { CR_RX_FILTER, STA_RX_FILTER },
Daniel Drakee85d0912006-06-02 17:11:32 +0100142 { CR_SNIFFER_ON, 0U },
Daniel Drakee85d0912006-06-02 17:11:32 +0100143 };
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
154int 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;
185disable_rx:
186 zd_chip_disable_rx(chip);
187disable_radio:
188 zd_chip_switch_radio_off(chip);
189disable_int:
190 zd_chip_disable_int(chip);
191out:
192 return r;
193}
194
195int 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
Daniel Drakec9a4b352006-06-11 23:18:54 +0100200 netif_stop_queue(netdev);
201
Daniel Drakee85d0912006-06-02 17:11:32 +0100202 /*
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
218int 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
243int 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
273u8 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
284static 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. */
294int 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
315int 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 */
333static 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. */
355static 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
384int 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
414int 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
426int 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
482static 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
525enum {
526 R2M_SHORT_PREAMBLE = 0x01,
527 R2M_11A = 0x02,
528};
529
530static 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
551static 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
567static 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
621static 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
691static 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
713struct zd_rt_hdr {
714 struct ieee80211_radiotap_header rt_hdr;
715 u8 rt_flags;
Ulrich Kunitz99f65f22006-08-01 23:43:30 +0200716 u8 rt_rate;
Daniel Drakee85d0912006-06-02 17:11:32 +0100717 u16 rt_channel;
718 u16 rt_chbitmask;
Ulrich Kunitz99f65f22006-08-01 23:43:30 +0200719} __attribute__((packed));
Daniel Drakee85d0912006-06-02 17:11:32 +0100720
721static 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
Ulrich Kunitz99f65f22006-08-01 23:43:30 +0200738 hdr->rt_rate = stats->rate / 5;
739
Daniel Drakee85d0912006-06-02 17:11:32 +0100740 /* 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));
Daniel Drakee85d0912006-06-02 17:11:32 +0100746}
747
748/* Returns 1 if the data packet is for us and 0 otherwise. */
749static 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 */
786static 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
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +0100819static void update_qual_rssi(struct zd_mac *mac,
820 const u8 *buffer, unsigned int length,
821 u8 qual_percent, u8 rssi_percent)
Daniel Drakee85d0912006-06-02 17:11:32 +0100822{
823 unsigned long flags;
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +0100824 struct ieee80211_hdr_3addr *hdr;
825 int i;
826
827 hdr = (struct ieee80211_hdr_3addr *)buffer;
828 if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
829 return;
830 if (memcmp(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid, ETH_ALEN) != 0)
831 return;
Daniel Drakee85d0912006-06-02 17:11:32 +0100832
833 spin_lock_irqsave(&mac->lock, flags);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +0100834 i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
835 mac->qual_buffer[i] = qual_percent;
836 mac->rssi_buffer[i] = rssi_percent;
837 mac->stats_count++;
Daniel Drakee85d0912006-06-02 17:11:32 +0100838 spin_unlock_irqrestore(&mac->lock, flags);
839}
840
841static int fill_rx_stats(struct ieee80211_rx_stats *stats,
842 const struct rx_status **pstatus,
843 struct zd_mac *mac,
844 const u8 *buffer, unsigned int length)
845{
846 const struct rx_status *status;
847
848 *pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
849 if (status->frame_status & ZD_RX_ERROR) {
850 /* FIXME: update? */
851 return -EINVAL;
852 }
853 memset(stats, 0, sizeof(struct ieee80211_rx_stats));
854 stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
855 + sizeof(struct rx_status));
856 /* FIXME: 802.11a */
857 stats->freq = IEEE80211_24GHZ_BAND;
858 stats->received_channel = _zd_chip_get_channel(&mac->chip);
859 stats->rssi = zd_rx_strength_percent(status->signal_strength);
860 stats->signal = zd_rx_qual_percent(buffer,
861 length - sizeof(struct rx_status),
862 status);
863 stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
864 stats->rate = zd_rx_rate(buffer, status);
865 if (stats->rate)
866 stats->mask |= IEEE80211_STATMASK_RATE;
867
Daniel Drakee85d0912006-06-02 17:11:32 +0100868 return 0;
869}
870
871int zd_mac_rx(struct zd_mac *mac, const u8 *buffer, unsigned int length)
872{
873 int r;
874 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
875 struct ieee80211_rx_stats stats;
876 const struct rx_status *status;
877 struct sk_buff *skb;
878
879 if (length < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
880 IEEE80211_FCS_LEN + sizeof(struct rx_status))
881 return -EINVAL;
882
883 r = fill_rx_stats(&stats, &status, mac, buffer, length);
884 if (r)
885 return r;
886
887 length -= ZD_PLCP_HEADER_SIZE+IEEE80211_FCS_LEN+
888 sizeof(struct rx_status);
889 buffer += ZD_PLCP_HEADER_SIZE;
890
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +0100891 update_qual_rssi(mac, buffer, length, stats.signal, stats.rssi);
892
Daniel Drakee85d0912006-06-02 17:11:32 +0100893 r = filter_rx(ieee, buffer, length, &stats);
894 if (r <= 0)
895 return r;
896
897 skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
898 if (!skb)
899 return -ENOMEM;
900 if (ieee->iw_mode == IW_MODE_MONITOR)
901 fill_rt_header(skb_put(skb, sizeof(struct zd_rt_hdr)), mac,
902 &stats, status);
903 memcpy(skb_put(skb, length), buffer, length);
904
905 r = ieee80211_rx(ieee, skb, &stats);
906 if (!r) {
907 ZD_ASSERT(in_irq());
908 dev_kfree_skb_irq(skb);
909 }
910 return 0;
911}
912
913static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
914 int pri)
915{
916 return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
917}
918
919static void set_security(struct net_device *netdev,
920 struct ieee80211_security *sec)
921{
922 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
923 struct ieee80211_security *secinfo = &ieee->sec;
924 int keyidx;
925
926 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
927
928 for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
929 if (sec->flags & (1<<keyidx)) {
930 secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
931 secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
932 memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
933 SCM_KEY_LEN);
934 }
935
936 if (sec->flags & SEC_ACTIVE_KEY) {
937 secinfo->active_key = sec->active_key;
938 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
939 " .active_key = %d\n", sec->active_key);
940 }
941 if (sec->flags & SEC_UNICAST_GROUP) {
942 secinfo->unicast_uses_group = sec->unicast_uses_group;
943 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
944 " .unicast_uses_group = %d\n",
945 sec->unicast_uses_group);
946 }
947 if (sec->flags & SEC_LEVEL) {
948 secinfo->level = sec->level;
949 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
950 " .level = %d\n", sec->level);
951 }
952 if (sec->flags & SEC_ENABLED) {
953 secinfo->enabled = sec->enabled;
954 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
955 " .enabled = %d\n", sec->enabled);
956 }
957 if (sec->flags & SEC_ENCRYPT) {
958 secinfo->encrypt = sec->encrypt;
959 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
960 " .encrypt = %d\n", sec->encrypt);
961 }
962 if (sec->flags & SEC_AUTH_MODE) {
963 secinfo->auth_mode = sec->auth_mode;
964 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
965 " .auth_mode = %d\n", sec->auth_mode);
966 }
967}
968
969static void ieee_init(struct ieee80211_device *ieee)
970{
971 ieee->mode = IEEE_B | IEEE_G;
972 ieee->freq_band = IEEE80211_24GHZ_BAND;
973 ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
974 ieee->tx_headroom = sizeof(struct zd_ctrlset);
975 ieee->set_security = set_security;
976 ieee->hard_start_xmit = netdev_tx;
977
978 /* Software encryption/decryption for now */
979 ieee->host_build_iv = 0;
980 ieee->host_encrypt = 1;
981 ieee->host_decrypt = 1;
982
983 /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
984 * correctly support AUTO */
985 ieee->iw_mode = IW_MODE_INFRA;
986}
987
988static void softmac_init(struct ieee80211softmac_device *sm)
989{
990 sm->set_channel = set_channel;
991}
992
993struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
994{
995 struct zd_mac *mac = zd_netdev_mac(ndev);
996 struct iw_statistics *iw_stats = &mac->iw_stats;
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +0100997 unsigned int i, count, qual_total, rssi_total;
Daniel Drakee85d0912006-06-02 17:11:32 +0100998
999 memset(iw_stats, 0, sizeof(struct iw_statistics));
1000 /* We are not setting the status, because ieee->state is not updated
1001 * at all and this driver doesn't track authentication state.
1002 */
1003 spin_lock_irq(&mac->lock);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001004 count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
1005 mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
1006 qual_total = rssi_total = 0;
1007 for (i = 0; i < count; i++) {
1008 qual_total += mac->qual_buffer[i];
1009 rssi_total += mac->rssi_buffer[i];
1010 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001011 spin_unlock_irq(&mac->lock);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001012 iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
1013 if (count > 0) {
1014 iw_stats->qual.qual = qual_total / count;
1015 iw_stats->qual.level = rssi_total / count;
1016 iw_stats->qual.updated |=
1017 IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
1018 } else {
1019 iw_stats->qual.updated |=
1020 IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
1021 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001022 /* TODO: update counter */
1023 return iw_stats;
1024}
1025
1026#ifdef DEBUG
1027static const char* decryption_types[] = {
1028 [ZD_RX_NO_WEP] = "none",
1029 [ZD_RX_WEP64] = "WEP64",
1030 [ZD_RX_TKIP] = "TKIP",
1031 [ZD_RX_AES] = "AES",
1032 [ZD_RX_WEP128] = "WEP128",
1033 [ZD_RX_WEP256] = "WEP256",
1034};
1035
1036static const char *decryption_type_string(u8 type)
1037{
1038 const char *s;
1039
1040 if (type < ARRAY_SIZE(decryption_types)) {
1041 s = decryption_types[type];
1042 } else {
1043 s = NULL;
1044 }
1045 return s ? s : "unknown";
1046}
1047
1048static int is_ofdm(u8 frame_status)
1049{
1050 return (frame_status & ZD_RX_OFDM);
1051}
1052
1053void zd_dump_rx_status(const struct rx_status *status)
1054{
1055 const char* modulation;
1056 u8 quality;
1057
1058 if (is_ofdm(status->frame_status)) {
1059 modulation = "ofdm";
1060 quality = status->signal_quality_ofdm;
1061 } else {
1062 modulation = "cck";
1063 quality = status->signal_quality_cck;
1064 }
1065 pr_debug("rx status %s strength %#04x qual %#04x decryption %s\n",
1066 modulation, status->signal_strength, quality,
1067 decryption_type_string(status->decryption_type));
1068 if (status->frame_status & ZD_RX_ERROR) {
1069 pr_debug("rx error %s%s%s%s%s%s\n",
1070 (status->frame_status & ZD_RX_TIMEOUT_ERROR) ?
1071 "timeout " : "",
1072 (status->frame_status & ZD_RX_FIFO_OVERRUN_ERROR) ?
1073 "fifo " : "",
1074 (status->frame_status & ZD_RX_DECRYPTION_ERROR) ?
1075 "decryption " : "",
1076 (status->frame_status & ZD_RX_CRC32_ERROR) ?
1077 "crc32 " : "",
1078 (status->frame_status & ZD_RX_NO_ADDR1_MATCH_ERROR) ?
1079 "addr1 " : "",
1080 (status->frame_status & ZD_RX_CRC16_ERROR) ?
1081 "crc16" : "");
1082 }
1083}
1084#endif /* DEBUG */