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