]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/wireless/zd1211rw/zd_mac.c
libertas: add Dan Williams as maintainer
[net-next-2.6.git] / drivers / net / wireless / zd1211rw / zd_mac.c
CommitLineData
e85d0918
DD
1/* zd_mac.c
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18#include <linux/netdevice.h>
19#include <linux/etherdevice.h>
20#include <linux/wireless.h>
21#include <linux/usb.h>
22#include <linux/jiffies.h>
23#include <net/ieee80211_radiotap.h>
24
25#include "zd_def.h"
26#include "zd_chip.h"
27#include "zd_mac.h"
28#include "zd_ieee80211.h"
29#include "zd_netdev.h"
30#include "zd_rf.h"
e85d0918
DD
31
32static void ieee_init(struct ieee80211_device *ieee);
33static void softmac_init(struct ieee80211softmac_device *sm);
6d5aefb8
DH
34static void set_rts_cts_work(struct work_struct *work);
35static void set_basic_rates_work(struct work_struct *work);
e85d0918 36
583afd1e
UK
37static void housekeeping_init(struct zd_mac *mac);
38static void housekeeping_enable(struct zd_mac *mac);
39static void housekeeping_disable(struct zd_mac *mac);
40
0ae85135 41static void set_multicast_hash_handler(struct work_struct *work);
9cdac965 42
4d1feabc
UK
43static void do_rx(unsigned long mac_ptr);
44
e85d0918
DD
45int zd_mac_init(struct zd_mac *mac,
46 struct net_device *netdev,
47 struct usb_interface *intf)
48{
49 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
50
51 memset(mac, 0, sizeof(*mac));
52 spin_lock_init(&mac->lock);
53 mac->netdev = netdev;
6d5aefb8
DH
54 INIT_DELAYED_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
55 INIT_DELAYED_WORK(&mac->set_basic_rates_work, set_basic_rates_work);
e85d0918 56
4d1feabc
UK
57 skb_queue_head_init(&mac->rx_queue);
58 tasklet_init(&mac->rx_tasklet, do_rx, (unsigned long)mac);
59 tasklet_disable(&mac->rx_tasklet);
60
e85d0918
DD
61 ieee_init(ieee);
62 softmac_init(ieee80211_priv(netdev));
63 zd_chip_init(&mac->chip, netdev, intf);
583afd1e 64 housekeeping_init(mac);
0ae85135 65 INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
e85d0918
DD
66 return 0;
67}
68
69static int reset_channel(struct zd_mac *mac)
70{
71 int r;
72 unsigned long flags;
73 const struct channel_range *range;
74
75 spin_lock_irqsave(&mac->lock, flags);
76 range = zd_channel_range(mac->regdomain);
77 if (!range->start) {
78 r = -EINVAL;
79 goto out;
80 }
81 mac->requested_channel = range->start;
82 r = 0;
83out:
84 spin_unlock_irqrestore(&mac->lock, flags);
85 return r;
86}
87
74553aed 88int zd_mac_preinit_hw(struct zd_mac *mac)
e85d0918
DD
89{
90 int r;
e85d0918 91 u8 addr[ETH_ALEN];
74553aed
DD
92
93 r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
94 if (r)
95 return r;
96
97 memcpy(mac->netdev->dev_addr, addr, ETH_ALEN);
98 return 0;
99}
100
101int zd_mac_init_hw(struct zd_mac *mac)
102{
103 int r;
104 struct zd_chip *chip = &mac->chip;
e85d0918
DD
105 u8 default_regdomain;
106
107 r = zd_chip_enable_int(chip);
108 if (r)
109 goto out;
74553aed 110 r = zd_chip_init_hw(chip);
e85d0918
DD
111 if (r)
112 goto disable_int;
113
e85d0918 114 ZD_ASSERT(!irqs_disabled());
e85d0918
DD
115
116 r = zd_read_regdomain(chip, &default_regdomain);
117 if (r)
118 goto disable_int;
119 if (!zd_regdomain_supported(default_regdomain)) {
86d95c21
DD
120 /* The vendor driver overrides the regulatory domain and
121 * allowed channel registers and unconditionally restricts
122 * available channels to 1-11 everywhere. Match their
123 * questionable behaviour only for regdomains which we don't
124 * recognise. */
125 dev_warn(zd_mac_dev(mac), "Unrecognised regulatory domain: "
126 "%#04x. Defaulting to FCC.\n", default_regdomain);
127 default_regdomain = ZD_REGDOMAIN_FCC;
e85d0918
DD
128 }
129 spin_lock_irq(&mac->lock);
130 mac->regdomain = mac->default_regdomain = default_regdomain;
131 spin_unlock_irq(&mac->lock);
132 r = reset_channel(mac);
133 if (r)
134 goto disable_int;
135
40da08bc
DD
136 /* We must inform the device that we are doing encryption/decryption in
137 * software at the moment. */
138 r = zd_set_encryption_type(chip, ENC_SNIFFER);
e85d0918
DD
139 if (r)
140 goto disable_int;
141
142 r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
143 if (r)
144 goto disable_int;
145
146 r = 0;
147disable_int:
148 zd_chip_disable_int(chip);
149out:
150 return r;
151}
152
153void zd_mac_clear(struct zd_mac *mac)
154{
9cdac965 155 flush_workqueue(zd_workqueue);
4d1feabc
UK
156 skb_queue_purge(&mac->rx_queue);
157 tasklet_kill(&mac->rx_tasklet);
e85d0918 158 zd_chip_clear(&mac->chip);
c48cf125
UK
159 ZD_ASSERT(!spin_is_locked(&mac->lock));
160 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
e85d0918
DD
161}
162
c5691235 163static int set_rx_filter(struct zd_mac *mac)
e85d0918
DD
164{
165 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
44713b1d
DD
166 u32 filter = (ieee->iw_mode == IW_MODE_MONITOR) ? ~0 : STA_RX_FILTER;
167 return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
e85d0918
DD
168}
169
c5691235
UK
170static int set_sniffer(struct zd_mac *mac)
171{
172 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
173 return zd_iowrite32(&mac->chip, CR_SNIFFER_ON,
174 ieee->iw_mode == IW_MODE_MONITOR ? 1 : 0);
175 return 0;
176}
177
178static int set_mc_hash(struct zd_mac *mac)
179{
180 struct zd_mc_hash hash;
181 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
182
183 zd_mc_clear(&hash);
184 if (ieee->iw_mode == IW_MODE_MONITOR)
185 zd_mc_add_all(&hash);
186
187 return zd_chip_set_multicast_hash(&mac->chip, &hash);
188}
189
e85d0918
DD
190int zd_mac_open(struct net_device *netdev)
191{
192 struct zd_mac *mac = zd_netdev_mac(netdev);
193 struct zd_chip *chip = &mac->chip;
74553aed 194 struct zd_usb *usb = &chip->usb;
e85d0918
DD
195 int r;
196
74553aed
DD
197 if (!usb->initialized) {
198 r = zd_usb_init_hw(usb);
199 if (r)
200 goto out;
201 }
202
4d1feabc
UK
203 tasklet_enable(&mac->rx_tasklet);
204
e85d0918
DD
205 r = zd_chip_enable_int(chip);
206 if (r < 0)
207 goto out;
208
74553aed
DD
209 r = zd_write_mac_addr(chip, netdev->dev_addr);
210 if (r)
211 goto disable_int;
212
e85d0918
DD
213 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
214 if (r < 0)
215 goto disable_int;
c5691235
UK
216 r = set_rx_filter(mac);
217 if (r)
218 goto disable_int;
219 r = set_sniffer(mac);
220 if (r)
221 goto disable_int;
222 r = set_mc_hash(mac);
e85d0918
DD
223 if (r)
224 goto disable_int;
225 r = zd_chip_switch_radio_on(chip);
226 if (r < 0)
227 goto disable_int;
228 r = zd_chip_set_channel(chip, mac->requested_channel);
229 if (r < 0)
230 goto disable_radio;
231 r = zd_chip_enable_rx(chip);
232 if (r < 0)
233 goto disable_radio;
234 r = zd_chip_enable_hwint(chip);
235 if (r < 0)
236 goto disable_rx;
237
583afd1e 238 housekeeping_enable(mac);
e85d0918
DD
239 ieee80211softmac_start(netdev);
240 return 0;
241disable_rx:
242 zd_chip_disable_rx(chip);
243disable_radio:
244 zd_chip_switch_radio_off(chip);
245disable_int:
246 zd_chip_disable_int(chip);
247out:
248 return r;
249}
250
251int zd_mac_stop(struct net_device *netdev)
252{
253 struct zd_mac *mac = zd_netdev_mac(netdev);
254 struct zd_chip *chip = &mac->chip;
255
c9a4b35d
DD
256 netif_stop_queue(netdev);
257
e85d0918
DD
258 /*
259 * The order here deliberately is a little different from the open()
260 * method, since we need to make sure there is no opportunity for RX
261 * frames to be processed by softmac after we have stopped it.
262 */
263
264 zd_chip_disable_rx(chip);
4d1feabc
UK
265 skb_queue_purge(&mac->rx_queue);
266 tasklet_disable(&mac->rx_tasklet);
583afd1e 267 housekeeping_disable(mac);
e85d0918
DD
268 ieee80211softmac_stop(netdev);
269
b1382ede
DD
270 /* Ensure no work items are running or queued from this point */
271 cancel_delayed_work(&mac->set_rts_cts_work);
272 cancel_delayed_work(&mac->set_basic_rates_work);
273 flush_workqueue(zd_workqueue);
274 mac->updating_rts_rate = 0;
275 mac->updating_basic_rates = 0;
276
e85d0918
DD
277 zd_chip_disable_hwint(chip);
278 zd_chip_switch_radio_off(chip);
279 zd_chip_disable_int(chip);
280
281 return 0;
282}
283
284int zd_mac_set_mac_address(struct net_device *netdev, void *p)
285{
286 int r;
287 unsigned long flags;
288 struct sockaddr *addr = p;
289 struct zd_mac *mac = zd_netdev_mac(netdev);
290 struct zd_chip *chip = &mac->chip;
0795af57 291 DECLARE_MAC_BUF(mac2);
e85d0918
DD
292
293 if (!is_valid_ether_addr(addr->sa_data))
294 return -EADDRNOTAVAIL;
295
296 dev_dbg_f(zd_mac_dev(mac),
0795af57 297 "Setting MAC to %s\n", print_mac(mac2, addr->sa_data));
e85d0918 298
74553aed
DD
299 if (netdev->flags & IFF_UP) {
300 r = zd_write_mac_addr(chip, addr->sa_data);
301 if (r)
302 return r;
303 }
e85d0918
DD
304
305 spin_lock_irqsave(&mac->lock, flags);
306 memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
307 spin_unlock_irqrestore(&mac->lock, flags);
308
309 return 0;
310}
311
0ae85135 312static void set_multicast_hash_handler(struct work_struct *work)
9cdac965 313{
0ae85135
JG
314 struct zd_mac *mac = container_of(work, struct zd_mac,
315 set_multicast_hash_work);
9cdac965
UK
316 struct zd_mc_hash hash;
317
318 spin_lock_irq(&mac->lock);
319 hash = mac->multicast_hash;
320 spin_unlock_irq(&mac->lock);
321
322 zd_chip_set_multicast_hash(&mac->chip, &hash);
323}
324
325void zd_mac_set_multicast_list(struct net_device *dev)
326{
9cdac965 327 struct zd_mac *mac = zd_netdev_mac(dev);
c5691235
UK
328 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
329 struct zd_mc_hash hash;
9cdac965
UK
330 struct dev_mc_list *mc;
331 unsigned long flags;
0795af57 332 DECLARE_MAC_BUF(mac2);
9cdac965 333
c5691235
UK
334 if (dev->flags & (IFF_PROMISC|IFF_ALLMULTI) ||
335 ieee->iw_mode == IW_MODE_MONITOR) {
9cdac965
UK
336 zd_mc_add_all(&hash);
337 } else {
338 zd_mc_clear(&hash);
339 for (mc = dev->mc_list; mc; mc = mc->next) {
0795af57
JP
340 dev_dbg_f(zd_mac_dev(mac), "mc addr %s\n",
341 print_mac(mac2, mc->dmi_addr));
9cdac965
UK
342 zd_mc_add_addr(&hash, mc->dmi_addr);
343 }
344 }
345
346 spin_lock_irqsave(&mac->lock, flags);
347 mac->multicast_hash = hash;
348 spin_unlock_irqrestore(&mac->lock, flags);
349 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
350}
351
e85d0918
DD
352int zd_mac_set_regdomain(struct zd_mac *mac, u8 regdomain)
353{
354 int r;
355 u8 channel;
356
357 ZD_ASSERT(!irqs_disabled());
358 spin_lock_irq(&mac->lock);
359 if (regdomain == 0) {
360 regdomain = mac->default_regdomain;
361 }
362 if (!zd_regdomain_supported(regdomain)) {
363 spin_unlock_irq(&mac->lock);
364 return -EINVAL;
365 }
366 mac->regdomain = regdomain;
367 channel = mac->requested_channel;
368 spin_unlock_irq(&mac->lock);
369
370 r = zd_geo_init(zd_mac_to_ieee80211(mac), regdomain);
371 if (r)
372 return r;
373 if (!zd_regdomain_supports_channel(regdomain, channel)) {
374 r = reset_channel(mac);
375 if (r)
376 return r;
377 }
378
379 return 0;
380}
381
382u8 zd_mac_get_regdomain(struct zd_mac *mac)
383{
384 unsigned long flags;
385 u8 regdomain;
386
387 spin_lock_irqsave(&mac->lock, flags);
388 regdomain = mac->regdomain;
389 spin_unlock_irqrestore(&mac->lock, flags);
390 return regdomain;
391}
392
b1382ede
DD
393/* Fallback to lowest rate, if rate is unknown. */
394static u8 rate_to_zd_rate(u8 rate)
395{
396 switch (rate) {
397 case IEEE80211_CCK_RATE_2MB:
398 return ZD_CCK_RATE_2M;
399 case IEEE80211_CCK_RATE_5MB:
400 return ZD_CCK_RATE_5_5M;
401 case IEEE80211_CCK_RATE_11MB:
402 return ZD_CCK_RATE_11M;
403 case IEEE80211_OFDM_RATE_6MB:
404 return ZD_OFDM_RATE_6M;
405 case IEEE80211_OFDM_RATE_9MB:
406 return ZD_OFDM_RATE_9M;
407 case IEEE80211_OFDM_RATE_12MB:
408 return ZD_OFDM_RATE_12M;
409 case IEEE80211_OFDM_RATE_18MB:
410 return ZD_OFDM_RATE_18M;
411 case IEEE80211_OFDM_RATE_24MB:
412 return ZD_OFDM_RATE_24M;
413 case IEEE80211_OFDM_RATE_36MB:
414 return ZD_OFDM_RATE_36M;
415 case IEEE80211_OFDM_RATE_48MB:
416 return ZD_OFDM_RATE_48M;
417 case IEEE80211_OFDM_RATE_54MB:
418 return ZD_OFDM_RATE_54M;
419 }
420 return ZD_CCK_RATE_1M;
421}
422
423static u16 rate_to_cr_rate(u8 rate)
424{
425 switch (rate) {
426 case IEEE80211_CCK_RATE_2MB:
427 return CR_RATE_1M;
428 case IEEE80211_CCK_RATE_5MB:
429 return CR_RATE_5_5M;
430 case IEEE80211_CCK_RATE_11MB:
431 return CR_RATE_11M;
432 case IEEE80211_OFDM_RATE_6MB:
433 return CR_RATE_6M;
434 case IEEE80211_OFDM_RATE_9MB:
435 return CR_RATE_9M;
436 case IEEE80211_OFDM_RATE_12MB:
437 return CR_RATE_12M;
438 case IEEE80211_OFDM_RATE_18MB:
439 return CR_RATE_18M;
440 case IEEE80211_OFDM_RATE_24MB:
441 return CR_RATE_24M;
442 case IEEE80211_OFDM_RATE_36MB:
443 return CR_RATE_36M;
444 case IEEE80211_OFDM_RATE_48MB:
445 return CR_RATE_48M;
446 case IEEE80211_OFDM_RATE_54MB:
447 return CR_RATE_54M;
448 }
449 return CR_RATE_1M;
450}
451
452static void try_enable_tx(struct zd_mac *mac)
453{
454 unsigned long flags;
455
456 spin_lock_irqsave(&mac->lock, flags);
457 if (mac->updating_rts_rate == 0 && mac->updating_basic_rates == 0)
458 netif_wake_queue(mac->netdev);
459 spin_unlock_irqrestore(&mac->lock, flags);
460}
461
6d5aefb8 462static void set_rts_cts_work(struct work_struct *work)
b1382ede 463{
6d5aefb8
DH
464 struct zd_mac *mac =
465 container_of(work, struct zd_mac, set_rts_cts_work.work);
b1382ede
DD
466 unsigned long flags;
467 u8 rts_rate;
468 unsigned int short_preamble;
469
470 mutex_lock(&mac->chip.mutex);
471
472 spin_lock_irqsave(&mac->lock, flags);
473 mac->updating_rts_rate = 0;
474 rts_rate = mac->rts_rate;
475 short_preamble = mac->short_preamble;
476 spin_unlock_irqrestore(&mac->lock, flags);
477
478 zd_chip_set_rts_cts_rate_locked(&mac->chip, rts_rate, short_preamble);
479 mutex_unlock(&mac->chip.mutex);
480
481 try_enable_tx(mac);
482}
483
6d5aefb8 484static void set_basic_rates_work(struct work_struct *work)
b1382ede 485{
6d5aefb8
DH
486 struct zd_mac *mac =
487 container_of(work, struct zd_mac, set_basic_rates_work.work);
b1382ede
DD
488 unsigned long flags;
489 u16 basic_rates;
490
491 mutex_lock(&mac->chip.mutex);
492
493 spin_lock_irqsave(&mac->lock, flags);
494 mac->updating_basic_rates = 0;
495 basic_rates = mac->basic_rates;
496 spin_unlock_irqrestore(&mac->lock, flags);
497
498 zd_chip_set_basic_rates_locked(&mac->chip, basic_rates);
499 mutex_unlock(&mac->chip.mutex);
500
501 try_enable_tx(mac);
502}
503
504static void bssinfo_change(struct net_device *netdev, u32 changes)
505{
506 struct zd_mac *mac = zd_netdev_mac(netdev);
507 struct ieee80211softmac_device *softmac = ieee80211_priv(netdev);
508 struct ieee80211softmac_bss_info *bssinfo = &softmac->bssinfo;
509 int need_set_rts_cts = 0;
510 int need_set_rates = 0;
511 u16 basic_rates;
512 unsigned long flags;
513
514 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
515
516 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_SHORT_PREAMBLE) {
517 spin_lock_irqsave(&mac->lock, flags);
518 mac->short_preamble = bssinfo->short_preamble;
519 spin_unlock_irqrestore(&mac->lock, flags);
520 need_set_rts_cts = 1;
521 }
522
523 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_RATES) {
524 /* Set RTS rate to highest available basic rate */
4d1feabc 525 u8 hi_rate = ieee80211softmac_highest_supported_rate(softmac,
b1382ede 526 &bssinfo->supported_rates, 1);
4d1feabc 527 hi_rate = rate_to_zd_rate(hi_rate);
b1382ede
DD
528
529 spin_lock_irqsave(&mac->lock, flags);
4d1feabc
UK
530 if (hi_rate != mac->rts_rate) {
531 mac->rts_rate = hi_rate;
b1382ede
DD
532 need_set_rts_cts = 1;
533 }
534 spin_unlock_irqrestore(&mac->lock, flags);
535
536 /* Set basic rates */
537 need_set_rates = 1;
538 if (bssinfo->supported_rates.count == 0) {
539 /* Allow the device to be flexible */
540 basic_rates = CR_RATES_80211B | CR_RATES_80211G;
541 } else {
542 int i = 0;
543 basic_rates = 0;
544
545 for (i = 0; i < bssinfo->supported_rates.count; i++) {
546 u16 rate = bssinfo->supported_rates.rates[i];
547 if ((rate & IEEE80211_BASIC_RATE_MASK) == 0)
548 continue;
549
550 rate &= ~IEEE80211_BASIC_RATE_MASK;
551 basic_rates |= rate_to_cr_rate(rate);
552 }
553 }
554 spin_lock_irqsave(&mac->lock, flags);
555 mac->basic_rates = basic_rates;
556 spin_unlock_irqrestore(&mac->lock, flags);
557 }
558
559 /* Schedule any changes we made above */
560
561 spin_lock_irqsave(&mac->lock, flags);
562 if (need_set_rts_cts && !mac->updating_rts_rate) {
563 mac->updating_rts_rate = 1;
564 netif_stop_queue(mac->netdev);
6d5aefb8 565 queue_delayed_work(zd_workqueue, &mac->set_rts_cts_work, 0);
b1382ede
DD
566 }
567 if (need_set_rates && !mac->updating_basic_rates) {
568 mac->updating_basic_rates = 1;
569 netif_stop_queue(mac->netdev);
6d5aefb8
DH
570 queue_delayed_work(zd_workqueue, &mac->set_basic_rates_work,
571 0);
b1382ede
DD
572 }
573 spin_unlock_irqrestore(&mac->lock, flags);
574}
575
e85d0918
DD
576static void set_channel(struct net_device *netdev, u8 channel)
577{
578 struct zd_mac *mac = zd_netdev_mac(netdev);
579
580 dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
581
582 zd_chip_set_channel(&mac->chip, channel);
583}
584
e85d0918
DD
585int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
586{
587 unsigned long lock_flags;
588 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
589
590 if (ieee->iw_mode == IW_MODE_INFRA)
591 return -EPERM;
592
593 spin_lock_irqsave(&mac->lock, lock_flags);
594 if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
595 spin_unlock_irqrestore(&mac->lock, lock_flags);
596 return -EINVAL;
597 }
598 mac->requested_channel = channel;
599 spin_unlock_irqrestore(&mac->lock, lock_flags);
600 if (netif_running(mac->netdev))
601 return zd_chip_set_channel(&mac->chip, channel);
602 else
603 return 0;
604}
605
84bc715c 606u8 zd_mac_get_channel(struct zd_mac *mac)
e85d0918 607{
84bc715c 608 u8 channel = zd_chip_get_channel(&mac->chip);
e85d0918 609
84bc715c
DD
610 dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
611 return channel;
e85d0918
DD
612}
613
e85d0918
DD
614int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
615{
616 struct ieee80211_device *ieee;
617
618 switch (mode) {
619 case IW_MODE_AUTO:
620 case IW_MODE_ADHOC:
621 case IW_MODE_INFRA:
622 mac->netdev->type = ARPHRD_ETHER;
623 break;
624 case IW_MODE_MONITOR:
625 mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
626 break;
627 default:
628 dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
629 return -EINVAL;
630 }
631
632 ieee = zd_mac_to_ieee80211(mac);
633 ZD_ASSERT(!irqs_disabled());
634 spin_lock_irq(&ieee->lock);
635 ieee->iw_mode = mode;
636 spin_unlock_irq(&ieee->lock);
637
c5691235
UK
638 if (netif_running(mac->netdev)) {
639 int r = set_rx_filter(mac);
640 if (r)
641 return r;
642 return set_sniffer(mac);
643 }
e85d0918
DD
644
645 return 0;
646}
647
648int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
649{
650 unsigned long flags;
651 struct ieee80211_device *ieee;
652
653 ieee = zd_mac_to_ieee80211(mac);
654 spin_lock_irqsave(&ieee->lock, flags);
655 *mode = ieee->iw_mode;
656 spin_unlock_irqrestore(&ieee->lock, flags);
657 return 0;
658}
659
660int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
661{
662 int i;
663 const struct channel_range *channel_range;
664 u8 regdomain;
665
666 memset(range, 0, sizeof(*range));
667
668 /* FIXME: Not so important and depends on the mode. For 802.11g
669 * usually this value is used. It seems to be that Bit/s number is
670 * given here.
671 */
672 range->throughput = 27 * 1000 * 1000;
673
674 range->max_qual.qual = 100;
675 range->max_qual.level = 100;
676
677 /* FIXME: Needs still to be tuned. */
678 range->avg_qual.qual = 71;
679 range->avg_qual.level = 80;
680
681 /* FIXME: depends on standard? */
682 range->min_rts = 256;
683 range->max_rts = 2346;
684
685 range->min_frag = MIN_FRAG_THRESHOLD;
686 range->max_frag = MAX_FRAG_THRESHOLD;
687
688 range->max_encoding_tokens = WEP_KEYS;
689 range->num_encoding_sizes = 2;
690 range->encoding_size[0] = 5;
691 range->encoding_size[1] = WEP_KEY_LEN;
692
693 range->we_version_compiled = WIRELESS_EXT;
694 range->we_version_source = 20;
695
ff9b99bc
DD
696 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
697 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
698
e85d0918
DD
699 ZD_ASSERT(!irqs_disabled());
700 spin_lock_irq(&mac->lock);
701 regdomain = mac->regdomain;
702 spin_unlock_irq(&mac->lock);
703 channel_range = zd_channel_range(regdomain);
704
705 range->num_channels = channel_range->end - channel_range->start;
706 range->old_num_channels = range->num_channels;
707 range->num_frequency = range->num_channels;
708 range->old_num_frequency = range->num_frequency;
709
710 for (i = 0; i < range->num_frequency; i++) {
711 struct iw_freq *freq = &range->freq[i];
712 freq->i = channel_range->start + i;
713 zd_channel_to_freq(freq, freq->i);
714 }
715
716 return 0;
717}
718
b1cd8416 719static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
e85d0918 720{
64f222cc
UK
721 /* ZD_PURE_RATE() must be used to remove the modulation type flag of
722 * the zd-rate values. */
e85d0918 723 static const u8 rate_divisor[] = {
64f222cc
UK
724 [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
725 [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
726
727 /* bits must be doubled */
728 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
729
730 [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
731 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
732 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
733 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
734 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
735 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
736 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
737 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
738 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
e85d0918
DD
739 };
740
741 u32 bits = (u32)tx_length * 8;
742 u32 divisor;
743
64f222cc 744 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
e85d0918
DD
745 if (divisor == 0)
746 return -EINVAL;
747
b1cd8416
DD
748 switch (zd_rate) {
749 case ZD_CCK_RATE_5_5M:
e85d0918
DD
750 bits = (2*bits) + 10; /* round up to the next integer */
751 break;
b1cd8416 752 case ZD_CCK_RATE_11M:
e85d0918
DD
753 if (service) {
754 u32 t = bits % 11;
755 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
756 if (0 < t && t <= 3) {
757 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
758 }
759 }
760 bits += 10; /* round up to the next integer */
761 break;
762 }
763
764 return bits/divisor;
765}
766
e85d0918
DD
767static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
768 struct ieee80211_hdr_4addr *hdr)
769{
770 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
771 u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
64f222cc 772 u8 rate;
e85d0918 773 int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
b1382ede
DD
774 int is_multicast = is_multicast_ether_addr(hdr->addr1);
775 int short_preamble = ieee80211softmac_short_preamble_ok(softmac,
776 is_multicast, is_mgt);
e85d0918 777
b1382ede 778 rate = ieee80211softmac_suggest_txrate(softmac, is_multicast, is_mgt);
64f222cc 779 cs->modulation = rate_to_zd_rate(rate);
e85d0918 780
64f222cc
UK
781 /* Set short preamble bit when appropriate */
782 if (short_preamble && ZD_MODULATION_TYPE(cs->modulation) == ZD_CCK
783 && cs->modulation != ZD_CCK_RATE_1M)
784 cs->modulation |= ZD_CCK_PREA_SHORT;
e85d0918
DD
785}
786
787static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
788 struct ieee80211_hdr_4addr *header)
789{
b1382ede 790 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
e85d0918
DD
791 unsigned int tx_length = le16_to_cpu(cs->tx_length);
792 u16 fctl = le16_to_cpu(header->frame_ctl);
793 u16 ftype = WLAN_FC_GET_TYPE(fctl);
794 u16 stype = WLAN_FC_GET_STYPE(fctl);
795
796 /*
b1382ede 797 * CONTROL TODO:
e85d0918
DD
798 * - if backoff needed, enable bit 0
799 * - if burst (backoff not needed) disable bit 0
e85d0918
DD
800 */
801
802 cs->control = 0;
803
804 /* First fragment */
805 if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
806 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
807
808 /* Multicast */
809 if (is_multicast_ether_addr(header->addr1))
810 cs->control |= ZD_CS_MULTICAST;
811
812 /* PS-POLL */
69dad6e5 813 if (ftype == IEEE80211_FTYPE_CTL && stype == IEEE80211_STYPE_PSPOLL)
e85d0918
DD
814 cs->control |= ZD_CS_PS_POLL_FRAME;
815
b1382ede 816 /* Unicast data frames over the threshold should have RTS */
e85d0918 817 if (!is_multicast_ether_addr(header->addr1) &&
b1382ede
DD
818 ftype != IEEE80211_FTYPE_MGMT &&
819 tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
820 cs->control |= ZD_CS_RTS;
821
822 /* Use CTS-to-self protection if required */
64f222cc 823 if (ZD_MODULATION_TYPE(cs->modulation) == ZD_OFDM &&
b1382ede
DD
824 ieee80211softmac_protection_needed(softmac)) {
825 /* FIXME: avoid sending RTS *and* self-CTS, is that correct? */
826 cs->control &= ~ZD_CS_RTS;
827 cs->control |= ZD_CS_SELF_CTS;
e85d0918
DD
828 }
829
830 /* FIXME: Management frame? */
831}
832
833static int fill_ctrlset(struct zd_mac *mac,
834 struct ieee80211_txb *txb,
835 int frag_num)
836{
837 int r;
838 struct sk_buff *skb = txb->fragments[frag_num];
839 struct ieee80211_hdr_4addr *hdr =
840 (struct ieee80211_hdr_4addr *) skb->data;
841 unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
842 unsigned int next_frag_len;
843 unsigned int packet_length;
844 struct zd_ctrlset *cs = (struct zd_ctrlset *)
845 skb_push(skb, sizeof(struct zd_ctrlset));
846
847 if (frag_num+1 < txb->nr_frags) {
848 next_frag_len = txb->fragments[frag_num+1]->len +
849 IEEE80211_FCS_LEN;
850 } else {
851 next_frag_len = 0;
852 }
853 ZD_ASSERT(frag_len <= 0xffff);
854 ZD_ASSERT(next_frag_len <= 0xffff);
855
856 cs_set_modulation(mac, cs, hdr);
857
858 cs->tx_length = cpu_to_le16(frag_len);
859
860 cs_set_control(mac, cs, hdr);
861
862 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
863 ZD_ASSERT(packet_length <= 0xffff);
864 /* ZD1211B: Computing the length difference this way, gives us
865 * flexibility to compute the packet length.
866 */
74553aed 867 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
e85d0918
DD
868 packet_length - frag_len : packet_length);
869
870 /*
871 * CURRENT LENGTH:
872 * - transmit frame length in microseconds
873 * - seems to be derived from frame length
874 * - see Cal_Us_Service() in zdinlinef.h
875 * - if macp->bTxBurstEnable is enabled, then multiply by 4
876 * - bTxBurstEnable is never set in the vendor driver
877 *
878 * SERVICE:
879 * - "for PLCP configuration"
880 * - always 0 except in some situations at 802.11b 11M
881 * - see line 53 of zdinlinef.h
882 */
883 cs->service = 0;
64f222cc 884 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
e85d0918
DD
885 le16_to_cpu(cs->tx_length));
886 if (r < 0)
887 return r;
888 cs->current_length = cpu_to_le16(r);
889
890 if (next_frag_len == 0) {
891 cs->next_frame_length = 0;
892 } else {
64f222cc 893 r = zd_calc_tx_length_us(NULL, ZD_RATE(cs->modulation),
e85d0918
DD
894 next_frag_len);
895 if (r < 0)
896 return r;
897 cs->next_frame_length = cpu_to_le16(r);
898 }
899
900 return 0;
901}
902
903static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
904{
905 int i, r;
22d3405f 906 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
e85d0918
DD
907
908 for (i = 0; i < txb->nr_frags; i++) {
909 struct sk_buff *skb = txb->fragments[i];
910
911 r = fill_ctrlset(mac, txb, i);
22d3405f
UK
912 if (r) {
913 ieee->stats.tx_dropped++;
e85d0918 914 return r;
22d3405f 915 }
e85d0918 916 r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
22d3405f
UK
917 if (r) {
918 ieee->stats.tx_dropped++;
e85d0918 919 return r;
22d3405f 920 }
e85d0918
DD
921 }
922
923 /* FIXME: shouldn't this be handled by the upper layers? */
924 mac->netdev->trans_start = jiffies;
925
926 ieee80211_txb_free(txb);
927 return 0;
928}
929
930struct zd_rt_hdr {
931 struct ieee80211_radiotap_header rt_hdr;
932 u8 rt_flags;
99f65f25 933 u8 rt_rate;
e85d0918
DD
934 u16 rt_channel;
935 u16 rt_chbitmask;
a88556a4 936} __attribute__((packed));
e85d0918
DD
937
938static void fill_rt_header(void *buffer, struct zd_mac *mac,
939 const struct ieee80211_rx_stats *stats,
940 const struct rx_status *status)
941{
942 struct zd_rt_hdr *hdr = buffer;
943
944 hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
945 hdr->rt_hdr.it_pad = 0;
946 hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
947 hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
948 (1 << IEEE80211_RADIOTAP_CHANNEL) |
949 (1 << IEEE80211_RADIOTAP_RATE));
950
951 hdr->rt_flags = 0;
952 if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
953 hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
954
99f65f25
UK
955 hdr->rt_rate = stats->rate / 5;
956
e85d0918
DD
957 /* FIXME: 802.11a */
958 hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
959 _zd_chip_get_channel(&mac->chip)));
960 hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
961 ((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
962 ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
e85d0918
DD
963}
964
965/* Returns 1 if the data packet is for us and 0 otherwise. */
966static int is_data_packet_for_us(struct ieee80211_device *ieee,
967 struct ieee80211_hdr_4addr *hdr)
968{
969 struct net_device *netdev = ieee->dev;
970 u16 fc = le16_to_cpu(hdr->frame_ctl);
971
972 ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
973
974 switch (ieee->iw_mode) {
975 case IW_MODE_ADHOC:
976 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
832855dc 977 compare_ether_addr(hdr->addr3, ieee->bssid) != 0)
e85d0918
DD
978 return 0;
979 break;
980 case IW_MODE_AUTO:
981 case IW_MODE_INFRA:
982 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
983 IEEE80211_FCTL_FROMDS ||
832855dc 984 compare_ether_addr(hdr->addr2, ieee->bssid) != 0)
e85d0918
DD
985 return 0;
986 break;
987 default:
988 ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
989 return 0;
990 }
991
832855dc 992 return compare_ether_addr(hdr->addr1, netdev->dev_addr) == 0 ||
9cdac965 993 (is_multicast_ether_addr(hdr->addr1) &&
832855dc 994 compare_ether_addr(hdr->addr3, netdev->dev_addr) != 0) ||
e85d0918
DD
995 (netdev->flags & IFF_PROMISC);
996}
997
741fec53
UK
998/* Filters received packets. The function returns 1 if the packet should be
999 * forwarded to ieee80211_rx(). If the packet should be ignored the function
1000 * returns 0. If an invalid packet is found the function returns -EINVAL.
1001 *
1002 * The function calls ieee80211_rx_mgt() directly.
e85d0918
DD
1003 *
1004 * It has been based on ieee80211_rx_any.
1005 */
1006static int filter_rx(struct ieee80211_device *ieee,
1007 const u8 *buffer, unsigned int length,
1008 struct ieee80211_rx_stats *stats)
1009{
1010 struct ieee80211_hdr_4addr *hdr;
1011 u16 fc;
1012
1013 if (ieee->iw_mode == IW_MODE_MONITOR)
1014 return 1;
1015
1016 hdr = (struct ieee80211_hdr_4addr *)buffer;
1017 fc = le16_to_cpu(hdr->frame_ctl);
1018 if ((fc & IEEE80211_FCTL_VERS) != 0)
1019 return -EINVAL;
1020
1021 switch (WLAN_FC_GET_TYPE(fc)) {
1022 case IEEE80211_FTYPE_MGMT:
1023 if (length < sizeof(struct ieee80211_hdr_3addr))
1024 return -EINVAL;
1025 ieee80211_rx_mgt(ieee, hdr, stats);
1026 return 0;
1027 case IEEE80211_FTYPE_CTL:
e85d0918
DD
1028 return 0;
1029 case IEEE80211_FTYPE_DATA:
741fec53 1030 /* Ignore invalid short buffers */
e85d0918
DD
1031 if (length < sizeof(struct ieee80211_hdr_3addr))
1032 return -EINVAL;
1033 return is_data_packet_for_us(ieee, hdr);
1034 }
1035
1036 return -EINVAL;
1037}
1038
db888aed
UK
1039static void update_qual_rssi(struct zd_mac *mac,
1040 const u8 *buffer, unsigned int length,
1041 u8 qual_percent, u8 rssi_percent)
e85d0918
DD
1042{
1043 unsigned long flags;
db888aed
UK
1044 struct ieee80211_hdr_3addr *hdr;
1045 int i;
1046
1047 hdr = (struct ieee80211_hdr_3addr *)buffer;
1048 if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
1049 return;
832855dc 1050 if (compare_ether_addr(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid) != 0)
db888aed 1051 return;
e85d0918
DD
1052
1053 spin_lock_irqsave(&mac->lock, flags);
db888aed
UK
1054 i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
1055 mac->qual_buffer[i] = qual_percent;
1056 mac->rssi_buffer[i] = rssi_percent;
1057 mac->stats_count++;
e85d0918
DD
1058 spin_unlock_irqrestore(&mac->lock, flags);
1059}
1060
1061static int fill_rx_stats(struct ieee80211_rx_stats *stats,
1062 const struct rx_status **pstatus,
1063 struct zd_mac *mac,
1064 const u8 *buffer, unsigned int length)
1065{
1066 const struct rx_status *status;
1067
937a049d
UK
1068 *pstatus = status = (struct rx_status *)
1069 (buffer + (length - sizeof(struct rx_status)));
e85d0918 1070 if (status->frame_status & ZD_RX_ERROR) {
22d3405f
UK
1071 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1072 ieee->stats.rx_errors++;
1073 if (status->frame_status & ZD_RX_TIMEOUT_ERROR)
1074 ieee->stats.rx_missed_errors++;
1075 else if (status->frame_status & ZD_RX_FIFO_OVERRUN_ERROR)
1076 ieee->stats.rx_fifo_errors++;
1077 else if (status->frame_status & ZD_RX_DECRYPTION_ERROR)
1078 ieee->ieee_stats.rx_discards_undecryptable++;
1079 else if (status->frame_status & ZD_RX_CRC32_ERROR) {
1080 ieee->stats.rx_crc_errors++;
1081 ieee->ieee_stats.rx_fcs_errors++;
1082 }
1083 else if (status->frame_status & ZD_RX_CRC16_ERROR)
1084 ieee->stats.rx_crc_errors++;
e85d0918
DD
1085 return -EINVAL;
1086 }
22d3405f 1087
e85d0918
DD
1088 memset(stats, 0, sizeof(struct ieee80211_rx_stats));
1089 stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
1090 + sizeof(struct rx_status));
1091 /* FIXME: 802.11a */
1092 stats->freq = IEEE80211_24GHZ_BAND;
1093 stats->received_channel = _zd_chip_get_channel(&mac->chip);
1094 stats->rssi = zd_rx_strength_percent(status->signal_strength);
1095 stats->signal = zd_rx_qual_percent(buffer,
1096 length - sizeof(struct rx_status),
1097 status);
1098 stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
1099 stats->rate = zd_rx_rate(buffer, status);
1100 if (stats->rate)
1101 stats->mask |= IEEE80211_STATMASK_RATE;
1102
e85d0918
DD
1103 return 0;
1104}
1105
4d1feabc 1106static void zd_mac_rx(struct zd_mac *mac, struct sk_buff *skb)
e85d0918
DD
1107{
1108 int r;
1109 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1110 struct ieee80211_rx_stats stats;
1111 const struct rx_status *status;
e85d0918 1112
4d1feabc
UK
1113 if (skb->len < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
1114 IEEE80211_FCS_LEN + sizeof(struct rx_status))
1115 {
22d3405f
UK
1116 ieee->stats.rx_errors++;
1117 ieee->stats.rx_length_errors++;
4d1feabc
UK
1118 goto free_skb;
1119 }
e85d0918 1120
4d1feabc
UK
1121 r = fill_rx_stats(&stats, &status, mac, skb->data, skb->len);
1122 if (r) {
22d3405f
UK
1123 /* Only packets with rx errors are included here.
1124 * The error stats have already been set in fill_rx_stats.
1125 */
4d1feabc
UK
1126 goto free_skb;
1127 }
e85d0918 1128
4d1feabc
UK
1129 __skb_pull(skb, ZD_PLCP_HEADER_SIZE);
1130 __skb_trim(skb, skb->len -
1131 (IEEE80211_FCS_LEN + sizeof(struct rx_status)));
e85d0918 1132
4d1feabc
UK
1133 update_qual_rssi(mac, skb->data, skb->len, stats.signal,
1134 status->signal_strength);
db888aed 1135
4d1feabc
UK
1136 r = filter_rx(ieee, skb->data, skb->len, &stats);
1137 if (r <= 0) {
22d3405f
UK
1138 if (r < 0) {
1139 ieee->stats.rx_errors++;
4d1feabc 1140 dev_dbg_f(zd_mac_dev(mac), "Error in packet.\n");
22d3405f 1141 }
4d1feabc
UK
1142 goto free_skb;
1143 }
e85d0918 1144
e85d0918 1145 if (ieee->iw_mode == IW_MODE_MONITOR)
4d1feabc 1146 fill_rt_header(skb_push(skb, sizeof(struct zd_rt_hdr)), mac,
e85d0918 1147 &stats, status);
e85d0918
DD
1148
1149 r = ieee80211_rx(ieee, skb, &stats);
4d1feabc
UK
1150 if (r)
1151 return;
1152free_skb:
1153 /* We are always in a soft irq. */
1154 dev_kfree_skb(skb);
1155}
1156
1157static void do_rx(unsigned long mac_ptr)
1158{
1159 struct zd_mac *mac = (struct zd_mac *)mac_ptr;
1160 struct sk_buff *skb;
1161
1162 while ((skb = skb_dequeue(&mac->rx_queue)) != NULL)
1163 zd_mac_rx(mac, skb);
1164}
1165
1166int zd_mac_rx_irq(struct zd_mac *mac, const u8 *buffer, unsigned int length)
1167{
1168 struct sk_buff *skb;
1169
1170 skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
1171 if (!skb) {
22d3405f 1172 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
4d1feabc 1173 dev_warn(zd_mac_dev(mac), "Could not allocate skb.\n");
22d3405f 1174 ieee->stats.rx_dropped++;
4d1feabc
UK
1175 return -ENOMEM;
1176 }
1177 skb_reserve(skb, sizeof(struct zd_rt_hdr));
1178 memcpy(__skb_put(skb, length), buffer, length);
1179 skb_queue_tail(&mac->rx_queue, skb);
1180 tasklet_schedule(&mac->rx_tasklet);
e85d0918
DD
1181 return 0;
1182}
1183
1184static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
1185 int pri)
1186{
1187 return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
1188}
1189
1190static void set_security(struct net_device *netdev,
1191 struct ieee80211_security *sec)
1192{
1193 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
1194 struct ieee80211_security *secinfo = &ieee->sec;
1195 int keyidx;
1196
1197 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
1198
1199 for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
1200 if (sec->flags & (1<<keyidx)) {
1201 secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
1202 secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
1203 memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
1204 SCM_KEY_LEN);
1205 }
1206
1207 if (sec->flags & SEC_ACTIVE_KEY) {
1208 secinfo->active_key = sec->active_key;
1209 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1210 " .active_key = %d\n", sec->active_key);
1211 }
1212 if (sec->flags & SEC_UNICAST_GROUP) {
1213 secinfo->unicast_uses_group = sec->unicast_uses_group;
1214 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1215 " .unicast_uses_group = %d\n",
1216 sec->unicast_uses_group);
1217 }
1218 if (sec->flags & SEC_LEVEL) {
1219 secinfo->level = sec->level;
1220 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1221 " .level = %d\n", sec->level);
1222 }
1223 if (sec->flags & SEC_ENABLED) {
1224 secinfo->enabled = sec->enabled;
1225 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1226 " .enabled = %d\n", sec->enabled);
1227 }
1228 if (sec->flags & SEC_ENCRYPT) {
1229 secinfo->encrypt = sec->encrypt;
1230 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1231 " .encrypt = %d\n", sec->encrypt);
1232 }
1233 if (sec->flags & SEC_AUTH_MODE) {
1234 secinfo->auth_mode = sec->auth_mode;
1235 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1236 " .auth_mode = %d\n", sec->auth_mode);
1237 }
1238}
1239
1240static void ieee_init(struct ieee80211_device *ieee)
1241{
1242 ieee->mode = IEEE_B | IEEE_G;
1243 ieee->freq_band = IEEE80211_24GHZ_BAND;
1244 ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
1245 ieee->tx_headroom = sizeof(struct zd_ctrlset);
1246 ieee->set_security = set_security;
1247 ieee->hard_start_xmit = netdev_tx;
1248
1249 /* Software encryption/decryption for now */
1250 ieee->host_build_iv = 0;
1251 ieee->host_encrypt = 1;
1252 ieee->host_decrypt = 1;
1253
1254 /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
1255 * correctly support AUTO */
1256 ieee->iw_mode = IW_MODE_INFRA;
1257}
1258
1259static void softmac_init(struct ieee80211softmac_device *sm)
1260{
1261 sm->set_channel = set_channel;
b1382ede 1262 sm->bssinfo_change = bssinfo_change;
e85d0918
DD
1263}
1264
1265struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
1266{
1267 struct zd_mac *mac = zd_netdev_mac(ndev);
1268 struct iw_statistics *iw_stats = &mac->iw_stats;
db888aed 1269 unsigned int i, count, qual_total, rssi_total;
e85d0918
DD
1270
1271 memset(iw_stats, 0, sizeof(struct iw_statistics));
1272 /* We are not setting the status, because ieee->state is not updated
1273 * at all and this driver doesn't track authentication state.
1274 */
1275 spin_lock_irq(&mac->lock);
db888aed
UK
1276 count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
1277 mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
1278 qual_total = rssi_total = 0;
1279 for (i = 0; i < count; i++) {
1280 qual_total += mac->qual_buffer[i];
1281 rssi_total += mac->rssi_buffer[i];
1282 }
e85d0918 1283 spin_unlock_irq(&mac->lock);
db888aed
UK
1284 iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
1285 if (count > 0) {
1286 iw_stats->qual.qual = qual_total / count;
1287 iw_stats->qual.level = rssi_total / count;
1288 iw_stats->qual.updated |=
1289 IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
1290 } else {
1291 iw_stats->qual.updated |=
1292 IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
1293 }
e85d0918
DD
1294 /* TODO: update counter */
1295 return iw_stats;
1296}
1297
583afd1e
UK
1298#define LINK_LED_WORK_DELAY HZ
1299
c4028958 1300static void link_led_handler(struct work_struct *work)
583afd1e 1301{
c4028958
DH
1302 struct zd_mac *mac =
1303 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
583afd1e
UK
1304 struct zd_chip *chip = &mac->chip;
1305 struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
1306 int is_associated;
1307 int r;
1308
1309 spin_lock_irq(&mac->lock);
41072a1b 1310 is_associated = sm->associnfo.associated != 0;
583afd1e
UK
1311 spin_unlock_irq(&mac->lock);
1312
1313 r = zd_chip_control_leds(chip,
1314 is_associated ? LED_ASSOCIATED : LED_SCANNING);
1315 if (r)
1316 dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1317
1318 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1319 LINK_LED_WORK_DELAY);
1320}
1321
1322static void housekeeping_init(struct zd_mac *mac)
1323{
c4028958 1324 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
583afd1e
UK
1325}
1326
1327static void housekeeping_enable(struct zd_mac *mac)
1328{
1329 dev_dbg_f(zd_mac_dev(mac), "\n");
1330 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1331 0);
1332}
1333
1334static void housekeeping_disable(struct zd_mac *mac)
1335{
1336 dev_dbg_f(zd_mac_dev(mac), "\n");
1337 cancel_rearming_delayed_workqueue(zd_workqueue,
1338 &mac->housekeeping.link_led_work);
1339 zd_chip_control_leds(&mac->chip, LED_OFF);
1340}