blob: e6af18304bac263d1d2e926a97eeb25f8822f84a [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
Ulrich Kunitz583afd12006-09-13 02:42:38 +010036static void housekeeping_init(struct zd_mac *mac);
37static void housekeeping_enable(struct zd_mac *mac);
38static void housekeeping_disable(struct zd_mac *mac);
39
Daniel Drakee85d0912006-06-02 17:11:32 +010040int 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);
Ulrich Kunitz583afd12006-09-13 02:42:38 +010053 housekeeping_init(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +010054 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
Daniel Drake40da08b2006-08-01 23:43:32 +0200116 /* 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);
Daniel Drakee85d0912006-06-02 17:11:32 +0100119 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{
Daniel Drakee85d0912006-06-02 17:11:32 +0100135 zd_chip_clear(&mac->chip);
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100136 ZD_ASSERT(!spin_is_locked(&mac->lock));
137 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
Daniel Drakee85d0912006-06-02 17:11:32 +0100138}
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] = {
Ulrich Kunitzfde627b2006-08-01 23:43:35 +0200144 { CR_RX_FILTER, STA_RX_FILTER },
Daniel Drakee85d0912006-06-02 17:11:32 +0100145 { CR_SNIFFER_ON, 0U },
Daniel Drakee85d0912006-06-02 17:11:32 +0100146 };
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
Ulrich Kunitz583afd12006-09-13 02:42:38 +0100186 housekeeping_enable(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100187 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
Daniel Drakec9a4b352006-06-11 23:18:54 +0100204 netif_stop_queue(netdev);
205
Daniel Drakee85d0912006-06-02 17:11:32 +0100206 /*
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);
Ulrich Kunitz583afd12006-09-13 02:42:38 +0100213 housekeeping_disable(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100214 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
Daniel Drakee85d0912006-06-02 17:11:32 +0100298int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
299{
300 unsigned long lock_flags;
301 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
302
303 if (ieee->iw_mode == IW_MODE_INFRA)
304 return -EPERM;
305
306 spin_lock_irqsave(&mac->lock, lock_flags);
307 if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
308 spin_unlock_irqrestore(&mac->lock, lock_flags);
309 return -EINVAL;
310 }
311 mac->requested_channel = channel;
312 spin_unlock_irqrestore(&mac->lock, lock_flags);
313 if (netif_running(mac->netdev))
314 return zd_chip_set_channel(&mac->chip, channel);
315 else
316 return 0;
317}
318
Daniel Drake84bc7152006-11-22 00:05:30 +0000319u8 zd_mac_get_channel(struct zd_mac *mac)
Daniel Drakee85d0912006-06-02 17:11:32 +0100320{
Daniel Drake84bc7152006-11-22 00:05:30 +0000321 u8 channel = zd_chip_get_channel(&mac->chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100322
Daniel Drake84bc7152006-11-22 00:05:30 +0000323 dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
324 return channel;
Daniel Drakee85d0912006-06-02 17:11:32 +0100325}
326
327/* If wrong rate is given, we are falling back to the slowest rate: 1MBit/s */
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000328static u8 zd_rate_typed(u8 zd_rate)
Daniel Drakee85d0912006-06-02 17:11:32 +0100329{
330 static const u8 typed_rates[16] = {
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000331 [ZD_CCK_RATE_1M] = ZD_CS_CCK|ZD_CCK_RATE_1M,
332 [ZD_CCK_RATE_2M] = ZD_CS_CCK|ZD_CCK_RATE_2M,
333 [ZD_CCK_RATE_5_5M] = ZD_CS_CCK|ZD_CCK_RATE_5_5M,
334 [ZD_CCK_RATE_11M] = ZD_CS_CCK|ZD_CCK_RATE_11M,
Daniel Drakee85d0912006-06-02 17:11:32 +0100335 [ZD_OFDM_RATE_6M] = ZD_CS_OFDM|ZD_OFDM_RATE_6M,
336 [ZD_OFDM_RATE_9M] = ZD_CS_OFDM|ZD_OFDM_RATE_9M,
337 [ZD_OFDM_RATE_12M] = ZD_CS_OFDM|ZD_OFDM_RATE_12M,
338 [ZD_OFDM_RATE_18M] = ZD_CS_OFDM|ZD_OFDM_RATE_18M,
339 [ZD_OFDM_RATE_24M] = ZD_CS_OFDM|ZD_OFDM_RATE_24M,
340 [ZD_OFDM_RATE_36M] = ZD_CS_OFDM|ZD_OFDM_RATE_36M,
341 [ZD_OFDM_RATE_48M] = ZD_CS_OFDM|ZD_OFDM_RATE_48M,
342 [ZD_OFDM_RATE_54M] = ZD_CS_OFDM|ZD_OFDM_RATE_54M,
343 };
344
345 ZD_ASSERT(ZD_CS_RATE_MASK == 0x0f);
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000346 return typed_rates[zd_rate & ZD_CS_RATE_MASK];
Daniel Drakee85d0912006-06-02 17:11:32 +0100347}
348
349/* Fallback to lowest rate, if rate is unknown. */
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000350static u8 rate_to_zd_rate(u8 rate)
Daniel Drakee85d0912006-06-02 17:11:32 +0100351{
352 switch (rate) {
353 case IEEE80211_CCK_RATE_2MB:
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000354 return ZD_CCK_RATE_2M;
Daniel Drakee85d0912006-06-02 17:11:32 +0100355 case IEEE80211_CCK_RATE_5MB:
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000356 return ZD_CCK_RATE_5_5M;
Daniel Drakee85d0912006-06-02 17:11:32 +0100357 case IEEE80211_CCK_RATE_11MB:
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000358 return ZD_CCK_RATE_11M;
Daniel Drakee85d0912006-06-02 17:11:32 +0100359 case IEEE80211_OFDM_RATE_6MB:
360 return ZD_OFDM_RATE_6M;
361 case IEEE80211_OFDM_RATE_9MB:
362 return ZD_OFDM_RATE_9M;
363 case IEEE80211_OFDM_RATE_12MB:
364 return ZD_OFDM_RATE_12M;
365 case IEEE80211_OFDM_RATE_18MB:
366 return ZD_OFDM_RATE_18M;
367 case IEEE80211_OFDM_RATE_24MB:
368 return ZD_OFDM_RATE_24M;
369 case IEEE80211_OFDM_RATE_36MB:
370 return ZD_OFDM_RATE_36M;
371 case IEEE80211_OFDM_RATE_48MB:
372 return ZD_OFDM_RATE_48M;
373 case IEEE80211_OFDM_RATE_54MB:
374 return ZD_OFDM_RATE_54M;
375 }
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000376 return ZD_CCK_RATE_1M;
Daniel Drakee85d0912006-06-02 17:11:32 +0100377}
378
379int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
380{
381 struct ieee80211_device *ieee;
382
383 switch (mode) {
384 case IW_MODE_AUTO:
385 case IW_MODE_ADHOC:
386 case IW_MODE_INFRA:
387 mac->netdev->type = ARPHRD_ETHER;
388 break;
389 case IW_MODE_MONITOR:
390 mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
391 break;
392 default:
393 dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
394 return -EINVAL;
395 }
396
397 ieee = zd_mac_to_ieee80211(mac);
398 ZD_ASSERT(!irqs_disabled());
399 spin_lock_irq(&ieee->lock);
400 ieee->iw_mode = mode;
401 spin_unlock_irq(&ieee->lock);
402
403 if (netif_running(mac->netdev))
404 return reset_mode(mac);
405
406 return 0;
407}
408
409int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
410{
411 unsigned long flags;
412 struct ieee80211_device *ieee;
413
414 ieee = zd_mac_to_ieee80211(mac);
415 spin_lock_irqsave(&ieee->lock, flags);
416 *mode = ieee->iw_mode;
417 spin_unlock_irqrestore(&ieee->lock, flags);
418 return 0;
419}
420
421int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
422{
423 int i;
424 const struct channel_range *channel_range;
425 u8 regdomain;
426
427 memset(range, 0, sizeof(*range));
428
429 /* FIXME: Not so important and depends on the mode. For 802.11g
430 * usually this value is used. It seems to be that Bit/s number is
431 * given here.
432 */
433 range->throughput = 27 * 1000 * 1000;
434
435 range->max_qual.qual = 100;
436 range->max_qual.level = 100;
437
438 /* FIXME: Needs still to be tuned. */
439 range->avg_qual.qual = 71;
440 range->avg_qual.level = 80;
441
442 /* FIXME: depends on standard? */
443 range->min_rts = 256;
444 range->max_rts = 2346;
445
446 range->min_frag = MIN_FRAG_THRESHOLD;
447 range->max_frag = MAX_FRAG_THRESHOLD;
448
449 range->max_encoding_tokens = WEP_KEYS;
450 range->num_encoding_sizes = 2;
451 range->encoding_size[0] = 5;
452 range->encoding_size[1] = WEP_KEY_LEN;
453
454 range->we_version_compiled = WIRELESS_EXT;
455 range->we_version_source = 20;
456
457 ZD_ASSERT(!irqs_disabled());
458 spin_lock_irq(&mac->lock);
459 regdomain = mac->regdomain;
460 spin_unlock_irq(&mac->lock);
461 channel_range = zd_channel_range(regdomain);
462
463 range->num_channels = channel_range->end - channel_range->start;
464 range->old_num_channels = range->num_channels;
465 range->num_frequency = range->num_channels;
466 range->old_num_frequency = range->num_frequency;
467
468 for (i = 0; i < range->num_frequency; i++) {
469 struct iw_freq *freq = &range->freq[i];
470 freq->i = channel_range->start + i;
471 zd_channel_to_freq(freq, freq->i);
472 }
473
474 return 0;
475}
476
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000477static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
Daniel Drakee85d0912006-06-02 17:11:32 +0100478{
479 static const u8 rate_divisor[] = {
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000480 [ZD_CCK_RATE_1M] = 1,
481 [ZD_CCK_RATE_2M] = 2,
482 [ZD_CCK_RATE_5_5M] = 11, /* bits must be doubled */
483 [ZD_CCK_RATE_11M] = 11,
Daniel Drakee85d0912006-06-02 17:11:32 +0100484 [ZD_OFDM_RATE_6M] = 6,
485 [ZD_OFDM_RATE_9M] = 9,
486 [ZD_OFDM_RATE_12M] = 12,
487 [ZD_OFDM_RATE_18M] = 18,
488 [ZD_OFDM_RATE_24M] = 24,
489 [ZD_OFDM_RATE_36M] = 36,
490 [ZD_OFDM_RATE_48M] = 48,
491 [ZD_OFDM_RATE_54M] = 54,
492 };
493
494 u32 bits = (u32)tx_length * 8;
495 u32 divisor;
496
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000497 divisor = rate_divisor[zd_rate];
Daniel Drakee85d0912006-06-02 17:11:32 +0100498 if (divisor == 0)
499 return -EINVAL;
500
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000501 switch (zd_rate) {
502 case ZD_CCK_RATE_5_5M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100503 bits = (2*bits) + 10; /* round up to the next integer */
504 break;
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000505 case ZD_CCK_RATE_11M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100506 if (service) {
507 u32 t = bits % 11;
508 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
509 if (0 < t && t <= 3) {
510 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
511 }
512 }
513 bits += 10; /* round up to the next integer */
514 break;
515 }
516
517 return bits/divisor;
518}
519
520enum {
521 R2M_SHORT_PREAMBLE = 0x01,
522 R2M_11A = 0x02,
523};
524
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000525static u8 zd_rate_to_modulation(u8 zd_rate, int flags)
Daniel Drakee85d0912006-06-02 17:11:32 +0100526{
527 u8 modulation;
528
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000529 modulation = zd_rate_typed(zd_rate);
Daniel Drakee85d0912006-06-02 17:11:32 +0100530 if (flags & R2M_SHORT_PREAMBLE) {
531 switch (ZD_CS_RATE(modulation)) {
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000532 case ZD_CCK_RATE_2M:
533 case ZD_CCK_RATE_5_5M:
534 case ZD_CCK_RATE_11M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100535 modulation |= ZD_CS_CCK_PREA_SHORT;
536 return modulation;
537 }
538 }
539 if (flags & R2M_11A) {
540 if (ZD_CS_TYPE(modulation) == ZD_CS_OFDM)
541 modulation |= ZD_CS_OFDM_MODE_11A;
542 }
543 return modulation;
544}
545
546static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
547 struct ieee80211_hdr_4addr *hdr)
548{
549 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
550 u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000551 u8 rate, zd_rate;
Daniel Drakee85d0912006-06-02 17:11:32 +0100552 int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
553
554 /* FIXME: 802.11a? short preamble? */
555 rate = ieee80211softmac_suggest_txrate(softmac,
556 is_multicast_ether_addr(hdr->addr1), is_mgt);
557
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000558 zd_rate = rate_to_zd_rate(rate);
559 cs->modulation = zd_rate_to_modulation(zd_rate, 0);
Daniel Drakee85d0912006-06-02 17:11:32 +0100560}
561
562static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
563 struct ieee80211_hdr_4addr *header)
564{
565 unsigned int tx_length = le16_to_cpu(cs->tx_length);
566 u16 fctl = le16_to_cpu(header->frame_ctl);
567 u16 ftype = WLAN_FC_GET_TYPE(fctl);
568 u16 stype = WLAN_FC_GET_STYPE(fctl);
569
570 /*
571 * CONTROL:
572 * - start at 0x00
573 * - if fragment 0, enable bit 0
574 * - if backoff needed, enable bit 0
575 * - if burst (backoff not needed) disable bit 0
576 * - if multicast, enable bit 1
577 * - if PS-POLL frame, enable bit 2
578 * - if in INDEPENDENT_BSS mode and zd1205_DestPowerSave, then enable
579 * bit 4 (FIXME: wtf)
580 * - if frag_len > RTS threshold, set bit 5 as long if it isnt
581 * multicast or mgt
582 * - if bit 5 is set, and we are in OFDM mode, unset bit 5 and set bit
583 * 7
584 */
585
586 cs->control = 0;
587
588 /* First fragment */
589 if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
590 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
591
592 /* Multicast */
593 if (is_multicast_ether_addr(header->addr1))
594 cs->control |= ZD_CS_MULTICAST;
595
596 /* PS-POLL */
597 if (stype == IEEE80211_STYPE_PSPOLL)
598 cs->control |= ZD_CS_PS_POLL_FRAME;
599
600 if (!is_multicast_ether_addr(header->addr1) &&
601 ftype != IEEE80211_FTYPE_MGMT &&
602 tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
603 {
604 /* FIXME: check the logic */
605 if (ZD_CS_TYPE(cs->modulation) == ZD_CS_OFDM) {
606 /* 802.11g */
607 cs->control |= ZD_CS_SELF_CTS;
608 } else { /* 802.11b */
609 cs->control |= ZD_CS_RTS;
610 }
611 }
612
613 /* FIXME: Management frame? */
614}
615
616static int fill_ctrlset(struct zd_mac *mac,
617 struct ieee80211_txb *txb,
618 int frag_num)
619{
620 int r;
621 struct sk_buff *skb = txb->fragments[frag_num];
622 struct ieee80211_hdr_4addr *hdr =
623 (struct ieee80211_hdr_4addr *) skb->data;
624 unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
625 unsigned int next_frag_len;
626 unsigned int packet_length;
627 struct zd_ctrlset *cs = (struct zd_ctrlset *)
628 skb_push(skb, sizeof(struct zd_ctrlset));
629
630 if (frag_num+1 < txb->nr_frags) {
631 next_frag_len = txb->fragments[frag_num+1]->len +
632 IEEE80211_FCS_LEN;
633 } else {
634 next_frag_len = 0;
635 }
636 ZD_ASSERT(frag_len <= 0xffff);
637 ZD_ASSERT(next_frag_len <= 0xffff);
638
639 cs_set_modulation(mac, cs, hdr);
640
641 cs->tx_length = cpu_to_le16(frag_len);
642
643 cs_set_control(mac, cs, hdr);
644
645 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
646 ZD_ASSERT(packet_length <= 0xffff);
647 /* ZD1211B: Computing the length difference this way, gives us
648 * flexibility to compute the packet length.
649 */
650 cs->packet_length = cpu_to_le16(mac->chip.is_zd1211b ?
651 packet_length - frag_len : packet_length);
652
653 /*
654 * CURRENT LENGTH:
655 * - transmit frame length in microseconds
656 * - seems to be derived from frame length
657 * - see Cal_Us_Service() in zdinlinef.h
658 * - if macp->bTxBurstEnable is enabled, then multiply by 4
659 * - bTxBurstEnable is never set in the vendor driver
660 *
661 * SERVICE:
662 * - "for PLCP configuration"
663 * - always 0 except in some situations at 802.11b 11M
664 * - see line 53 of zdinlinef.h
665 */
666 cs->service = 0;
667 r = zd_calc_tx_length_us(&cs->service, ZD_CS_RATE(cs->modulation),
668 le16_to_cpu(cs->tx_length));
669 if (r < 0)
670 return r;
671 cs->current_length = cpu_to_le16(r);
672
673 if (next_frag_len == 0) {
674 cs->next_frame_length = 0;
675 } else {
676 r = zd_calc_tx_length_us(NULL, ZD_CS_RATE(cs->modulation),
677 next_frag_len);
678 if (r < 0)
679 return r;
680 cs->next_frame_length = cpu_to_le16(r);
681 }
682
683 return 0;
684}
685
686static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
687{
688 int i, r;
689
690 for (i = 0; i < txb->nr_frags; i++) {
691 struct sk_buff *skb = txb->fragments[i];
692
693 r = fill_ctrlset(mac, txb, i);
694 if (r)
695 return r;
696 r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
697 if (r)
698 return r;
699 }
700
701 /* FIXME: shouldn't this be handled by the upper layers? */
702 mac->netdev->trans_start = jiffies;
703
704 ieee80211_txb_free(txb);
705 return 0;
706}
707
708struct zd_rt_hdr {
709 struct ieee80211_radiotap_header rt_hdr;
710 u8 rt_flags;
Ulrich Kunitz99f65f22006-08-01 23:43:30 +0200711 u8 rt_rate;
Daniel Drakee85d0912006-06-02 17:11:32 +0100712 u16 rt_channel;
713 u16 rt_chbitmask;
John W. Linvillea88556a2006-11-28 14:16:37 -0500714} __attribute__((packed));
Daniel Drakee85d0912006-06-02 17:11:32 +0100715
716static void fill_rt_header(void *buffer, struct zd_mac *mac,
717 const struct ieee80211_rx_stats *stats,
718 const struct rx_status *status)
719{
720 struct zd_rt_hdr *hdr = buffer;
721
722 hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
723 hdr->rt_hdr.it_pad = 0;
724 hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
725 hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
726 (1 << IEEE80211_RADIOTAP_CHANNEL) |
727 (1 << IEEE80211_RADIOTAP_RATE));
728
729 hdr->rt_flags = 0;
730 if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
731 hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
732
Ulrich Kunitz99f65f22006-08-01 23:43:30 +0200733 hdr->rt_rate = stats->rate / 5;
734
Daniel Drakee85d0912006-06-02 17:11:32 +0100735 /* FIXME: 802.11a */
736 hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
737 _zd_chip_get_channel(&mac->chip)));
738 hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
739 ((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
740 ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
Daniel Drakee85d0912006-06-02 17:11:32 +0100741}
742
743/* Returns 1 if the data packet is for us and 0 otherwise. */
744static int is_data_packet_for_us(struct ieee80211_device *ieee,
745 struct ieee80211_hdr_4addr *hdr)
746{
747 struct net_device *netdev = ieee->dev;
748 u16 fc = le16_to_cpu(hdr->frame_ctl);
749
750 ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
751
752 switch (ieee->iw_mode) {
753 case IW_MODE_ADHOC:
754 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
755 memcmp(hdr->addr3, ieee->bssid, ETH_ALEN) != 0)
756 return 0;
757 break;
758 case IW_MODE_AUTO:
759 case IW_MODE_INFRA:
760 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
761 IEEE80211_FCTL_FROMDS ||
762 memcmp(hdr->addr2, ieee->bssid, ETH_ALEN) != 0)
763 return 0;
764 break;
765 default:
766 ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
767 return 0;
768 }
769
770 return memcmp(hdr->addr1, netdev->dev_addr, ETH_ALEN) == 0 ||
771 is_multicast_ether_addr(hdr->addr1) ||
772 (netdev->flags & IFF_PROMISC);
773}
774
Ulrich Kunitz741fec52006-11-22 00:05:53 +0000775/* Filters received packets. The function returns 1 if the packet should be
776 * forwarded to ieee80211_rx(). If the packet should be ignored the function
777 * returns 0. If an invalid packet is found the function returns -EINVAL.
778 *
779 * The function calls ieee80211_rx_mgt() directly.
Daniel Drakee85d0912006-06-02 17:11:32 +0100780 *
781 * It has been based on ieee80211_rx_any.
782 */
783static int filter_rx(struct ieee80211_device *ieee,
784 const u8 *buffer, unsigned int length,
785 struct ieee80211_rx_stats *stats)
786{
787 struct ieee80211_hdr_4addr *hdr;
788 u16 fc;
789
790 if (ieee->iw_mode == IW_MODE_MONITOR)
791 return 1;
792
793 hdr = (struct ieee80211_hdr_4addr *)buffer;
794 fc = le16_to_cpu(hdr->frame_ctl);
795 if ((fc & IEEE80211_FCTL_VERS) != 0)
796 return -EINVAL;
797
798 switch (WLAN_FC_GET_TYPE(fc)) {
799 case IEEE80211_FTYPE_MGMT:
800 if (length < sizeof(struct ieee80211_hdr_3addr))
801 return -EINVAL;
802 ieee80211_rx_mgt(ieee, hdr, stats);
803 return 0;
804 case IEEE80211_FTYPE_CTL:
Daniel Drakee85d0912006-06-02 17:11:32 +0100805 return 0;
806 case IEEE80211_FTYPE_DATA:
Ulrich Kunitz741fec52006-11-22 00:05:53 +0000807 /* Ignore invalid short buffers */
Daniel Drakee85d0912006-06-02 17:11:32 +0100808 if (length < sizeof(struct ieee80211_hdr_3addr))
809 return -EINVAL;
810 return is_data_packet_for_us(ieee, hdr);
811 }
812
813 return -EINVAL;
814}
815
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +0100816static void update_qual_rssi(struct zd_mac *mac,
817 const u8 *buffer, unsigned int length,
818 u8 qual_percent, u8 rssi_percent)
Daniel Drakee85d0912006-06-02 17:11:32 +0100819{
820 unsigned long flags;
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +0100821 struct ieee80211_hdr_3addr *hdr;
822 int i;
823
824 hdr = (struct ieee80211_hdr_3addr *)buffer;
825 if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
826 return;
827 if (memcmp(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid, ETH_ALEN) != 0)
828 return;
Daniel Drakee85d0912006-06-02 17:11:32 +0100829
830 spin_lock_irqsave(&mac->lock, flags);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +0100831 i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
832 mac->qual_buffer[i] = qual_percent;
833 mac->rssi_buffer[i] = rssi_percent;
834 mac->stats_count++;
Daniel Drakee85d0912006-06-02 17:11:32 +0100835 spin_unlock_irqrestore(&mac->lock, flags);
836}
837
838static int fill_rx_stats(struct ieee80211_rx_stats *stats,
839 const struct rx_status **pstatus,
840 struct zd_mac *mac,
841 const u8 *buffer, unsigned int length)
842{
843 const struct rx_status *status;
844
845 *pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
846 if (status->frame_status & ZD_RX_ERROR) {
847 /* FIXME: update? */
848 return -EINVAL;
849 }
850 memset(stats, 0, sizeof(struct ieee80211_rx_stats));
851 stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
852 + sizeof(struct rx_status));
853 /* FIXME: 802.11a */
854 stats->freq = IEEE80211_24GHZ_BAND;
855 stats->received_channel = _zd_chip_get_channel(&mac->chip);
856 stats->rssi = zd_rx_strength_percent(status->signal_strength);
857 stats->signal = zd_rx_qual_percent(buffer,
858 length - sizeof(struct rx_status),
859 status);
860 stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
861 stats->rate = zd_rx_rate(buffer, status);
862 if (stats->rate)
863 stats->mask |= IEEE80211_STATMASK_RATE;
864
Daniel Drakee85d0912006-06-02 17:11:32 +0100865 return 0;
866}
867
868int zd_mac_rx(struct zd_mac *mac, const u8 *buffer, unsigned int length)
869{
870 int r;
871 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
872 struct ieee80211_rx_stats stats;
873 const struct rx_status *status;
874 struct sk_buff *skb;
875
876 if (length < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
877 IEEE80211_FCS_LEN + sizeof(struct rx_status))
878 return -EINVAL;
879
880 r = fill_rx_stats(&stats, &status, mac, buffer, length);
881 if (r)
882 return r;
883
884 length -= ZD_PLCP_HEADER_SIZE+IEEE80211_FCS_LEN+
885 sizeof(struct rx_status);
886 buffer += ZD_PLCP_HEADER_SIZE;
887
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +0100888 update_qual_rssi(mac, buffer, length, stats.signal, stats.rssi);
889
Daniel Drakee85d0912006-06-02 17:11:32 +0100890 r = filter_rx(ieee, buffer, length, &stats);
891 if (r <= 0)
892 return r;
893
894 skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
895 if (!skb)
896 return -ENOMEM;
897 if (ieee->iw_mode == IW_MODE_MONITOR)
898 fill_rt_header(skb_put(skb, sizeof(struct zd_rt_hdr)), mac,
899 &stats, status);
900 memcpy(skb_put(skb, length), buffer, length);
901
902 r = ieee80211_rx(ieee, skb, &stats);
903 if (!r) {
904 ZD_ASSERT(in_irq());
905 dev_kfree_skb_irq(skb);
906 }
907 return 0;
908}
909
910static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
911 int pri)
912{
913 return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
914}
915
916static void set_security(struct net_device *netdev,
917 struct ieee80211_security *sec)
918{
919 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
920 struct ieee80211_security *secinfo = &ieee->sec;
921 int keyidx;
922
923 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
924
925 for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
926 if (sec->flags & (1<<keyidx)) {
927 secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
928 secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
929 memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
930 SCM_KEY_LEN);
931 }
932
933 if (sec->flags & SEC_ACTIVE_KEY) {
934 secinfo->active_key = sec->active_key;
935 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
936 " .active_key = %d\n", sec->active_key);
937 }
938 if (sec->flags & SEC_UNICAST_GROUP) {
939 secinfo->unicast_uses_group = sec->unicast_uses_group;
940 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
941 " .unicast_uses_group = %d\n",
942 sec->unicast_uses_group);
943 }
944 if (sec->flags & SEC_LEVEL) {
945 secinfo->level = sec->level;
946 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
947 " .level = %d\n", sec->level);
948 }
949 if (sec->flags & SEC_ENABLED) {
950 secinfo->enabled = sec->enabled;
951 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
952 " .enabled = %d\n", sec->enabled);
953 }
954 if (sec->flags & SEC_ENCRYPT) {
955 secinfo->encrypt = sec->encrypt;
956 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
957 " .encrypt = %d\n", sec->encrypt);
958 }
959 if (sec->flags & SEC_AUTH_MODE) {
960 secinfo->auth_mode = sec->auth_mode;
961 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
962 " .auth_mode = %d\n", sec->auth_mode);
963 }
964}
965
966static void ieee_init(struct ieee80211_device *ieee)
967{
968 ieee->mode = IEEE_B | IEEE_G;
969 ieee->freq_band = IEEE80211_24GHZ_BAND;
970 ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
971 ieee->tx_headroom = sizeof(struct zd_ctrlset);
972 ieee->set_security = set_security;
973 ieee->hard_start_xmit = netdev_tx;
974
975 /* Software encryption/decryption for now */
976 ieee->host_build_iv = 0;
977 ieee->host_encrypt = 1;
978 ieee->host_decrypt = 1;
979
980 /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
981 * correctly support AUTO */
982 ieee->iw_mode = IW_MODE_INFRA;
983}
984
985static void softmac_init(struct ieee80211softmac_device *sm)
986{
987 sm->set_channel = set_channel;
988}
989
990struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
991{
992 struct zd_mac *mac = zd_netdev_mac(ndev);
993 struct iw_statistics *iw_stats = &mac->iw_stats;
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +0100994 unsigned int i, count, qual_total, rssi_total;
Daniel Drakee85d0912006-06-02 17:11:32 +0100995
996 memset(iw_stats, 0, sizeof(struct iw_statistics));
997 /* We are not setting the status, because ieee->state is not updated
998 * at all and this driver doesn't track authentication state.
999 */
1000 spin_lock_irq(&mac->lock);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001001 count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
1002 mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
1003 qual_total = rssi_total = 0;
1004 for (i = 0; i < count; i++) {
1005 qual_total += mac->qual_buffer[i];
1006 rssi_total += mac->rssi_buffer[i];
1007 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001008 spin_unlock_irq(&mac->lock);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001009 iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
1010 if (count > 0) {
1011 iw_stats->qual.qual = qual_total / count;
1012 iw_stats->qual.level = rssi_total / count;
1013 iw_stats->qual.updated |=
1014 IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
1015 } else {
1016 iw_stats->qual.updated |=
1017 IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
1018 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001019 /* TODO: update counter */
1020 return iw_stats;
1021}
1022
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001023#define LINK_LED_WORK_DELAY HZ
1024
1025static void link_led_handler(void *p)
1026{
1027 struct zd_mac *mac = p;
1028 struct zd_chip *chip = &mac->chip;
1029 struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
1030 int is_associated;
1031 int r;
1032
1033 spin_lock_irq(&mac->lock);
John W. Linville41072a12006-10-17 13:47:40 -04001034 is_associated = sm->associnfo.associated != 0;
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001035 spin_unlock_irq(&mac->lock);
1036
1037 r = zd_chip_control_leds(chip,
1038 is_associated ? LED_ASSOCIATED : LED_SCANNING);
1039 if (r)
1040 dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1041
1042 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1043 LINK_LED_WORK_DELAY);
1044}
1045
1046static void housekeeping_init(struct zd_mac *mac)
1047{
1048 INIT_WORK(&mac->housekeeping.link_led_work, link_led_handler, mac);
1049}
1050
1051static void housekeeping_enable(struct zd_mac *mac)
1052{
1053 dev_dbg_f(zd_mac_dev(mac), "\n");
1054 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1055 0);
1056}
1057
1058static void housekeeping_disable(struct zd_mac *mac)
1059{
1060 dev_dbg_f(zd_mac_dev(mac), "\n");
1061 cancel_rearming_delayed_workqueue(zd_workqueue,
1062 &mac->housekeeping.link_led_work);
1063 zd_chip_control_leds(&mac->chip, LED_OFF);
1064}