blob: 1dd3f766ff49f32b8e3f407c47879b87f3d1edc1 [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 Kunitz583afd12006-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
Ulrich Kunitz9cdac962006-12-01 00:58:07 +000042static void set_multicast_hash_handler(void *mac_ptr);
43
Daniel Drakee85d0912006-06-02 17:11:32 +010044int zd_mac_init(struct zd_mac *mac,
45 struct net_device *netdev,
46 struct usb_interface *intf)
47{
48 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
49
50 memset(mac, 0, sizeof(*mac));
51 spin_lock_init(&mac->lock);
52 mac->netdev = netdev;
Daniel Drakeb1382ed2006-11-22 00:06:48 +000053 INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work, mac);
54 INIT_WORK(&mac->set_basic_rates_work, set_basic_rates_work, mac);
Daniel Drakee85d0912006-06-02 17:11:32 +010055
56 ieee_init(ieee);
57 softmac_init(ieee80211_priv(netdev));
58 zd_chip_init(&mac->chip, netdev, intf);
Ulrich Kunitz583afd12006-09-13 02:42:38 +010059 housekeeping_init(mac);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +000060 INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler,
61 mac);
Daniel Drakee85d0912006-06-02 17:11:32 +010062 return 0;
63}
64
65static int reset_channel(struct zd_mac *mac)
66{
67 int r;
68 unsigned long flags;
69 const struct channel_range *range;
70
71 spin_lock_irqsave(&mac->lock, flags);
72 range = zd_channel_range(mac->regdomain);
73 if (!range->start) {
74 r = -EINVAL;
75 goto out;
76 }
77 mac->requested_channel = range->start;
78 r = 0;
79out:
80 spin_unlock_irqrestore(&mac->lock, flags);
81 return r;
82}
83
84int zd_mac_init_hw(struct zd_mac *mac, u8 device_type)
85{
86 int r;
87 struct zd_chip *chip = &mac->chip;
88 u8 addr[ETH_ALEN];
89 u8 default_regdomain;
90
91 r = zd_chip_enable_int(chip);
92 if (r)
93 goto out;
94 r = zd_chip_init_hw(chip, device_type);
95 if (r)
96 goto disable_int;
97
98 zd_get_e2p_mac_addr(chip, addr);
99 r = zd_write_mac_addr(chip, addr);
100 if (r)
101 goto disable_int;
102 ZD_ASSERT(!irqs_disabled());
103 spin_lock_irq(&mac->lock);
104 memcpy(mac->netdev->dev_addr, addr, ETH_ALEN);
105 spin_unlock_irq(&mac->lock);
106
107 r = zd_read_regdomain(chip, &default_regdomain);
108 if (r)
109 goto disable_int;
110 if (!zd_regdomain_supported(default_regdomain)) {
111 dev_dbg_f(zd_mac_dev(mac),
112 "Regulatory Domain %#04x is not supported.\n",
113 default_regdomain);
114 r = -EINVAL;
115 goto disable_int;
116 }
117 spin_lock_irq(&mac->lock);
118 mac->regdomain = mac->default_regdomain = default_regdomain;
119 spin_unlock_irq(&mac->lock);
120 r = reset_channel(mac);
121 if (r)
122 goto disable_int;
123
Daniel Drake40da08b2006-08-01 23:43:32 +0200124 /* We must inform the device that we are doing encryption/decryption in
125 * software at the moment. */
126 r = zd_set_encryption_type(chip, ENC_SNIFFER);
Daniel Drakee85d0912006-06-02 17:11:32 +0100127 if (r)
128 goto disable_int;
129
130 r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
131 if (r)
132 goto disable_int;
133
134 r = 0;
135disable_int:
136 zd_chip_disable_int(chip);
137out:
138 return r;
139}
140
141void zd_mac_clear(struct zd_mac *mac)
142{
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000143 flush_workqueue(zd_workqueue);
Daniel Drakee85d0912006-06-02 17:11:32 +0100144 zd_chip_clear(&mac->chip);
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100145 ZD_ASSERT(!spin_is_locked(&mac->lock));
146 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
Daniel Drakee85d0912006-06-02 17:11:32 +0100147}
148
149static int reset_mode(struct zd_mac *mac)
150{
151 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
152 struct zd_ioreq32 ioreqs[3] = {
Ulrich Kunitzfde627b2006-08-01 23:43:35 +0200153 { CR_RX_FILTER, STA_RX_FILTER },
Daniel Drakee85d0912006-06-02 17:11:32 +0100154 { CR_SNIFFER_ON, 0U },
Daniel Drakee85d0912006-06-02 17:11:32 +0100155 };
156
157 if (ieee->iw_mode == IW_MODE_MONITOR) {
158 ioreqs[0].value = 0xffffffff;
159 ioreqs[1].value = 0x1;
160 ioreqs[2].value = ENC_SNIFFER;
161 }
162
163 return zd_iowrite32a(&mac->chip, ioreqs, 3);
164}
165
166int zd_mac_open(struct net_device *netdev)
167{
168 struct zd_mac *mac = zd_netdev_mac(netdev);
169 struct zd_chip *chip = &mac->chip;
170 int r;
171
172 r = zd_chip_enable_int(chip);
173 if (r < 0)
174 goto out;
175
176 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
177 if (r < 0)
178 goto disable_int;
179 r = reset_mode(mac);
180 if (r)
181 goto disable_int;
182 r = zd_chip_switch_radio_on(chip);
183 if (r < 0)
184 goto disable_int;
185 r = zd_chip_set_channel(chip, mac->requested_channel);
186 if (r < 0)
187 goto disable_radio;
188 r = zd_chip_enable_rx(chip);
189 if (r < 0)
190 goto disable_radio;
191 r = zd_chip_enable_hwint(chip);
192 if (r < 0)
193 goto disable_rx;
194
Ulrich Kunitz583afd12006-09-13 02:42:38 +0100195 housekeeping_enable(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100196 ieee80211softmac_start(netdev);
197 return 0;
198disable_rx:
199 zd_chip_disable_rx(chip);
200disable_radio:
201 zd_chip_switch_radio_off(chip);
202disable_int:
203 zd_chip_disable_int(chip);
204out:
205 return r;
206}
207
208int zd_mac_stop(struct net_device *netdev)
209{
210 struct zd_mac *mac = zd_netdev_mac(netdev);
211 struct zd_chip *chip = &mac->chip;
212
Daniel Drakec9a4b352006-06-11 23:18:54 +0100213 netif_stop_queue(netdev);
214
Daniel Drakee85d0912006-06-02 17:11:32 +0100215 /*
216 * The order here deliberately is a little different from the open()
217 * method, since we need to make sure there is no opportunity for RX
218 * frames to be processed by softmac after we have stopped it.
219 */
220
221 zd_chip_disable_rx(chip);
Ulrich Kunitz583afd12006-09-13 02:42:38 +0100222 housekeeping_disable(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100223 ieee80211softmac_stop(netdev);
224
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000225 /* Ensure no work items are running or queued from this point */
226 cancel_delayed_work(&mac->set_rts_cts_work);
227 cancel_delayed_work(&mac->set_basic_rates_work);
228 flush_workqueue(zd_workqueue);
229 mac->updating_rts_rate = 0;
230 mac->updating_basic_rates = 0;
231
Daniel Drakee85d0912006-06-02 17:11:32 +0100232 zd_chip_disable_hwint(chip);
233 zd_chip_switch_radio_off(chip);
234 zd_chip_disable_int(chip);
235
236 return 0;
237}
238
239int zd_mac_set_mac_address(struct net_device *netdev, void *p)
240{
241 int r;
242 unsigned long flags;
243 struct sockaddr *addr = p;
244 struct zd_mac *mac = zd_netdev_mac(netdev);
245 struct zd_chip *chip = &mac->chip;
246
247 if (!is_valid_ether_addr(addr->sa_data))
248 return -EADDRNOTAVAIL;
249
250 dev_dbg_f(zd_mac_dev(mac),
251 "Setting MAC to " MAC_FMT "\n", MAC_ARG(addr->sa_data));
252
253 r = zd_write_mac_addr(chip, addr->sa_data);
254 if (r)
255 return r;
256
257 spin_lock_irqsave(&mac->lock, flags);
258 memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
259 spin_unlock_irqrestore(&mac->lock, flags);
260
261 return 0;
262}
263
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000264static void set_multicast_hash_handler(void *mac_ptr)
265{
266 struct zd_mac *mac = mac_ptr;
267 struct zd_mc_hash hash;
268
269 spin_lock_irq(&mac->lock);
270 hash = mac->multicast_hash;
271 spin_unlock_irq(&mac->lock);
272
273 zd_chip_set_multicast_hash(&mac->chip, &hash);
274}
275
276void zd_mac_set_multicast_list(struct net_device *dev)
277{
278 struct zd_mc_hash hash;
279 struct zd_mac *mac = zd_netdev_mac(dev);
280 struct dev_mc_list *mc;
281 unsigned long flags;
282
283 if (dev->flags & (IFF_PROMISC|IFF_ALLMULTI)) {
284 zd_mc_add_all(&hash);
285 } else {
286 zd_mc_clear(&hash);
287 for (mc = dev->mc_list; mc; mc = mc->next) {
288 dev_dbg_f(zd_mac_dev(mac), "mc addr " MAC_FMT "\n",
289 MAC_ARG(mc->dmi_addr));
290 zd_mc_add_addr(&hash, mc->dmi_addr);
291 }
292 }
293
294 spin_lock_irqsave(&mac->lock, flags);
295 mac->multicast_hash = hash;
296 spin_unlock_irqrestore(&mac->lock, flags);
297 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
298}
299
Daniel Drakee85d0912006-06-02 17:11:32 +0100300int zd_mac_set_regdomain(struct zd_mac *mac, u8 regdomain)
301{
302 int r;
303 u8 channel;
304
305 ZD_ASSERT(!irqs_disabled());
306 spin_lock_irq(&mac->lock);
307 if (regdomain == 0) {
308 regdomain = mac->default_regdomain;
309 }
310 if (!zd_regdomain_supported(regdomain)) {
311 spin_unlock_irq(&mac->lock);
312 return -EINVAL;
313 }
314 mac->regdomain = regdomain;
315 channel = mac->requested_channel;
316 spin_unlock_irq(&mac->lock);
317
318 r = zd_geo_init(zd_mac_to_ieee80211(mac), regdomain);
319 if (r)
320 return r;
321 if (!zd_regdomain_supports_channel(regdomain, channel)) {
322 r = reset_channel(mac);
323 if (r)
324 return r;
325 }
326
327 return 0;
328}
329
330u8 zd_mac_get_regdomain(struct zd_mac *mac)
331{
332 unsigned long flags;
333 u8 regdomain;
334
335 spin_lock_irqsave(&mac->lock, flags);
336 regdomain = mac->regdomain;
337 spin_unlock_irqrestore(&mac->lock, flags);
338 return regdomain;
339}
340
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000341/* Fallback to lowest rate, if rate is unknown. */
342static u8 rate_to_zd_rate(u8 rate)
343{
344 switch (rate) {
345 case IEEE80211_CCK_RATE_2MB:
346 return ZD_CCK_RATE_2M;
347 case IEEE80211_CCK_RATE_5MB:
348 return ZD_CCK_RATE_5_5M;
349 case IEEE80211_CCK_RATE_11MB:
350 return ZD_CCK_RATE_11M;
351 case IEEE80211_OFDM_RATE_6MB:
352 return ZD_OFDM_RATE_6M;
353 case IEEE80211_OFDM_RATE_9MB:
354 return ZD_OFDM_RATE_9M;
355 case IEEE80211_OFDM_RATE_12MB:
356 return ZD_OFDM_RATE_12M;
357 case IEEE80211_OFDM_RATE_18MB:
358 return ZD_OFDM_RATE_18M;
359 case IEEE80211_OFDM_RATE_24MB:
360 return ZD_OFDM_RATE_24M;
361 case IEEE80211_OFDM_RATE_36MB:
362 return ZD_OFDM_RATE_36M;
363 case IEEE80211_OFDM_RATE_48MB:
364 return ZD_OFDM_RATE_48M;
365 case IEEE80211_OFDM_RATE_54MB:
366 return ZD_OFDM_RATE_54M;
367 }
368 return ZD_CCK_RATE_1M;
369}
370
371static u16 rate_to_cr_rate(u8 rate)
372{
373 switch (rate) {
374 case IEEE80211_CCK_RATE_2MB:
375 return CR_RATE_1M;
376 case IEEE80211_CCK_RATE_5MB:
377 return CR_RATE_5_5M;
378 case IEEE80211_CCK_RATE_11MB:
379 return CR_RATE_11M;
380 case IEEE80211_OFDM_RATE_6MB:
381 return CR_RATE_6M;
382 case IEEE80211_OFDM_RATE_9MB:
383 return CR_RATE_9M;
384 case IEEE80211_OFDM_RATE_12MB:
385 return CR_RATE_12M;
386 case IEEE80211_OFDM_RATE_18MB:
387 return CR_RATE_18M;
388 case IEEE80211_OFDM_RATE_24MB:
389 return CR_RATE_24M;
390 case IEEE80211_OFDM_RATE_36MB:
391 return CR_RATE_36M;
392 case IEEE80211_OFDM_RATE_48MB:
393 return CR_RATE_48M;
394 case IEEE80211_OFDM_RATE_54MB:
395 return CR_RATE_54M;
396 }
397 return CR_RATE_1M;
398}
399
400static void try_enable_tx(struct zd_mac *mac)
401{
402 unsigned long flags;
403
404 spin_lock_irqsave(&mac->lock, flags);
405 if (mac->updating_rts_rate == 0 && mac->updating_basic_rates == 0)
406 netif_wake_queue(mac->netdev);
407 spin_unlock_irqrestore(&mac->lock, flags);
408}
409
410static void set_rts_cts_work(void *d)
411{
412 struct zd_mac *mac = d;
413 unsigned long flags;
414 u8 rts_rate;
415 unsigned int short_preamble;
416
417 mutex_lock(&mac->chip.mutex);
418
419 spin_lock_irqsave(&mac->lock, flags);
420 mac->updating_rts_rate = 0;
421 rts_rate = mac->rts_rate;
422 short_preamble = mac->short_preamble;
423 spin_unlock_irqrestore(&mac->lock, flags);
424
425 zd_chip_set_rts_cts_rate_locked(&mac->chip, rts_rate, short_preamble);
426 mutex_unlock(&mac->chip.mutex);
427
428 try_enable_tx(mac);
429}
430
431static void set_basic_rates_work(void *d)
432{
433 struct zd_mac *mac = d;
434 unsigned long flags;
435 u16 basic_rates;
436
437 mutex_lock(&mac->chip.mutex);
438
439 spin_lock_irqsave(&mac->lock, flags);
440 mac->updating_basic_rates = 0;
441 basic_rates = mac->basic_rates;
442 spin_unlock_irqrestore(&mac->lock, flags);
443
444 zd_chip_set_basic_rates_locked(&mac->chip, basic_rates);
445 mutex_unlock(&mac->chip.mutex);
446
447 try_enable_tx(mac);
448}
449
450static void bssinfo_change(struct net_device *netdev, u32 changes)
451{
452 struct zd_mac *mac = zd_netdev_mac(netdev);
453 struct ieee80211softmac_device *softmac = ieee80211_priv(netdev);
454 struct ieee80211softmac_bss_info *bssinfo = &softmac->bssinfo;
455 int need_set_rts_cts = 0;
456 int need_set_rates = 0;
457 u16 basic_rates;
458 unsigned long flags;
459
460 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
461
462 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_SHORT_PREAMBLE) {
463 spin_lock_irqsave(&mac->lock, flags);
464 mac->short_preamble = bssinfo->short_preamble;
465 spin_unlock_irqrestore(&mac->lock, flags);
466 need_set_rts_cts = 1;
467 }
468
469 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_RATES) {
470 /* Set RTS rate to highest available basic rate */
471 u8 rate = ieee80211softmac_highest_supported_rate(softmac,
472 &bssinfo->supported_rates, 1);
473 rate = rate_to_zd_rate(rate);
474
475 spin_lock_irqsave(&mac->lock, flags);
476 if (rate != mac->rts_rate) {
477 mac->rts_rate = rate;
478 need_set_rts_cts = 1;
479 }
480 spin_unlock_irqrestore(&mac->lock, flags);
481
482 /* Set basic rates */
483 need_set_rates = 1;
484 if (bssinfo->supported_rates.count == 0) {
485 /* Allow the device to be flexible */
486 basic_rates = CR_RATES_80211B | CR_RATES_80211G;
487 } else {
488 int i = 0;
489 basic_rates = 0;
490
491 for (i = 0; i < bssinfo->supported_rates.count; i++) {
492 u16 rate = bssinfo->supported_rates.rates[i];
493 if ((rate & IEEE80211_BASIC_RATE_MASK) == 0)
494 continue;
495
496 rate &= ~IEEE80211_BASIC_RATE_MASK;
497 basic_rates |= rate_to_cr_rate(rate);
498 }
499 }
500 spin_lock_irqsave(&mac->lock, flags);
501 mac->basic_rates = basic_rates;
502 spin_unlock_irqrestore(&mac->lock, flags);
503 }
504
505 /* Schedule any changes we made above */
506
507 spin_lock_irqsave(&mac->lock, flags);
508 if (need_set_rts_cts && !mac->updating_rts_rate) {
509 mac->updating_rts_rate = 1;
510 netif_stop_queue(mac->netdev);
511 queue_work(zd_workqueue, &mac->set_rts_cts_work);
512 }
513 if (need_set_rates && !mac->updating_basic_rates) {
514 mac->updating_basic_rates = 1;
515 netif_stop_queue(mac->netdev);
516 queue_work(zd_workqueue, &mac->set_basic_rates_work);
517 }
518 spin_unlock_irqrestore(&mac->lock, flags);
519}
520
Daniel Drakee85d0912006-06-02 17:11:32 +0100521static void set_channel(struct net_device *netdev, u8 channel)
522{
523 struct zd_mac *mac = zd_netdev_mac(netdev);
524
525 dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
526
527 zd_chip_set_channel(&mac->chip, channel);
528}
529
Daniel Drakee85d0912006-06-02 17:11:32 +0100530int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
531{
532 unsigned long lock_flags;
533 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
534
535 if (ieee->iw_mode == IW_MODE_INFRA)
536 return -EPERM;
537
538 spin_lock_irqsave(&mac->lock, lock_flags);
539 if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
540 spin_unlock_irqrestore(&mac->lock, lock_flags);
541 return -EINVAL;
542 }
543 mac->requested_channel = channel;
544 spin_unlock_irqrestore(&mac->lock, lock_flags);
545 if (netif_running(mac->netdev))
546 return zd_chip_set_channel(&mac->chip, channel);
547 else
548 return 0;
549}
550
Daniel Drake84bc7152006-11-22 00:05:30 +0000551u8 zd_mac_get_channel(struct zd_mac *mac)
Daniel Drakee85d0912006-06-02 17:11:32 +0100552{
Daniel Drake84bc7152006-11-22 00:05:30 +0000553 u8 channel = zd_chip_get_channel(&mac->chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100554
Daniel Drake84bc7152006-11-22 00:05:30 +0000555 dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
556 return channel;
Daniel Drakee85d0912006-06-02 17:11:32 +0100557}
558
559/* If wrong rate is given, we are falling back to the slowest rate: 1MBit/s */
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000560static u8 zd_rate_typed(u8 zd_rate)
Daniel Drakee85d0912006-06-02 17:11:32 +0100561{
562 static const u8 typed_rates[16] = {
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000563 [ZD_CCK_RATE_1M] = ZD_CS_CCK|ZD_CCK_RATE_1M,
564 [ZD_CCK_RATE_2M] = ZD_CS_CCK|ZD_CCK_RATE_2M,
565 [ZD_CCK_RATE_5_5M] = ZD_CS_CCK|ZD_CCK_RATE_5_5M,
566 [ZD_CCK_RATE_11M] = ZD_CS_CCK|ZD_CCK_RATE_11M,
Daniel Drakee85d0912006-06-02 17:11:32 +0100567 [ZD_OFDM_RATE_6M] = ZD_CS_OFDM|ZD_OFDM_RATE_6M,
568 [ZD_OFDM_RATE_9M] = ZD_CS_OFDM|ZD_OFDM_RATE_9M,
569 [ZD_OFDM_RATE_12M] = ZD_CS_OFDM|ZD_OFDM_RATE_12M,
570 [ZD_OFDM_RATE_18M] = ZD_CS_OFDM|ZD_OFDM_RATE_18M,
571 [ZD_OFDM_RATE_24M] = ZD_CS_OFDM|ZD_OFDM_RATE_24M,
572 [ZD_OFDM_RATE_36M] = ZD_CS_OFDM|ZD_OFDM_RATE_36M,
573 [ZD_OFDM_RATE_48M] = ZD_CS_OFDM|ZD_OFDM_RATE_48M,
574 [ZD_OFDM_RATE_54M] = ZD_CS_OFDM|ZD_OFDM_RATE_54M,
575 };
576
577 ZD_ASSERT(ZD_CS_RATE_MASK == 0x0f);
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000578 return typed_rates[zd_rate & ZD_CS_RATE_MASK];
Daniel Drakee85d0912006-06-02 17:11:32 +0100579}
580
Daniel Drakee85d0912006-06-02 17:11:32 +0100581int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
582{
583 struct ieee80211_device *ieee;
584
585 switch (mode) {
586 case IW_MODE_AUTO:
587 case IW_MODE_ADHOC:
588 case IW_MODE_INFRA:
589 mac->netdev->type = ARPHRD_ETHER;
590 break;
591 case IW_MODE_MONITOR:
592 mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
593 break;
594 default:
595 dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
596 return -EINVAL;
597 }
598
599 ieee = zd_mac_to_ieee80211(mac);
600 ZD_ASSERT(!irqs_disabled());
601 spin_lock_irq(&ieee->lock);
602 ieee->iw_mode = mode;
603 spin_unlock_irq(&ieee->lock);
604
605 if (netif_running(mac->netdev))
606 return reset_mode(mac);
607
608 return 0;
609}
610
611int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
612{
613 unsigned long flags;
614 struct ieee80211_device *ieee;
615
616 ieee = zd_mac_to_ieee80211(mac);
617 spin_lock_irqsave(&ieee->lock, flags);
618 *mode = ieee->iw_mode;
619 spin_unlock_irqrestore(&ieee->lock, flags);
620 return 0;
621}
622
623int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
624{
625 int i;
626 const struct channel_range *channel_range;
627 u8 regdomain;
628
629 memset(range, 0, sizeof(*range));
630
631 /* FIXME: Not so important and depends on the mode. For 802.11g
632 * usually this value is used. It seems to be that Bit/s number is
633 * given here.
634 */
635 range->throughput = 27 * 1000 * 1000;
636
637 range->max_qual.qual = 100;
638 range->max_qual.level = 100;
639
640 /* FIXME: Needs still to be tuned. */
641 range->avg_qual.qual = 71;
642 range->avg_qual.level = 80;
643
644 /* FIXME: depends on standard? */
645 range->min_rts = 256;
646 range->max_rts = 2346;
647
648 range->min_frag = MIN_FRAG_THRESHOLD;
649 range->max_frag = MAX_FRAG_THRESHOLD;
650
651 range->max_encoding_tokens = WEP_KEYS;
652 range->num_encoding_sizes = 2;
653 range->encoding_size[0] = 5;
654 range->encoding_size[1] = WEP_KEY_LEN;
655
656 range->we_version_compiled = WIRELESS_EXT;
657 range->we_version_source = 20;
658
Daniel Drakeff9b99b2006-12-01 00:57:11 +0000659 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
660 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
661
Daniel Drakee85d0912006-06-02 17:11:32 +0100662 ZD_ASSERT(!irqs_disabled());
663 spin_lock_irq(&mac->lock);
664 regdomain = mac->regdomain;
665 spin_unlock_irq(&mac->lock);
666 channel_range = zd_channel_range(regdomain);
667
668 range->num_channels = channel_range->end - channel_range->start;
669 range->old_num_channels = range->num_channels;
670 range->num_frequency = range->num_channels;
671 range->old_num_frequency = range->num_frequency;
672
673 for (i = 0; i < range->num_frequency; i++) {
674 struct iw_freq *freq = &range->freq[i];
675 freq->i = channel_range->start + i;
676 zd_channel_to_freq(freq, freq->i);
677 }
678
679 return 0;
680}
681
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000682static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
Daniel Drakee85d0912006-06-02 17:11:32 +0100683{
684 static const u8 rate_divisor[] = {
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000685 [ZD_CCK_RATE_1M] = 1,
686 [ZD_CCK_RATE_2M] = 2,
687 [ZD_CCK_RATE_5_5M] = 11, /* bits must be doubled */
688 [ZD_CCK_RATE_11M] = 11,
Daniel Drakee85d0912006-06-02 17:11:32 +0100689 [ZD_OFDM_RATE_6M] = 6,
690 [ZD_OFDM_RATE_9M] = 9,
691 [ZD_OFDM_RATE_12M] = 12,
692 [ZD_OFDM_RATE_18M] = 18,
693 [ZD_OFDM_RATE_24M] = 24,
694 [ZD_OFDM_RATE_36M] = 36,
695 [ZD_OFDM_RATE_48M] = 48,
696 [ZD_OFDM_RATE_54M] = 54,
697 };
698
699 u32 bits = (u32)tx_length * 8;
700 u32 divisor;
701
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000702 divisor = rate_divisor[zd_rate];
Daniel Drakee85d0912006-06-02 17:11:32 +0100703 if (divisor == 0)
704 return -EINVAL;
705
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000706 switch (zd_rate) {
707 case ZD_CCK_RATE_5_5M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100708 bits = (2*bits) + 10; /* round up to the next integer */
709 break;
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000710 case ZD_CCK_RATE_11M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100711 if (service) {
712 u32 t = bits % 11;
713 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
714 if (0 < t && t <= 3) {
715 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
716 }
717 }
718 bits += 10; /* round up to the next integer */
719 break;
720 }
721
722 return bits/divisor;
723}
724
725enum {
726 R2M_SHORT_PREAMBLE = 0x01,
727 R2M_11A = 0x02,
728};
729
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000730static u8 zd_rate_to_modulation(u8 zd_rate, int flags)
Daniel Drakee85d0912006-06-02 17:11:32 +0100731{
732 u8 modulation;
733
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000734 modulation = zd_rate_typed(zd_rate);
Daniel Drakee85d0912006-06-02 17:11:32 +0100735 if (flags & R2M_SHORT_PREAMBLE) {
736 switch (ZD_CS_RATE(modulation)) {
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000737 case ZD_CCK_RATE_2M:
738 case ZD_CCK_RATE_5_5M:
739 case ZD_CCK_RATE_11M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100740 modulation |= ZD_CS_CCK_PREA_SHORT;
741 return modulation;
742 }
743 }
744 if (flags & R2M_11A) {
745 if (ZD_CS_TYPE(modulation) == ZD_CS_OFDM)
746 modulation |= ZD_CS_OFDM_MODE_11A;
747 }
748 return modulation;
749}
750
751static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
752 struct ieee80211_hdr_4addr *hdr)
753{
754 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
755 u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000756 u8 rate, zd_rate;
Daniel Drakee85d0912006-06-02 17:11:32 +0100757 int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000758 int is_multicast = is_multicast_ether_addr(hdr->addr1);
759 int short_preamble = ieee80211softmac_short_preamble_ok(softmac,
760 is_multicast, is_mgt);
761 int flags = 0;
Daniel Drakee85d0912006-06-02 17:11:32 +0100762
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000763 /* FIXME: 802.11a? */
764 rate = ieee80211softmac_suggest_txrate(softmac, is_multicast, is_mgt);
765
766 if (short_preamble)
767 flags |= R2M_SHORT_PREAMBLE;
Daniel Drakee85d0912006-06-02 17:11:32 +0100768
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000769 zd_rate = rate_to_zd_rate(rate);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000770 cs->modulation = zd_rate_to_modulation(zd_rate, flags);
Daniel Drakee85d0912006-06-02 17:11:32 +0100771}
772
773static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
774 struct ieee80211_hdr_4addr *header)
775{
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000776 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
Daniel Drakee85d0912006-06-02 17:11:32 +0100777 unsigned int tx_length = le16_to_cpu(cs->tx_length);
778 u16 fctl = le16_to_cpu(header->frame_ctl);
779 u16 ftype = WLAN_FC_GET_TYPE(fctl);
780 u16 stype = WLAN_FC_GET_STYPE(fctl);
781
782 /*
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000783 * CONTROL TODO:
Daniel Drakee85d0912006-06-02 17:11:32 +0100784 * - if backoff needed, enable bit 0
785 * - if burst (backoff not needed) disable bit 0
Daniel Drakee85d0912006-06-02 17:11:32 +0100786 */
787
788 cs->control = 0;
789
790 /* First fragment */
791 if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
792 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
793
794 /* Multicast */
795 if (is_multicast_ether_addr(header->addr1))
796 cs->control |= ZD_CS_MULTICAST;
797
798 /* PS-POLL */
799 if (stype == IEEE80211_STYPE_PSPOLL)
800 cs->control |= ZD_CS_PS_POLL_FRAME;
801
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000802 /* Unicast data frames over the threshold should have RTS */
Daniel Drakee85d0912006-06-02 17:11:32 +0100803 if (!is_multicast_ether_addr(header->addr1) &&
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000804 ftype != IEEE80211_FTYPE_MGMT &&
805 tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
806 cs->control |= ZD_CS_RTS;
807
808 /* Use CTS-to-self protection if required */
809 if (ZD_CS_TYPE(cs->modulation) == ZD_CS_OFDM &&
810 ieee80211softmac_protection_needed(softmac)) {
811 /* FIXME: avoid sending RTS *and* self-CTS, is that correct? */
812 cs->control &= ~ZD_CS_RTS;
813 cs->control |= ZD_CS_SELF_CTS;
Daniel Drakee85d0912006-06-02 17:11:32 +0100814 }
815
816 /* FIXME: Management frame? */
817}
818
819static int fill_ctrlset(struct zd_mac *mac,
820 struct ieee80211_txb *txb,
821 int frag_num)
822{
823 int r;
824 struct sk_buff *skb = txb->fragments[frag_num];
825 struct ieee80211_hdr_4addr *hdr =
826 (struct ieee80211_hdr_4addr *) skb->data;
827 unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
828 unsigned int next_frag_len;
829 unsigned int packet_length;
830 struct zd_ctrlset *cs = (struct zd_ctrlset *)
831 skb_push(skb, sizeof(struct zd_ctrlset));
832
833 if (frag_num+1 < txb->nr_frags) {
834 next_frag_len = txb->fragments[frag_num+1]->len +
835 IEEE80211_FCS_LEN;
836 } else {
837 next_frag_len = 0;
838 }
839 ZD_ASSERT(frag_len <= 0xffff);
840 ZD_ASSERT(next_frag_len <= 0xffff);
841
842 cs_set_modulation(mac, cs, hdr);
843
844 cs->tx_length = cpu_to_le16(frag_len);
845
846 cs_set_control(mac, cs, hdr);
847
848 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
849 ZD_ASSERT(packet_length <= 0xffff);
850 /* ZD1211B: Computing the length difference this way, gives us
851 * flexibility to compute the packet length.
852 */
853 cs->packet_length = cpu_to_le16(mac->chip.is_zd1211b ?
854 packet_length - frag_len : packet_length);
855
856 /*
857 * CURRENT LENGTH:
858 * - transmit frame length in microseconds
859 * - seems to be derived from frame length
860 * - see Cal_Us_Service() in zdinlinef.h
861 * - if macp->bTxBurstEnable is enabled, then multiply by 4
862 * - bTxBurstEnable is never set in the vendor driver
863 *
864 * SERVICE:
865 * - "for PLCP configuration"
866 * - always 0 except in some situations at 802.11b 11M
867 * - see line 53 of zdinlinef.h
868 */
869 cs->service = 0;
870 r = zd_calc_tx_length_us(&cs->service, ZD_CS_RATE(cs->modulation),
871 le16_to_cpu(cs->tx_length));
872 if (r < 0)
873 return r;
874 cs->current_length = cpu_to_le16(r);
875
876 if (next_frag_len == 0) {
877 cs->next_frame_length = 0;
878 } else {
879 r = zd_calc_tx_length_us(NULL, ZD_CS_RATE(cs->modulation),
880 next_frag_len);
881 if (r < 0)
882 return r;
883 cs->next_frame_length = cpu_to_le16(r);
884 }
885
886 return 0;
887}
888
889static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
890{
891 int i, r;
892
893 for (i = 0; i < txb->nr_frags; i++) {
894 struct sk_buff *skb = txb->fragments[i];
895
896 r = fill_ctrlset(mac, txb, i);
897 if (r)
898 return r;
899 r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
900 if (r)
901 return r;
902 }
903
904 /* FIXME: shouldn't this be handled by the upper layers? */
905 mac->netdev->trans_start = jiffies;
906
907 ieee80211_txb_free(txb);
908 return 0;
909}
910
911struct zd_rt_hdr {
912 struct ieee80211_radiotap_header rt_hdr;
913 u8 rt_flags;
Ulrich Kunitz99f65f22006-08-01 23:43:30 +0200914 u8 rt_rate;
Daniel Drakee85d0912006-06-02 17:11:32 +0100915 u16 rt_channel;
916 u16 rt_chbitmask;
John W. Linvillea88556a2006-11-28 14:16:37 -0500917} __attribute__((packed));
Daniel Drakee85d0912006-06-02 17:11:32 +0100918
919static void fill_rt_header(void *buffer, struct zd_mac *mac,
920 const struct ieee80211_rx_stats *stats,
921 const struct rx_status *status)
922{
923 struct zd_rt_hdr *hdr = buffer;
924
925 hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
926 hdr->rt_hdr.it_pad = 0;
927 hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
928 hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
929 (1 << IEEE80211_RADIOTAP_CHANNEL) |
930 (1 << IEEE80211_RADIOTAP_RATE));
931
932 hdr->rt_flags = 0;
933 if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
934 hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
935
Ulrich Kunitz99f65f22006-08-01 23:43:30 +0200936 hdr->rt_rate = stats->rate / 5;
937
Daniel Drakee85d0912006-06-02 17:11:32 +0100938 /* FIXME: 802.11a */
939 hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
940 _zd_chip_get_channel(&mac->chip)));
941 hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
942 ((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
943 ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
Daniel Drakee85d0912006-06-02 17:11:32 +0100944}
945
946/* Returns 1 if the data packet is for us and 0 otherwise. */
947static int is_data_packet_for_us(struct ieee80211_device *ieee,
948 struct ieee80211_hdr_4addr *hdr)
949{
950 struct net_device *netdev = ieee->dev;
951 u16 fc = le16_to_cpu(hdr->frame_ctl);
952
953 ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
954
955 switch (ieee->iw_mode) {
956 case IW_MODE_ADHOC:
957 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
958 memcmp(hdr->addr3, ieee->bssid, ETH_ALEN) != 0)
959 return 0;
960 break;
961 case IW_MODE_AUTO:
962 case IW_MODE_INFRA:
963 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
964 IEEE80211_FCTL_FROMDS ||
965 memcmp(hdr->addr2, ieee->bssid, ETH_ALEN) != 0)
966 return 0;
967 break;
968 default:
969 ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
970 return 0;
971 }
972
973 return memcmp(hdr->addr1, netdev->dev_addr, ETH_ALEN) == 0 ||
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000974 (is_multicast_ether_addr(hdr->addr1) &&
975 memcmp(hdr->addr3, netdev->dev_addr, ETH_ALEN) != 0) ||
Daniel Drakee85d0912006-06-02 17:11:32 +0100976 (netdev->flags & IFF_PROMISC);
977}
978
Ulrich Kunitz741fec52006-11-22 00:05:53 +0000979/* Filters received packets. The function returns 1 if the packet should be
980 * forwarded to ieee80211_rx(). If the packet should be ignored the function
981 * returns 0. If an invalid packet is found the function returns -EINVAL.
982 *
983 * The function calls ieee80211_rx_mgt() directly.
Daniel Drakee85d0912006-06-02 17:11:32 +0100984 *
985 * It has been based on ieee80211_rx_any.
986 */
987static int filter_rx(struct ieee80211_device *ieee,
988 const u8 *buffer, unsigned int length,
989 struct ieee80211_rx_stats *stats)
990{
991 struct ieee80211_hdr_4addr *hdr;
992 u16 fc;
993
994 if (ieee->iw_mode == IW_MODE_MONITOR)
995 return 1;
996
997 hdr = (struct ieee80211_hdr_4addr *)buffer;
998 fc = le16_to_cpu(hdr->frame_ctl);
999 if ((fc & IEEE80211_FCTL_VERS) != 0)
1000 return -EINVAL;
1001
1002 switch (WLAN_FC_GET_TYPE(fc)) {
1003 case IEEE80211_FTYPE_MGMT:
1004 if (length < sizeof(struct ieee80211_hdr_3addr))
1005 return -EINVAL;
1006 ieee80211_rx_mgt(ieee, hdr, stats);
1007 return 0;
1008 case IEEE80211_FTYPE_CTL:
Daniel Drakee85d0912006-06-02 17:11:32 +01001009 return 0;
1010 case IEEE80211_FTYPE_DATA:
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001011 /* Ignore invalid short buffers */
Daniel Drakee85d0912006-06-02 17:11:32 +01001012 if (length < sizeof(struct ieee80211_hdr_3addr))
1013 return -EINVAL;
1014 return is_data_packet_for_us(ieee, hdr);
1015 }
1016
1017 return -EINVAL;
1018}
1019
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001020static void update_qual_rssi(struct zd_mac *mac,
1021 const u8 *buffer, unsigned int length,
1022 u8 qual_percent, u8 rssi_percent)
Daniel Drakee85d0912006-06-02 17:11:32 +01001023{
1024 unsigned long flags;
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001025 struct ieee80211_hdr_3addr *hdr;
1026 int i;
1027
1028 hdr = (struct ieee80211_hdr_3addr *)buffer;
1029 if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
1030 return;
1031 if (memcmp(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid, ETH_ALEN) != 0)
1032 return;
Daniel Drakee85d0912006-06-02 17:11:32 +01001033
1034 spin_lock_irqsave(&mac->lock, flags);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001035 i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
1036 mac->qual_buffer[i] = qual_percent;
1037 mac->rssi_buffer[i] = rssi_percent;
1038 mac->stats_count++;
Daniel Drakee85d0912006-06-02 17:11:32 +01001039 spin_unlock_irqrestore(&mac->lock, flags);
1040}
1041
1042static int fill_rx_stats(struct ieee80211_rx_stats *stats,
1043 const struct rx_status **pstatus,
1044 struct zd_mac *mac,
1045 const u8 *buffer, unsigned int length)
1046{
1047 const struct rx_status *status;
1048
1049 *pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
1050 if (status->frame_status & ZD_RX_ERROR) {
1051 /* FIXME: update? */
1052 return -EINVAL;
1053 }
1054 memset(stats, 0, sizeof(struct ieee80211_rx_stats));
1055 stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
1056 + sizeof(struct rx_status));
1057 /* FIXME: 802.11a */
1058 stats->freq = IEEE80211_24GHZ_BAND;
1059 stats->received_channel = _zd_chip_get_channel(&mac->chip);
1060 stats->rssi = zd_rx_strength_percent(status->signal_strength);
1061 stats->signal = zd_rx_qual_percent(buffer,
1062 length - sizeof(struct rx_status),
1063 status);
1064 stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
1065 stats->rate = zd_rx_rate(buffer, status);
1066 if (stats->rate)
1067 stats->mask |= IEEE80211_STATMASK_RATE;
1068
Daniel Drakee85d0912006-06-02 17:11:32 +01001069 return 0;
1070}
1071
1072int zd_mac_rx(struct zd_mac *mac, const u8 *buffer, unsigned int length)
1073{
1074 int r;
1075 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1076 struct ieee80211_rx_stats stats;
1077 const struct rx_status *status;
1078 struct sk_buff *skb;
1079
1080 if (length < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
1081 IEEE80211_FCS_LEN + sizeof(struct rx_status))
1082 return -EINVAL;
1083
1084 r = fill_rx_stats(&stats, &status, mac, buffer, length);
1085 if (r)
1086 return r;
1087
1088 length -= ZD_PLCP_HEADER_SIZE+IEEE80211_FCS_LEN+
1089 sizeof(struct rx_status);
1090 buffer += ZD_PLCP_HEADER_SIZE;
1091
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001092 update_qual_rssi(mac, buffer, length, stats.signal, stats.rssi);
1093
Daniel Drakee85d0912006-06-02 17:11:32 +01001094 r = filter_rx(ieee, buffer, length, &stats);
1095 if (r <= 0)
1096 return r;
1097
1098 skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
1099 if (!skb)
1100 return -ENOMEM;
1101 if (ieee->iw_mode == IW_MODE_MONITOR)
1102 fill_rt_header(skb_put(skb, sizeof(struct zd_rt_hdr)), mac,
1103 &stats, status);
1104 memcpy(skb_put(skb, length), buffer, length);
1105
1106 r = ieee80211_rx(ieee, skb, &stats);
Daniel Drake383956a2006-12-01 00:56:50 +00001107 if (!r)
1108 dev_kfree_skb_any(skb);
Daniel Drakee85d0912006-06-02 17:11:32 +01001109 return 0;
1110}
1111
1112static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
1113 int pri)
1114{
1115 return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
1116}
1117
1118static void set_security(struct net_device *netdev,
1119 struct ieee80211_security *sec)
1120{
1121 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
1122 struct ieee80211_security *secinfo = &ieee->sec;
1123 int keyidx;
1124
1125 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
1126
1127 for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
1128 if (sec->flags & (1<<keyidx)) {
1129 secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
1130 secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
1131 memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
1132 SCM_KEY_LEN);
1133 }
1134
1135 if (sec->flags & SEC_ACTIVE_KEY) {
1136 secinfo->active_key = sec->active_key;
1137 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1138 " .active_key = %d\n", sec->active_key);
1139 }
1140 if (sec->flags & SEC_UNICAST_GROUP) {
1141 secinfo->unicast_uses_group = sec->unicast_uses_group;
1142 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1143 " .unicast_uses_group = %d\n",
1144 sec->unicast_uses_group);
1145 }
1146 if (sec->flags & SEC_LEVEL) {
1147 secinfo->level = sec->level;
1148 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1149 " .level = %d\n", sec->level);
1150 }
1151 if (sec->flags & SEC_ENABLED) {
1152 secinfo->enabled = sec->enabled;
1153 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1154 " .enabled = %d\n", sec->enabled);
1155 }
1156 if (sec->flags & SEC_ENCRYPT) {
1157 secinfo->encrypt = sec->encrypt;
1158 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1159 " .encrypt = %d\n", sec->encrypt);
1160 }
1161 if (sec->flags & SEC_AUTH_MODE) {
1162 secinfo->auth_mode = sec->auth_mode;
1163 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1164 " .auth_mode = %d\n", sec->auth_mode);
1165 }
1166}
1167
1168static void ieee_init(struct ieee80211_device *ieee)
1169{
1170 ieee->mode = IEEE_B | IEEE_G;
1171 ieee->freq_band = IEEE80211_24GHZ_BAND;
1172 ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
1173 ieee->tx_headroom = sizeof(struct zd_ctrlset);
1174 ieee->set_security = set_security;
1175 ieee->hard_start_xmit = netdev_tx;
1176
1177 /* Software encryption/decryption for now */
1178 ieee->host_build_iv = 0;
1179 ieee->host_encrypt = 1;
1180 ieee->host_decrypt = 1;
1181
1182 /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
1183 * correctly support AUTO */
1184 ieee->iw_mode = IW_MODE_INFRA;
1185}
1186
1187static void softmac_init(struct ieee80211softmac_device *sm)
1188{
1189 sm->set_channel = set_channel;
Daniel Drakeb1382ed2006-11-22 00:06:48 +00001190 sm->bssinfo_change = bssinfo_change;
Daniel Drakee85d0912006-06-02 17:11:32 +01001191}
1192
1193struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
1194{
1195 struct zd_mac *mac = zd_netdev_mac(ndev);
1196 struct iw_statistics *iw_stats = &mac->iw_stats;
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001197 unsigned int i, count, qual_total, rssi_total;
Daniel Drakee85d0912006-06-02 17:11:32 +01001198
1199 memset(iw_stats, 0, sizeof(struct iw_statistics));
1200 /* We are not setting the status, because ieee->state is not updated
1201 * at all and this driver doesn't track authentication state.
1202 */
1203 spin_lock_irq(&mac->lock);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001204 count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
1205 mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
1206 qual_total = rssi_total = 0;
1207 for (i = 0; i < count; i++) {
1208 qual_total += mac->qual_buffer[i];
1209 rssi_total += mac->rssi_buffer[i];
1210 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001211 spin_unlock_irq(&mac->lock);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001212 iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
1213 if (count > 0) {
1214 iw_stats->qual.qual = qual_total / count;
1215 iw_stats->qual.level = rssi_total / count;
1216 iw_stats->qual.updated |=
1217 IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
1218 } else {
1219 iw_stats->qual.updated |=
1220 IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
1221 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001222 /* TODO: update counter */
1223 return iw_stats;
1224}
1225
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001226#define LINK_LED_WORK_DELAY HZ
1227
1228static void link_led_handler(void *p)
1229{
1230 struct zd_mac *mac = p;
1231 struct zd_chip *chip = &mac->chip;
1232 struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
1233 int is_associated;
1234 int r;
1235
1236 spin_lock_irq(&mac->lock);
John W. Linville41072a12006-10-17 13:47:40 -04001237 is_associated = sm->associnfo.associated != 0;
Ulrich Kunitz583afd12006-09-13 02:42:38 +01001238 spin_unlock_irq(&mac->lock);
1239
1240 r = zd_chip_control_leds(chip,
1241 is_associated ? LED_ASSOCIATED : LED_SCANNING);
1242 if (r)
1243 dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1244
1245 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1246 LINK_LED_WORK_DELAY);
1247}
1248
1249static void housekeeping_init(struct zd_mac *mac)
1250{
1251 INIT_WORK(&mac->housekeeping.link_led_work, link_led_handler, mac);
1252}
1253
1254static void housekeeping_enable(struct zd_mac *mac)
1255{
1256 dev_dbg_f(zd_mac_dev(mac), "\n");
1257 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1258 0);
1259}
1260
1261static void housekeeping_disable(struct zd_mac *mac)
1262{
1263 dev_dbg_f(zd_mac_dev(mac), "\n");
1264 cancel_rearming_delayed_workqueue(zd_workqueue,
1265 &mac->housekeeping.link_led_work);
1266 zd_chip_control_leds(&mac->chip, LED_OFF);
1267}