blob: d7a86b188a9989315126baa17d844c3bc8c4a8d3 [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);
Daniel Drakeb1382ed2006-11-22 00:06:48 +000035static void set_rts_cts_work(void *d);
36static void set_basic_rates_work(void *d);
Daniel Drakee85d0912006-06-02 17:11:32 +010037
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +010038static void housekeeping_init(struct zd_mac *mac);
39static void housekeeping_enable(struct zd_mac *mac);
40static void housekeeping_disable(struct zd_mac *mac);
41
Daniel Drakee85d0912006-06-02 17:11:32 +010042int zd_mac_init(struct zd_mac *mac,
43 struct net_device *netdev,
44 struct usb_interface *intf)
45{
46 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
47
48 memset(mac, 0, sizeof(*mac));
49 spin_lock_init(&mac->lock);
50 mac->netdev = netdev;
Daniel Drakeb1382ed2006-11-22 00:06:48 +000051 INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work, mac);
52 INIT_WORK(&mac->set_basic_rates_work, set_basic_rates_work, mac);
Daniel Drakee85d0912006-06-02 17:11:32 +010053
54 ieee_init(ieee);
55 softmac_init(ieee80211_priv(netdev));
56 zd_chip_init(&mac->chip, netdev, intf);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +010057 housekeeping_init(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +010058 return 0;
59}
60
61static int reset_channel(struct zd_mac *mac)
62{
63 int r;
64 unsigned long flags;
65 const struct channel_range *range;
66
67 spin_lock_irqsave(&mac->lock, flags);
68 range = zd_channel_range(mac->regdomain);
69 if (!range->start) {
70 r = -EINVAL;
71 goto out;
72 }
73 mac->requested_channel = range->start;
74 r = 0;
75out:
76 spin_unlock_irqrestore(&mac->lock, flags);
77 return r;
78}
79
80int zd_mac_init_hw(struct zd_mac *mac, u8 device_type)
81{
82 int r;
83 struct zd_chip *chip = &mac->chip;
84 u8 addr[ETH_ALEN];
85 u8 default_regdomain;
86
87 r = zd_chip_enable_int(chip);
88 if (r)
89 goto out;
90 r = zd_chip_init_hw(chip, device_type);
91 if (r)
92 goto disable_int;
93
94 zd_get_e2p_mac_addr(chip, addr);
95 r = zd_write_mac_addr(chip, addr);
96 if (r)
97 goto disable_int;
98 ZD_ASSERT(!irqs_disabled());
99 spin_lock_irq(&mac->lock);
100 memcpy(mac->netdev->dev_addr, addr, ETH_ALEN);
101 spin_unlock_irq(&mac->lock);
102
103 r = zd_read_regdomain(chip, &default_regdomain);
104 if (r)
105 goto disable_int;
106 if (!zd_regdomain_supported(default_regdomain)) {
107 dev_dbg_f(zd_mac_dev(mac),
108 "Regulatory Domain %#04x is not supported.\n",
109 default_regdomain);
110 r = -EINVAL;
111 goto disable_int;
112 }
113 spin_lock_irq(&mac->lock);
114 mac->regdomain = mac->default_regdomain = default_regdomain;
115 spin_unlock_irq(&mac->lock);
116 r = reset_channel(mac);
117 if (r)
118 goto disable_int;
119
Daniel Drake40da08b2006-08-01 23:43:32 +0200120 /* We must inform the device that we are doing encryption/decryption in
121 * software at the moment. */
122 r = zd_set_encryption_type(chip, ENC_SNIFFER);
Daniel Drakee85d0912006-06-02 17:11:32 +0100123 if (r)
124 goto disable_int;
125
126 r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
127 if (r)
128 goto disable_int;
129
130 r = 0;
131disable_int:
132 zd_chip_disable_int(chip);
133out:
134 return r;
135}
136
137void zd_mac_clear(struct zd_mac *mac)
138{
Daniel Drakee85d0912006-06-02 17:11:32 +0100139 zd_chip_clear(&mac->chip);
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100140 ZD_ASSERT(!spin_is_locked(&mac->lock));
141 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
Daniel Drakee85d0912006-06-02 17:11:32 +0100142}
143
144static int reset_mode(struct zd_mac *mac)
145{
146 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
147 struct zd_ioreq32 ioreqs[3] = {
Ulrich Kunitzfde627b2006-08-01 23:43:35 +0200148 { CR_RX_FILTER, STA_RX_FILTER },
Daniel Drakee85d0912006-06-02 17:11:32 +0100149 { CR_SNIFFER_ON, 0U },
Daniel Drakee85d0912006-06-02 17:11:32 +0100150 };
151
152 if (ieee->iw_mode == IW_MODE_MONITOR) {
153 ioreqs[0].value = 0xffffffff;
154 ioreqs[1].value = 0x1;
155 ioreqs[2].value = ENC_SNIFFER;
156 }
157
158 return zd_iowrite32a(&mac->chip, ioreqs, 3);
159}
160
161int zd_mac_open(struct net_device *netdev)
162{
163 struct zd_mac *mac = zd_netdev_mac(netdev);
164 struct zd_chip *chip = &mac->chip;
165 int r;
166
167 r = zd_chip_enable_int(chip);
168 if (r < 0)
169 goto out;
170
171 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
172 if (r < 0)
173 goto disable_int;
174 r = reset_mode(mac);
175 if (r)
176 goto disable_int;
177 r = zd_chip_switch_radio_on(chip);
178 if (r < 0)
179 goto disable_int;
180 r = zd_chip_set_channel(chip, mac->requested_channel);
181 if (r < 0)
182 goto disable_radio;
183 r = zd_chip_enable_rx(chip);
184 if (r < 0)
185 goto disable_radio;
186 r = zd_chip_enable_hwint(chip);
187 if (r < 0)
188 goto disable_rx;
189
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100190 housekeeping_enable(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100191 ieee80211softmac_start(netdev);
192 return 0;
193disable_rx:
194 zd_chip_disable_rx(chip);
195disable_radio:
196 zd_chip_switch_radio_off(chip);
197disable_int:
198 zd_chip_disable_int(chip);
199out:
200 return r;
201}
202
203int zd_mac_stop(struct net_device *netdev)
204{
205 struct zd_mac *mac = zd_netdev_mac(netdev);
206 struct zd_chip *chip = &mac->chip;
207
Daniel Drakec9a4b352006-06-11 23:18:54 +0100208 netif_stop_queue(netdev);
209
Daniel Drakee85d0912006-06-02 17:11:32 +0100210 /*
211 * The order here deliberately is a little different from the open()
212 * method, since we need to make sure there is no opportunity for RX
213 * frames to be processed by softmac after we have stopped it.
214 */
215
216 zd_chip_disable_rx(chip);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100217 housekeeping_disable(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100218 ieee80211softmac_stop(netdev);
219
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000220 /* Ensure no work items are running or queued from this point */
221 cancel_delayed_work(&mac->set_rts_cts_work);
222 cancel_delayed_work(&mac->set_basic_rates_work);
223 flush_workqueue(zd_workqueue);
224 mac->updating_rts_rate = 0;
225 mac->updating_basic_rates = 0;
226
Daniel Drakee85d0912006-06-02 17:11:32 +0100227 zd_chip_disable_hwint(chip);
228 zd_chip_switch_radio_off(chip);
229 zd_chip_disable_int(chip);
230
231 return 0;
232}
233
234int zd_mac_set_mac_address(struct net_device *netdev, void *p)
235{
236 int r;
237 unsigned long flags;
238 struct sockaddr *addr = p;
239 struct zd_mac *mac = zd_netdev_mac(netdev);
240 struct zd_chip *chip = &mac->chip;
241
242 if (!is_valid_ether_addr(addr->sa_data))
243 return -EADDRNOTAVAIL;
244
245 dev_dbg_f(zd_mac_dev(mac),
246 "Setting MAC to " MAC_FMT "\n", MAC_ARG(addr->sa_data));
247
248 r = zd_write_mac_addr(chip, addr->sa_data);
249 if (r)
250 return r;
251
252 spin_lock_irqsave(&mac->lock, flags);
253 memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
254 spin_unlock_irqrestore(&mac->lock, flags);
255
256 return 0;
257}
258
259int zd_mac_set_regdomain(struct zd_mac *mac, u8 regdomain)
260{
261 int r;
262 u8 channel;
263
264 ZD_ASSERT(!irqs_disabled());
265 spin_lock_irq(&mac->lock);
266 if (regdomain == 0) {
267 regdomain = mac->default_regdomain;
268 }
269 if (!zd_regdomain_supported(regdomain)) {
270 spin_unlock_irq(&mac->lock);
271 return -EINVAL;
272 }
273 mac->regdomain = regdomain;
274 channel = mac->requested_channel;
275 spin_unlock_irq(&mac->lock);
276
277 r = zd_geo_init(zd_mac_to_ieee80211(mac), regdomain);
278 if (r)
279 return r;
280 if (!zd_regdomain_supports_channel(regdomain, channel)) {
281 r = reset_channel(mac);
282 if (r)
283 return r;
284 }
285
286 return 0;
287}
288
289u8 zd_mac_get_regdomain(struct zd_mac *mac)
290{
291 unsigned long flags;
292 u8 regdomain;
293
294 spin_lock_irqsave(&mac->lock, flags);
295 regdomain = mac->regdomain;
296 spin_unlock_irqrestore(&mac->lock, flags);
297 return regdomain;
298}
299
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000300/* Fallback to lowest rate, if rate is unknown. */
301static u8 rate_to_zd_rate(u8 rate)
302{
303 switch (rate) {
304 case IEEE80211_CCK_RATE_2MB:
305 return ZD_CCK_RATE_2M;
306 case IEEE80211_CCK_RATE_5MB:
307 return ZD_CCK_RATE_5_5M;
308 case IEEE80211_CCK_RATE_11MB:
309 return ZD_CCK_RATE_11M;
310 case IEEE80211_OFDM_RATE_6MB:
311 return ZD_OFDM_RATE_6M;
312 case IEEE80211_OFDM_RATE_9MB:
313 return ZD_OFDM_RATE_9M;
314 case IEEE80211_OFDM_RATE_12MB:
315 return ZD_OFDM_RATE_12M;
316 case IEEE80211_OFDM_RATE_18MB:
317 return ZD_OFDM_RATE_18M;
318 case IEEE80211_OFDM_RATE_24MB:
319 return ZD_OFDM_RATE_24M;
320 case IEEE80211_OFDM_RATE_36MB:
321 return ZD_OFDM_RATE_36M;
322 case IEEE80211_OFDM_RATE_48MB:
323 return ZD_OFDM_RATE_48M;
324 case IEEE80211_OFDM_RATE_54MB:
325 return ZD_OFDM_RATE_54M;
326 }
327 return ZD_CCK_RATE_1M;
328}
329
330static u16 rate_to_cr_rate(u8 rate)
331{
332 switch (rate) {
333 case IEEE80211_CCK_RATE_2MB:
334 return CR_RATE_1M;
335 case IEEE80211_CCK_RATE_5MB:
336 return CR_RATE_5_5M;
337 case IEEE80211_CCK_RATE_11MB:
338 return CR_RATE_11M;
339 case IEEE80211_OFDM_RATE_6MB:
340 return CR_RATE_6M;
341 case IEEE80211_OFDM_RATE_9MB:
342 return CR_RATE_9M;
343 case IEEE80211_OFDM_RATE_12MB:
344 return CR_RATE_12M;
345 case IEEE80211_OFDM_RATE_18MB:
346 return CR_RATE_18M;
347 case IEEE80211_OFDM_RATE_24MB:
348 return CR_RATE_24M;
349 case IEEE80211_OFDM_RATE_36MB:
350 return CR_RATE_36M;
351 case IEEE80211_OFDM_RATE_48MB:
352 return CR_RATE_48M;
353 case IEEE80211_OFDM_RATE_54MB:
354 return CR_RATE_54M;
355 }
356 return CR_RATE_1M;
357}
358
359static void try_enable_tx(struct zd_mac *mac)
360{
361 unsigned long flags;
362
363 spin_lock_irqsave(&mac->lock, flags);
364 if (mac->updating_rts_rate == 0 && mac->updating_basic_rates == 0)
365 netif_wake_queue(mac->netdev);
366 spin_unlock_irqrestore(&mac->lock, flags);
367}
368
369static void set_rts_cts_work(void *d)
370{
371 struct zd_mac *mac = d;
372 unsigned long flags;
373 u8 rts_rate;
374 unsigned int short_preamble;
375
376 mutex_lock(&mac->chip.mutex);
377
378 spin_lock_irqsave(&mac->lock, flags);
379 mac->updating_rts_rate = 0;
380 rts_rate = mac->rts_rate;
381 short_preamble = mac->short_preamble;
382 spin_unlock_irqrestore(&mac->lock, flags);
383
384 zd_chip_set_rts_cts_rate_locked(&mac->chip, rts_rate, short_preamble);
385 mutex_unlock(&mac->chip.mutex);
386
387 try_enable_tx(mac);
388}
389
390static void set_basic_rates_work(void *d)
391{
392 struct zd_mac *mac = d;
393 unsigned long flags;
394 u16 basic_rates;
395
396 mutex_lock(&mac->chip.mutex);
397
398 spin_lock_irqsave(&mac->lock, flags);
399 mac->updating_basic_rates = 0;
400 basic_rates = mac->basic_rates;
401 spin_unlock_irqrestore(&mac->lock, flags);
402
403 zd_chip_set_basic_rates_locked(&mac->chip, basic_rates);
404 mutex_unlock(&mac->chip.mutex);
405
406 try_enable_tx(mac);
407}
408
409static void bssinfo_change(struct net_device *netdev, u32 changes)
410{
411 struct zd_mac *mac = zd_netdev_mac(netdev);
412 struct ieee80211softmac_device *softmac = ieee80211_priv(netdev);
413 struct ieee80211softmac_bss_info *bssinfo = &softmac->bssinfo;
414 int need_set_rts_cts = 0;
415 int need_set_rates = 0;
416 u16 basic_rates;
417 unsigned long flags;
418
419 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
420
421 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_SHORT_PREAMBLE) {
422 spin_lock_irqsave(&mac->lock, flags);
423 mac->short_preamble = bssinfo->short_preamble;
424 spin_unlock_irqrestore(&mac->lock, flags);
425 need_set_rts_cts = 1;
426 }
427
428 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_RATES) {
429 /* Set RTS rate to highest available basic rate */
430 u8 rate = ieee80211softmac_highest_supported_rate(softmac,
431 &bssinfo->supported_rates, 1);
432 rate = rate_to_zd_rate(rate);
433
434 spin_lock_irqsave(&mac->lock, flags);
435 if (rate != mac->rts_rate) {
436 mac->rts_rate = rate;
437 need_set_rts_cts = 1;
438 }
439 spin_unlock_irqrestore(&mac->lock, flags);
440
441 /* Set basic rates */
442 need_set_rates = 1;
443 if (bssinfo->supported_rates.count == 0) {
444 /* Allow the device to be flexible */
445 basic_rates = CR_RATES_80211B | CR_RATES_80211G;
446 } else {
447 int i = 0;
448 basic_rates = 0;
449
450 for (i = 0; i < bssinfo->supported_rates.count; i++) {
451 u16 rate = bssinfo->supported_rates.rates[i];
452 if ((rate & IEEE80211_BASIC_RATE_MASK) == 0)
453 continue;
454
455 rate &= ~IEEE80211_BASIC_RATE_MASK;
456 basic_rates |= rate_to_cr_rate(rate);
457 }
458 }
459 spin_lock_irqsave(&mac->lock, flags);
460 mac->basic_rates = basic_rates;
461 spin_unlock_irqrestore(&mac->lock, flags);
462 }
463
464 /* Schedule any changes we made above */
465
466 spin_lock_irqsave(&mac->lock, flags);
467 if (need_set_rts_cts && !mac->updating_rts_rate) {
468 mac->updating_rts_rate = 1;
469 netif_stop_queue(mac->netdev);
470 queue_work(zd_workqueue, &mac->set_rts_cts_work);
471 }
472 if (need_set_rates && !mac->updating_basic_rates) {
473 mac->updating_basic_rates = 1;
474 netif_stop_queue(mac->netdev);
475 queue_work(zd_workqueue, &mac->set_basic_rates_work);
476 }
477 spin_unlock_irqrestore(&mac->lock, flags);
478}
479
Daniel Drakee85d0912006-06-02 17:11:32 +0100480static void set_channel(struct net_device *netdev, u8 channel)
481{
482 struct zd_mac *mac = zd_netdev_mac(netdev);
483
484 dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
485
486 zd_chip_set_channel(&mac->chip, channel);
487}
488
Daniel Drakee85d0912006-06-02 17:11:32 +0100489int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
490{
491 unsigned long lock_flags;
492 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
493
494 if (ieee->iw_mode == IW_MODE_INFRA)
495 return -EPERM;
496
497 spin_lock_irqsave(&mac->lock, lock_flags);
498 if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
499 spin_unlock_irqrestore(&mac->lock, lock_flags);
500 return -EINVAL;
501 }
502 mac->requested_channel = channel;
503 spin_unlock_irqrestore(&mac->lock, lock_flags);
504 if (netif_running(mac->netdev))
505 return zd_chip_set_channel(&mac->chip, channel);
506 else
507 return 0;
508}
509
Daniel Drake84bc7152006-11-22 00:05:30 +0000510u8 zd_mac_get_channel(struct zd_mac *mac)
Daniel Drakee85d0912006-06-02 17:11:32 +0100511{
Daniel Drake84bc7152006-11-22 00:05:30 +0000512 u8 channel = zd_chip_get_channel(&mac->chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100513
Daniel Drake84bc7152006-11-22 00:05:30 +0000514 dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
515 return channel;
Daniel Drakee85d0912006-06-02 17:11:32 +0100516}
517
518/* If wrong rate is given, we are falling back to the slowest rate: 1MBit/s */
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000519static u8 zd_rate_typed(u8 zd_rate)
Daniel Drakee85d0912006-06-02 17:11:32 +0100520{
521 static const u8 typed_rates[16] = {
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000522 [ZD_CCK_RATE_1M] = ZD_CS_CCK|ZD_CCK_RATE_1M,
523 [ZD_CCK_RATE_2M] = ZD_CS_CCK|ZD_CCK_RATE_2M,
524 [ZD_CCK_RATE_5_5M] = ZD_CS_CCK|ZD_CCK_RATE_5_5M,
525 [ZD_CCK_RATE_11M] = ZD_CS_CCK|ZD_CCK_RATE_11M,
Daniel Drakee85d0912006-06-02 17:11:32 +0100526 [ZD_OFDM_RATE_6M] = ZD_CS_OFDM|ZD_OFDM_RATE_6M,
527 [ZD_OFDM_RATE_9M] = ZD_CS_OFDM|ZD_OFDM_RATE_9M,
528 [ZD_OFDM_RATE_12M] = ZD_CS_OFDM|ZD_OFDM_RATE_12M,
529 [ZD_OFDM_RATE_18M] = ZD_CS_OFDM|ZD_OFDM_RATE_18M,
530 [ZD_OFDM_RATE_24M] = ZD_CS_OFDM|ZD_OFDM_RATE_24M,
531 [ZD_OFDM_RATE_36M] = ZD_CS_OFDM|ZD_OFDM_RATE_36M,
532 [ZD_OFDM_RATE_48M] = ZD_CS_OFDM|ZD_OFDM_RATE_48M,
533 [ZD_OFDM_RATE_54M] = ZD_CS_OFDM|ZD_OFDM_RATE_54M,
534 };
535
536 ZD_ASSERT(ZD_CS_RATE_MASK == 0x0f);
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000537 return typed_rates[zd_rate & ZD_CS_RATE_MASK];
Daniel Drakee85d0912006-06-02 17:11:32 +0100538}
539
Daniel Drakee85d0912006-06-02 17:11:32 +0100540int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
541{
542 struct ieee80211_device *ieee;
543
544 switch (mode) {
545 case IW_MODE_AUTO:
546 case IW_MODE_ADHOC:
547 case IW_MODE_INFRA:
548 mac->netdev->type = ARPHRD_ETHER;
549 break;
550 case IW_MODE_MONITOR:
551 mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
552 break;
553 default:
554 dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
555 return -EINVAL;
556 }
557
558 ieee = zd_mac_to_ieee80211(mac);
559 ZD_ASSERT(!irqs_disabled());
560 spin_lock_irq(&ieee->lock);
561 ieee->iw_mode = mode;
562 spin_unlock_irq(&ieee->lock);
563
564 if (netif_running(mac->netdev))
565 return reset_mode(mac);
566
567 return 0;
568}
569
570int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
571{
572 unsigned long flags;
573 struct ieee80211_device *ieee;
574
575 ieee = zd_mac_to_ieee80211(mac);
576 spin_lock_irqsave(&ieee->lock, flags);
577 *mode = ieee->iw_mode;
578 spin_unlock_irqrestore(&ieee->lock, flags);
579 return 0;
580}
581
582int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
583{
584 int i;
585 const struct channel_range *channel_range;
586 u8 regdomain;
587
588 memset(range, 0, sizeof(*range));
589
590 /* FIXME: Not so important and depends on the mode. For 802.11g
591 * usually this value is used. It seems to be that Bit/s number is
592 * given here.
593 */
594 range->throughput = 27 * 1000 * 1000;
595
596 range->max_qual.qual = 100;
597 range->max_qual.level = 100;
598
599 /* FIXME: Needs still to be tuned. */
600 range->avg_qual.qual = 71;
601 range->avg_qual.level = 80;
602
603 /* FIXME: depends on standard? */
604 range->min_rts = 256;
605 range->max_rts = 2346;
606
607 range->min_frag = MIN_FRAG_THRESHOLD;
608 range->max_frag = MAX_FRAG_THRESHOLD;
609
610 range->max_encoding_tokens = WEP_KEYS;
611 range->num_encoding_sizes = 2;
612 range->encoding_size[0] = 5;
613 range->encoding_size[1] = WEP_KEY_LEN;
614
615 range->we_version_compiled = WIRELESS_EXT;
616 range->we_version_source = 20;
617
618 ZD_ASSERT(!irqs_disabled());
619 spin_lock_irq(&mac->lock);
620 regdomain = mac->regdomain;
621 spin_unlock_irq(&mac->lock);
622 channel_range = zd_channel_range(regdomain);
623
624 range->num_channels = channel_range->end - channel_range->start;
625 range->old_num_channels = range->num_channels;
626 range->num_frequency = range->num_channels;
627 range->old_num_frequency = range->num_frequency;
628
629 for (i = 0; i < range->num_frequency; i++) {
630 struct iw_freq *freq = &range->freq[i];
631 freq->i = channel_range->start + i;
632 zd_channel_to_freq(freq, freq->i);
633 }
634
635 return 0;
636}
637
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000638static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
Daniel Drakee85d0912006-06-02 17:11:32 +0100639{
640 static const u8 rate_divisor[] = {
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000641 [ZD_CCK_RATE_1M] = 1,
642 [ZD_CCK_RATE_2M] = 2,
643 [ZD_CCK_RATE_5_5M] = 11, /* bits must be doubled */
644 [ZD_CCK_RATE_11M] = 11,
Daniel Drakee85d0912006-06-02 17:11:32 +0100645 [ZD_OFDM_RATE_6M] = 6,
646 [ZD_OFDM_RATE_9M] = 9,
647 [ZD_OFDM_RATE_12M] = 12,
648 [ZD_OFDM_RATE_18M] = 18,
649 [ZD_OFDM_RATE_24M] = 24,
650 [ZD_OFDM_RATE_36M] = 36,
651 [ZD_OFDM_RATE_48M] = 48,
652 [ZD_OFDM_RATE_54M] = 54,
653 };
654
655 u32 bits = (u32)tx_length * 8;
656 u32 divisor;
657
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000658 divisor = rate_divisor[zd_rate];
Daniel Drakee85d0912006-06-02 17:11:32 +0100659 if (divisor == 0)
660 return -EINVAL;
661
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000662 switch (zd_rate) {
663 case ZD_CCK_RATE_5_5M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100664 bits = (2*bits) + 10; /* round up to the next integer */
665 break;
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000666 case ZD_CCK_RATE_11M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100667 if (service) {
668 u32 t = bits % 11;
669 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
670 if (0 < t && t <= 3) {
671 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
672 }
673 }
674 bits += 10; /* round up to the next integer */
675 break;
676 }
677
678 return bits/divisor;
679}
680
681enum {
682 R2M_SHORT_PREAMBLE = 0x01,
683 R2M_11A = 0x02,
684};
685
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000686static u8 zd_rate_to_modulation(u8 zd_rate, int flags)
Daniel Drakee85d0912006-06-02 17:11:32 +0100687{
688 u8 modulation;
689
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000690 modulation = zd_rate_typed(zd_rate);
Daniel Drakee85d0912006-06-02 17:11:32 +0100691 if (flags & R2M_SHORT_PREAMBLE) {
692 switch (ZD_CS_RATE(modulation)) {
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000693 case ZD_CCK_RATE_2M:
694 case ZD_CCK_RATE_5_5M:
695 case ZD_CCK_RATE_11M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100696 modulation |= ZD_CS_CCK_PREA_SHORT;
697 return modulation;
698 }
699 }
700 if (flags & R2M_11A) {
701 if (ZD_CS_TYPE(modulation) == ZD_CS_OFDM)
702 modulation |= ZD_CS_OFDM_MODE_11A;
703 }
704 return modulation;
705}
706
707static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
708 struct ieee80211_hdr_4addr *hdr)
709{
710 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
711 u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000712 u8 rate, zd_rate;
Daniel Drakee85d0912006-06-02 17:11:32 +0100713 int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000714 int is_multicast = is_multicast_ether_addr(hdr->addr1);
715 int short_preamble = ieee80211softmac_short_preamble_ok(softmac,
716 is_multicast, is_mgt);
717 int flags = 0;
Daniel Drakee85d0912006-06-02 17:11:32 +0100718
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000719 /* FIXME: 802.11a? */
720 rate = ieee80211softmac_suggest_txrate(softmac, is_multicast, is_mgt);
721
722 if (short_preamble)
723 flags |= R2M_SHORT_PREAMBLE;
Daniel Drakee85d0912006-06-02 17:11:32 +0100724
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000725 zd_rate = rate_to_zd_rate(rate);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000726 cs->modulation = zd_rate_to_modulation(zd_rate, flags);
Daniel Drakee85d0912006-06-02 17:11:32 +0100727}
728
729static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
730 struct ieee80211_hdr_4addr *header)
731{
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000732 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
Daniel Drakee85d0912006-06-02 17:11:32 +0100733 unsigned int tx_length = le16_to_cpu(cs->tx_length);
734 u16 fctl = le16_to_cpu(header->frame_ctl);
735 u16 ftype = WLAN_FC_GET_TYPE(fctl);
736 u16 stype = WLAN_FC_GET_STYPE(fctl);
737
738 /*
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000739 * CONTROL TODO:
Daniel Drakee85d0912006-06-02 17:11:32 +0100740 * - if backoff needed, enable bit 0
741 * - if burst (backoff not needed) disable bit 0
Daniel Drakee85d0912006-06-02 17:11:32 +0100742 */
743
744 cs->control = 0;
745
746 /* First fragment */
747 if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
748 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
749
750 /* Multicast */
751 if (is_multicast_ether_addr(header->addr1))
752 cs->control |= ZD_CS_MULTICAST;
753
754 /* PS-POLL */
755 if (stype == IEEE80211_STYPE_PSPOLL)
756 cs->control |= ZD_CS_PS_POLL_FRAME;
757
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000758 /* Unicast data frames over the threshold should have RTS */
Daniel Drakee85d0912006-06-02 17:11:32 +0100759 if (!is_multicast_ether_addr(header->addr1) &&
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000760 ftype != IEEE80211_FTYPE_MGMT &&
761 tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
762 cs->control |= ZD_CS_RTS;
763
764 /* Use CTS-to-self protection if required */
765 if (ZD_CS_TYPE(cs->modulation) == ZD_CS_OFDM &&
766 ieee80211softmac_protection_needed(softmac)) {
767 /* FIXME: avoid sending RTS *and* self-CTS, is that correct? */
768 cs->control &= ~ZD_CS_RTS;
769 cs->control |= ZD_CS_SELF_CTS;
Daniel Drakee85d0912006-06-02 17:11:32 +0100770 }
771
772 /* FIXME: Management frame? */
773}
774
775static int fill_ctrlset(struct zd_mac *mac,
776 struct ieee80211_txb *txb,
777 int frag_num)
778{
779 int r;
780 struct sk_buff *skb = txb->fragments[frag_num];
781 struct ieee80211_hdr_4addr *hdr =
782 (struct ieee80211_hdr_4addr *) skb->data;
783 unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
784 unsigned int next_frag_len;
785 unsigned int packet_length;
786 struct zd_ctrlset *cs = (struct zd_ctrlset *)
787 skb_push(skb, sizeof(struct zd_ctrlset));
788
789 if (frag_num+1 < txb->nr_frags) {
790 next_frag_len = txb->fragments[frag_num+1]->len +
791 IEEE80211_FCS_LEN;
792 } else {
793 next_frag_len = 0;
794 }
795 ZD_ASSERT(frag_len <= 0xffff);
796 ZD_ASSERT(next_frag_len <= 0xffff);
797
798 cs_set_modulation(mac, cs, hdr);
799
800 cs->tx_length = cpu_to_le16(frag_len);
801
802 cs_set_control(mac, cs, hdr);
803
804 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
805 ZD_ASSERT(packet_length <= 0xffff);
806 /* ZD1211B: Computing the length difference this way, gives us
807 * flexibility to compute the packet length.
808 */
809 cs->packet_length = cpu_to_le16(mac->chip.is_zd1211b ?
810 packet_length - frag_len : packet_length);
811
812 /*
813 * CURRENT LENGTH:
814 * - transmit frame length in microseconds
815 * - seems to be derived from frame length
816 * - see Cal_Us_Service() in zdinlinef.h
817 * - if macp->bTxBurstEnable is enabled, then multiply by 4
818 * - bTxBurstEnable is never set in the vendor driver
819 *
820 * SERVICE:
821 * - "for PLCP configuration"
822 * - always 0 except in some situations at 802.11b 11M
823 * - see line 53 of zdinlinef.h
824 */
825 cs->service = 0;
826 r = zd_calc_tx_length_us(&cs->service, ZD_CS_RATE(cs->modulation),
827 le16_to_cpu(cs->tx_length));
828 if (r < 0)
829 return r;
830 cs->current_length = cpu_to_le16(r);
831
832 if (next_frag_len == 0) {
833 cs->next_frame_length = 0;
834 } else {
835 r = zd_calc_tx_length_us(NULL, ZD_CS_RATE(cs->modulation),
836 next_frag_len);
837 if (r < 0)
838 return r;
839 cs->next_frame_length = cpu_to_le16(r);
840 }
841
842 return 0;
843}
844
845static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
846{
847 int i, r;
848
849 for (i = 0; i < txb->nr_frags; i++) {
850 struct sk_buff *skb = txb->fragments[i];
851
852 r = fill_ctrlset(mac, txb, i);
853 if (r)
854 return r;
855 r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
856 if (r)
857 return r;
858 }
859
860 /* FIXME: shouldn't this be handled by the upper layers? */
861 mac->netdev->trans_start = jiffies;
862
863 ieee80211_txb_free(txb);
864 return 0;
865}
866
867struct zd_rt_hdr {
868 struct ieee80211_radiotap_header rt_hdr;
869 u8 rt_flags;
Ulrich Kunitz99f65f22006-08-01 23:43:30 +0200870 u8 rt_rate;
Daniel Drakee85d0912006-06-02 17:11:32 +0100871 u16 rt_channel;
872 u16 rt_chbitmask;
John W. Linvillea88556a2006-11-28 14:16:37 -0500873} __attribute__((packed));
Daniel Drakee85d0912006-06-02 17:11:32 +0100874
875static void fill_rt_header(void *buffer, struct zd_mac *mac,
876 const struct ieee80211_rx_stats *stats,
877 const struct rx_status *status)
878{
879 struct zd_rt_hdr *hdr = buffer;
880
881 hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
882 hdr->rt_hdr.it_pad = 0;
883 hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
884 hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
885 (1 << IEEE80211_RADIOTAP_CHANNEL) |
886 (1 << IEEE80211_RADIOTAP_RATE));
887
888 hdr->rt_flags = 0;
889 if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
890 hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
891
Ulrich Kunitz99f65f22006-08-01 23:43:30 +0200892 hdr->rt_rate = stats->rate / 5;
893
Daniel Drakee85d0912006-06-02 17:11:32 +0100894 /* FIXME: 802.11a */
895 hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
896 _zd_chip_get_channel(&mac->chip)));
897 hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
898 ((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
899 ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
Daniel Drakee85d0912006-06-02 17:11:32 +0100900}
901
902/* Returns 1 if the data packet is for us and 0 otherwise. */
903static int is_data_packet_for_us(struct ieee80211_device *ieee,
904 struct ieee80211_hdr_4addr *hdr)
905{
906 struct net_device *netdev = ieee->dev;
907 u16 fc = le16_to_cpu(hdr->frame_ctl);
908
909 ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
910
911 switch (ieee->iw_mode) {
912 case IW_MODE_ADHOC:
913 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
914 memcmp(hdr->addr3, ieee->bssid, ETH_ALEN) != 0)
915 return 0;
916 break;
917 case IW_MODE_AUTO:
918 case IW_MODE_INFRA:
919 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
920 IEEE80211_FCTL_FROMDS ||
921 memcmp(hdr->addr2, ieee->bssid, ETH_ALEN) != 0)
922 return 0;
923 break;
924 default:
925 ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
926 return 0;
927 }
928
929 return memcmp(hdr->addr1, netdev->dev_addr, ETH_ALEN) == 0 ||
930 is_multicast_ether_addr(hdr->addr1) ||
931 (netdev->flags & IFF_PROMISC);
932}
933
Ulrich Kunitz741fec52006-11-22 00:05:53 +0000934/* Filters received packets. The function returns 1 if the packet should be
935 * forwarded to ieee80211_rx(). If the packet should be ignored the function
936 * returns 0. If an invalid packet is found the function returns -EINVAL.
937 *
938 * The function calls ieee80211_rx_mgt() directly.
Daniel Drakee85d0912006-06-02 17:11:32 +0100939 *
940 * It has been based on ieee80211_rx_any.
941 */
942static int filter_rx(struct ieee80211_device *ieee,
943 const u8 *buffer, unsigned int length,
944 struct ieee80211_rx_stats *stats)
945{
946 struct ieee80211_hdr_4addr *hdr;
947 u16 fc;
948
949 if (ieee->iw_mode == IW_MODE_MONITOR)
950 return 1;
951
952 hdr = (struct ieee80211_hdr_4addr *)buffer;
953 fc = le16_to_cpu(hdr->frame_ctl);
954 if ((fc & IEEE80211_FCTL_VERS) != 0)
955 return -EINVAL;
956
957 switch (WLAN_FC_GET_TYPE(fc)) {
958 case IEEE80211_FTYPE_MGMT:
959 if (length < sizeof(struct ieee80211_hdr_3addr))
960 return -EINVAL;
961 ieee80211_rx_mgt(ieee, hdr, stats);
962 return 0;
963 case IEEE80211_FTYPE_CTL:
Daniel Drakee85d0912006-06-02 17:11:32 +0100964 return 0;
965 case IEEE80211_FTYPE_DATA:
Ulrich Kunitz741fec52006-11-22 00:05:53 +0000966 /* Ignore invalid short buffers */
Daniel Drakee85d0912006-06-02 17:11:32 +0100967 if (length < sizeof(struct ieee80211_hdr_3addr))
968 return -EINVAL;
969 return is_data_packet_for_us(ieee, hdr);
970 }
971
972 return -EINVAL;
973}
974
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +0100975static void update_qual_rssi(struct zd_mac *mac,
976 const u8 *buffer, unsigned int length,
977 u8 qual_percent, u8 rssi_percent)
Daniel Drakee85d0912006-06-02 17:11:32 +0100978{
979 unsigned long flags;
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +0100980 struct ieee80211_hdr_3addr *hdr;
981 int i;
982
983 hdr = (struct ieee80211_hdr_3addr *)buffer;
984 if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
985 return;
986 if (memcmp(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid, ETH_ALEN) != 0)
987 return;
Daniel Drakee85d0912006-06-02 17:11:32 +0100988
989 spin_lock_irqsave(&mac->lock, flags);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +0100990 i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
991 mac->qual_buffer[i] = qual_percent;
992 mac->rssi_buffer[i] = rssi_percent;
993 mac->stats_count++;
Daniel Drakee85d0912006-06-02 17:11:32 +0100994 spin_unlock_irqrestore(&mac->lock, flags);
995}
996
997static int fill_rx_stats(struct ieee80211_rx_stats *stats,
998 const struct rx_status **pstatus,
999 struct zd_mac *mac,
1000 const u8 *buffer, unsigned int length)
1001{
1002 const struct rx_status *status;
1003
1004 *pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
1005 if (status->frame_status & ZD_RX_ERROR) {
1006 /* FIXME: update? */
1007 return -EINVAL;
1008 }
1009 memset(stats, 0, sizeof(struct ieee80211_rx_stats));
1010 stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
1011 + sizeof(struct rx_status));
1012 /* FIXME: 802.11a */
1013 stats->freq = IEEE80211_24GHZ_BAND;
1014 stats->received_channel = _zd_chip_get_channel(&mac->chip);
1015 stats->rssi = zd_rx_strength_percent(status->signal_strength);
1016 stats->signal = zd_rx_qual_percent(buffer,
1017 length - sizeof(struct rx_status),
1018 status);
1019 stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
1020 stats->rate = zd_rx_rate(buffer, status);
1021 if (stats->rate)
1022 stats->mask |= IEEE80211_STATMASK_RATE;
1023
Daniel Drakee85d0912006-06-02 17:11:32 +01001024 return 0;
1025}
1026
1027int zd_mac_rx(struct zd_mac *mac, const u8 *buffer, unsigned int length)
1028{
1029 int r;
1030 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1031 struct ieee80211_rx_stats stats;
1032 const struct rx_status *status;
1033 struct sk_buff *skb;
1034
1035 if (length < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
1036 IEEE80211_FCS_LEN + sizeof(struct rx_status))
1037 return -EINVAL;
1038
1039 r = fill_rx_stats(&stats, &status, mac, buffer, length);
1040 if (r)
1041 return r;
1042
1043 length -= ZD_PLCP_HEADER_SIZE+IEEE80211_FCS_LEN+
1044 sizeof(struct rx_status);
1045 buffer += ZD_PLCP_HEADER_SIZE;
1046
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001047 update_qual_rssi(mac, buffer, length, stats.signal, stats.rssi);
1048
Daniel Drakee85d0912006-06-02 17:11:32 +01001049 r = filter_rx(ieee, buffer, length, &stats);
1050 if (r <= 0)
1051 return r;
1052
1053 skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
1054 if (!skb)
1055 return -ENOMEM;
1056 if (ieee->iw_mode == IW_MODE_MONITOR)
1057 fill_rt_header(skb_put(skb, sizeof(struct zd_rt_hdr)), mac,
1058 &stats, status);
1059 memcpy(skb_put(skb, length), buffer, length);
1060
1061 r = ieee80211_rx(ieee, skb, &stats);
Daniel Drake383956a2006-12-01 00:56:50 +00001062 if (!r)
1063 dev_kfree_skb_any(skb);
Daniel Drakee85d0912006-06-02 17:11:32 +01001064 return 0;
1065}
1066
1067static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
1068 int pri)
1069{
1070 return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
1071}
1072
1073static void set_security(struct net_device *netdev,
1074 struct ieee80211_security *sec)
1075{
1076 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
1077 struct ieee80211_security *secinfo = &ieee->sec;
1078 int keyidx;
1079
1080 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
1081
1082 for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
1083 if (sec->flags & (1<<keyidx)) {
1084 secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
1085 secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
1086 memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
1087 SCM_KEY_LEN);
1088 }
1089
1090 if (sec->flags & SEC_ACTIVE_KEY) {
1091 secinfo->active_key = sec->active_key;
1092 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1093 " .active_key = %d\n", sec->active_key);
1094 }
1095 if (sec->flags & SEC_UNICAST_GROUP) {
1096 secinfo->unicast_uses_group = sec->unicast_uses_group;
1097 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1098 " .unicast_uses_group = %d\n",
1099 sec->unicast_uses_group);
1100 }
1101 if (sec->flags & SEC_LEVEL) {
1102 secinfo->level = sec->level;
1103 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1104 " .level = %d\n", sec->level);
1105 }
1106 if (sec->flags & SEC_ENABLED) {
1107 secinfo->enabled = sec->enabled;
1108 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1109 " .enabled = %d\n", sec->enabled);
1110 }
1111 if (sec->flags & SEC_ENCRYPT) {
1112 secinfo->encrypt = sec->encrypt;
1113 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1114 " .encrypt = %d\n", sec->encrypt);
1115 }
1116 if (sec->flags & SEC_AUTH_MODE) {
1117 secinfo->auth_mode = sec->auth_mode;
1118 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1119 " .auth_mode = %d\n", sec->auth_mode);
1120 }
1121}
1122
1123static void ieee_init(struct ieee80211_device *ieee)
1124{
1125 ieee->mode = IEEE_B | IEEE_G;
1126 ieee->freq_band = IEEE80211_24GHZ_BAND;
1127 ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
1128 ieee->tx_headroom = sizeof(struct zd_ctrlset);
1129 ieee->set_security = set_security;
1130 ieee->hard_start_xmit = netdev_tx;
1131
1132 /* Software encryption/decryption for now */
1133 ieee->host_build_iv = 0;
1134 ieee->host_encrypt = 1;
1135 ieee->host_decrypt = 1;
1136
1137 /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
1138 * correctly support AUTO */
1139 ieee->iw_mode = IW_MODE_INFRA;
1140}
1141
1142static void softmac_init(struct ieee80211softmac_device *sm)
1143{
1144 sm->set_channel = set_channel;
Daniel Drakeb1382ed2006-11-22 00:06:48 +00001145 sm->bssinfo_change = bssinfo_change;
Daniel Drakee85d0912006-06-02 17:11:32 +01001146}
1147
1148struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
1149{
1150 struct zd_mac *mac = zd_netdev_mac(ndev);
1151 struct iw_statistics *iw_stats = &mac->iw_stats;
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001152 unsigned int i, count, qual_total, rssi_total;
Daniel Drakee85d0912006-06-02 17:11:32 +01001153
1154 memset(iw_stats, 0, sizeof(struct iw_statistics));
1155 /* We are not setting the status, because ieee->state is not updated
1156 * at all and this driver doesn't track authentication state.
1157 */
1158 spin_lock_irq(&mac->lock);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001159 count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
1160 mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
1161 qual_total = rssi_total = 0;
1162 for (i = 0; i < count; i++) {
1163 qual_total += mac->qual_buffer[i];
1164 rssi_total += mac->rssi_buffer[i];
1165 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001166 spin_unlock_irq(&mac->lock);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001167 iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
1168 if (count > 0) {
1169 iw_stats->qual.qual = qual_total / count;
1170 iw_stats->qual.level = rssi_total / count;
1171 iw_stats->qual.updated |=
1172 IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
1173 } else {
1174 iw_stats->qual.updated |=
1175 IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
1176 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001177 /* TODO: update counter */
1178 return iw_stats;
1179}
1180
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001181#define LINK_LED_WORK_DELAY HZ
1182
1183static void link_led_handler(void *p)
1184{
1185 struct zd_mac *mac = p;
1186 struct zd_chip *chip = &mac->chip;
1187 struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
1188 int is_associated;
1189 int r;
1190
1191 spin_lock_irq(&mac->lock);
John W. Linville41072a12006-10-17 13:47:40 -04001192 is_associated = sm->associnfo.associated != 0;
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001193 spin_unlock_irq(&mac->lock);
1194
1195 r = zd_chip_control_leds(chip,
1196 is_associated ? LED_ASSOCIATED : LED_SCANNING);
1197 if (r)
1198 dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1199
1200 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1201 LINK_LED_WORK_DELAY);
1202}
1203
1204static void housekeeping_init(struct zd_mac *mac)
1205{
1206 INIT_WORK(&mac->housekeeping.link_led_work, link_led_handler, mac);
1207}
1208
1209static void housekeeping_enable(struct zd_mac *mac)
1210{
1211 dev_dbg_f(zd_mac_dev(mac), "\n");
1212 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1213 0);
1214}
1215
1216static void housekeeping_disable(struct zd_mac *mac)
1217{
1218 dev_dbg_f(zd_mac_dev(mac), "\n");
1219 cancel_rearming_delayed_workqueue(zd_workqueue,
1220 &mac->housekeeping.link_led_work);
1221 zd_chip_control_leds(&mac->chip, LED_OFF);
1222}