blob: 522de3f0dfaff27f86c126c779b1c98de437439e [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);
David Howells6d5aefb2006-12-05 19:36:26 +000035static void set_rts_cts_work(struct work_struct *work);
36static void set_basic_rates_work(struct work_struct *work);
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
Jeff Garzik0ae85132006-12-07 06:30:30 -050042static void set_multicast_hash_handler(struct work_struct *work);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +000043
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -080044static void do_rx(unsigned long mac_ptr);
45
Daniel Drakee85d0912006-06-02 17:11:32 +010046int zd_mac_init(struct zd_mac *mac,
47 struct net_device *netdev,
48 struct usb_interface *intf)
49{
50 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
51
52 memset(mac, 0, sizeof(*mac));
53 spin_lock_init(&mac->lock);
54 mac->netdev = netdev;
David Howells6d5aefb2006-12-05 19:36:26 +000055 INIT_DELAYED_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
56 INIT_DELAYED_WORK(&mac->set_basic_rates_work, set_basic_rates_work);
Daniel Drakee85d0912006-06-02 17:11:32 +010057
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -080058 skb_queue_head_init(&mac->rx_queue);
59 tasklet_init(&mac->rx_tasklet, do_rx, (unsigned long)mac);
60 tasklet_disable(&mac->rx_tasklet);
61
Daniel Drakee85d0912006-06-02 17:11:32 +010062 ieee_init(ieee);
63 softmac_init(ieee80211_priv(netdev));
64 zd_chip_init(&mac->chip, netdev, intf);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +010065 housekeeping_init(mac);
Jeff Garzik0ae85132006-12-07 06:30:30 -050066 INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
Daniel Drakee85d0912006-06-02 17:11:32 +010067 return 0;
68}
69
70static int reset_channel(struct zd_mac *mac)
71{
72 int r;
73 unsigned long flags;
74 const struct channel_range *range;
75
76 spin_lock_irqsave(&mac->lock, flags);
77 range = zd_channel_range(mac->regdomain);
78 if (!range->start) {
79 r = -EINVAL;
80 goto out;
81 }
82 mac->requested_channel = range->start;
83 r = 0;
84out:
85 spin_unlock_irqrestore(&mac->lock, flags);
86 return r;
87}
88
89int zd_mac_init_hw(struct zd_mac *mac, u8 device_type)
90{
91 int r;
92 struct zd_chip *chip = &mac->chip;
93 u8 addr[ETH_ALEN];
94 u8 default_regdomain;
95
96 r = zd_chip_enable_int(chip);
97 if (r)
98 goto out;
99 r = zd_chip_init_hw(chip, device_type);
100 if (r)
101 goto disable_int;
102
103 zd_get_e2p_mac_addr(chip, addr);
104 r = zd_write_mac_addr(chip, addr);
105 if (r)
106 goto disable_int;
107 ZD_ASSERT(!irqs_disabled());
108 spin_lock_irq(&mac->lock);
109 memcpy(mac->netdev->dev_addr, addr, ETH_ALEN);
110 spin_unlock_irq(&mac->lock);
111
112 r = zd_read_regdomain(chip, &default_regdomain);
113 if (r)
114 goto disable_int;
115 if (!zd_regdomain_supported(default_regdomain)) {
Daniel Drake86d95c212007-07-01 18:21:49 +0100116 /* The vendor driver overrides the regulatory domain and
117 * allowed channel registers and unconditionally restricts
118 * available channels to 1-11 everywhere. Match their
119 * questionable behaviour only for regdomains which we don't
120 * recognise. */
121 dev_warn(zd_mac_dev(mac), "Unrecognised regulatory domain: "
122 "%#04x. Defaulting to FCC.\n", default_regdomain);
123 default_regdomain = ZD_REGDOMAIN_FCC;
Daniel Drakee85d0912006-06-02 17:11:32 +0100124 }
125 spin_lock_irq(&mac->lock);
126 mac->regdomain = mac->default_regdomain = default_regdomain;
127 spin_unlock_irq(&mac->lock);
128 r = reset_channel(mac);
129 if (r)
130 goto disable_int;
131
Daniel Drake40da08b2006-08-01 23:43:32 +0200132 /* We must inform the device that we are doing encryption/decryption in
133 * software at the moment. */
134 r = zd_set_encryption_type(chip, ENC_SNIFFER);
Daniel Drakee85d0912006-06-02 17:11:32 +0100135 if (r)
136 goto disable_int;
137
138 r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
139 if (r)
140 goto disable_int;
141
142 r = 0;
143disable_int:
144 zd_chip_disable_int(chip);
145out:
146 return r;
147}
148
149void zd_mac_clear(struct zd_mac *mac)
150{
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000151 flush_workqueue(zd_workqueue);
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800152 skb_queue_purge(&mac->rx_queue);
153 tasklet_kill(&mac->rx_tasklet);
Daniel Drakee85d0912006-06-02 17:11:32 +0100154 zd_chip_clear(&mac->chip);
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100155 ZD_ASSERT(!spin_is_locked(&mac->lock));
156 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
Daniel Drakee85d0912006-06-02 17:11:32 +0100157}
158
159static int reset_mode(struct zd_mac *mac)
160{
161 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
Daniel Drake44713b12007-03-26 00:59:47 +0100162 u32 filter = (ieee->iw_mode == IW_MODE_MONITOR) ? ~0 : STA_RX_FILTER;
163 return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
Daniel Drakee85d0912006-06-02 17:11:32 +0100164}
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
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800172 tasklet_enable(&mac->rx_tasklet);
173
Daniel Drakee85d0912006-06-02 17:11:32 +0100174 r = zd_chip_enable_int(chip);
175 if (r < 0)
176 goto out;
177
178 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
179 if (r < 0)
180 goto disable_int;
181 r = reset_mode(mac);
182 if (r)
183 goto disable_int;
184 r = zd_chip_switch_radio_on(chip);
185 if (r < 0)
186 goto disable_int;
187 r = zd_chip_set_channel(chip, mac->requested_channel);
188 if (r < 0)
189 goto disable_radio;
190 r = zd_chip_enable_rx(chip);
191 if (r < 0)
192 goto disable_radio;
193 r = zd_chip_enable_hwint(chip);
194 if (r < 0)
195 goto disable_rx;
196
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100197 housekeeping_enable(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100198 ieee80211softmac_start(netdev);
199 return 0;
200disable_rx:
201 zd_chip_disable_rx(chip);
202disable_radio:
203 zd_chip_switch_radio_off(chip);
204disable_int:
205 zd_chip_disable_int(chip);
206out:
207 return r;
208}
209
210int zd_mac_stop(struct net_device *netdev)
211{
212 struct zd_mac *mac = zd_netdev_mac(netdev);
213 struct zd_chip *chip = &mac->chip;
214
Daniel Drakec9a4b352006-06-11 23:18:54 +0100215 netif_stop_queue(netdev);
216
Daniel Drakee85d0912006-06-02 17:11:32 +0100217 /*
218 * The order here deliberately is a little different from the open()
219 * method, since we need to make sure there is no opportunity for RX
220 * frames to be processed by softmac after we have stopped it.
221 */
222
223 zd_chip_disable_rx(chip);
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800224 skb_queue_purge(&mac->rx_queue);
225 tasklet_disable(&mac->rx_tasklet);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100226 housekeeping_disable(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100227 ieee80211softmac_stop(netdev);
228
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000229 /* Ensure no work items are running or queued from this point */
230 cancel_delayed_work(&mac->set_rts_cts_work);
231 cancel_delayed_work(&mac->set_basic_rates_work);
232 flush_workqueue(zd_workqueue);
233 mac->updating_rts_rate = 0;
234 mac->updating_basic_rates = 0;
235
Daniel Drakee85d0912006-06-02 17:11:32 +0100236 zd_chip_disable_hwint(chip);
237 zd_chip_switch_radio_off(chip);
238 zd_chip_disable_int(chip);
239
240 return 0;
241}
242
243int zd_mac_set_mac_address(struct net_device *netdev, void *p)
244{
245 int r;
246 unsigned long flags;
247 struct sockaddr *addr = p;
248 struct zd_mac *mac = zd_netdev_mac(netdev);
249 struct zd_chip *chip = &mac->chip;
250
251 if (!is_valid_ether_addr(addr->sa_data))
252 return -EADDRNOTAVAIL;
253
254 dev_dbg_f(zd_mac_dev(mac),
255 "Setting MAC to " MAC_FMT "\n", MAC_ARG(addr->sa_data));
256
257 r = zd_write_mac_addr(chip, addr->sa_data);
258 if (r)
259 return r;
260
261 spin_lock_irqsave(&mac->lock, flags);
262 memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
263 spin_unlock_irqrestore(&mac->lock, flags);
264
265 return 0;
266}
267
Jeff Garzik0ae85132006-12-07 06:30:30 -0500268static void set_multicast_hash_handler(struct work_struct *work)
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000269{
Jeff Garzik0ae85132006-12-07 06:30:30 -0500270 struct zd_mac *mac = container_of(work, struct zd_mac,
271 set_multicast_hash_work);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000272 struct zd_mc_hash hash;
273
274 spin_lock_irq(&mac->lock);
275 hash = mac->multicast_hash;
276 spin_unlock_irq(&mac->lock);
277
278 zd_chip_set_multicast_hash(&mac->chip, &hash);
279}
280
281void zd_mac_set_multicast_list(struct net_device *dev)
282{
283 struct zd_mc_hash hash;
284 struct zd_mac *mac = zd_netdev_mac(dev);
285 struct dev_mc_list *mc;
286 unsigned long flags;
287
288 if (dev->flags & (IFF_PROMISC|IFF_ALLMULTI)) {
289 zd_mc_add_all(&hash);
290 } else {
291 zd_mc_clear(&hash);
292 for (mc = dev->mc_list; mc; mc = mc->next) {
293 dev_dbg_f(zd_mac_dev(mac), "mc addr " MAC_FMT "\n",
294 MAC_ARG(mc->dmi_addr));
295 zd_mc_add_addr(&hash, mc->dmi_addr);
296 }
297 }
298
299 spin_lock_irqsave(&mac->lock, flags);
300 mac->multicast_hash = hash;
301 spin_unlock_irqrestore(&mac->lock, flags);
302 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
303}
304
Daniel Drakee85d0912006-06-02 17:11:32 +0100305int zd_mac_set_regdomain(struct zd_mac *mac, u8 regdomain)
306{
307 int r;
308 u8 channel;
309
310 ZD_ASSERT(!irqs_disabled());
311 spin_lock_irq(&mac->lock);
312 if (regdomain == 0) {
313 regdomain = mac->default_regdomain;
314 }
315 if (!zd_regdomain_supported(regdomain)) {
316 spin_unlock_irq(&mac->lock);
317 return -EINVAL;
318 }
319 mac->regdomain = regdomain;
320 channel = mac->requested_channel;
321 spin_unlock_irq(&mac->lock);
322
323 r = zd_geo_init(zd_mac_to_ieee80211(mac), regdomain);
324 if (r)
325 return r;
326 if (!zd_regdomain_supports_channel(regdomain, channel)) {
327 r = reset_channel(mac);
328 if (r)
329 return r;
330 }
331
332 return 0;
333}
334
335u8 zd_mac_get_regdomain(struct zd_mac *mac)
336{
337 unsigned long flags;
338 u8 regdomain;
339
340 spin_lock_irqsave(&mac->lock, flags);
341 regdomain = mac->regdomain;
342 spin_unlock_irqrestore(&mac->lock, flags);
343 return regdomain;
344}
345
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000346/* Fallback to lowest rate, if rate is unknown. */
347static u8 rate_to_zd_rate(u8 rate)
348{
349 switch (rate) {
350 case IEEE80211_CCK_RATE_2MB:
351 return ZD_CCK_RATE_2M;
352 case IEEE80211_CCK_RATE_5MB:
353 return ZD_CCK_RATE_5_5M;
354 case IEEE80211_CCK_RATE_11MB:
355 return ZD_CCK_RATE_11M;
356 case IEEE80211_OFDM_RATE_6MB:
357 return ZD_OFDM_RATE_6M;
358 case IEEE80211_OFDM_RATE_9MB:
359 return ZD_OFDM_RATE_9M;
360 case IEEE80211_OFDM_RATE_12MB:
361 return ZD_OFDM_RATE_12M;
362 case IEEE80211_OFDM_RATE_18MB:
363 return ZD_OFDM_RATE_18M;
364 case IEEE80211_OFDM_RATE_24MB:
365 return ZD_OFDM_RATE_24M;
366 case IEEE80211_OFDM_RATE_36MB:
367 return ZD_OFDM_RATE_36M;
368 case IEEE80211_OFDM_RATE_48MB:
369 return ZD_OFDM_RATE_48M;
370 case IEEE80211_OFDM_RATE_54MB:
371 return ZD_OFDM_RATE_54M;
372 }
373 return ZD_CCK_RATE_1M;
374}
375
376static u16 rate_to_cr_rate(u8 rate)
377{
378 switch (rate) {
379 case IEEE80211_CCK_RATE_2MB:
380 return CR_RATE_1M;
381 case IEEE80211_CCK_RATE_5MB:
382 return CR_RATE_5_5M;
383 case IEEE80211_CCK_RATE_11MB:
384 return CR_RATE_11M;
385 case IEEE80211_OFDM_RATE_6MB:
386 return CR_RATE_6M;
387 case IEEE80211_OFDM_RATE_9MB:
388 return CR_RATE_9M;
389 case IEEE80211_OFDM_RATE_12MB:
390 return CR_RATE_12M;
391 case IEEE80211_OFDM_RATE_18MB:
392 return CR_RATE_18M;
393 case IEEE80211_OFDM_RATE_24MB:
394 return CR_RATE_24M;
395 case IEEE80211_OFDM_RATE_36MB:
396 return CR_RATE_36M;
397 case IEEE80211_OFDM_RATE_48MB:
398 return CR_RATE_48M;
399 case IEEE80211_OFDM_RATE_54MB:
400 return CR_RATE_54M;
401 }
402 return CR_RATE_1M;
403}
404
405static void try_enable_tx(struct zd_mac *mac)
406{
407 unsigned long flags;
408
409 spin_lock_irqsave(&mac->lock, flags);
410 if (mac->updating_rts_rate == 0 && mac->updating_basic_rates == 0)
411 netif_wake_queue(mac->netdev);
412 spin_unlock_irqrestore(&mac->lock, flags);
413}
414
David Howells6d5aefb2006-12-05 19:36:26 +0000415static void set_rts_cts_work(struct work_struct *work)
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000416{
David Howells6d5aefb2006-12-05 19:36:26 +0000417 struct zd_mac *mac =
418 container_of(work, struct zd_mac, set_rts_cts_work.work);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000419 unsigned long flags;
420 u8 rts_rate;
421 unsigned int short_preamble;
422
423 mutex_lock(&mac->chip.mutex);
424
425 spin_lock_irqsave(&mac->lock, flags);
426 mac->updating_rts_rate = 0;
427 rts_rate = mac->rts_rate;
428 short_preamble = mac->short_preamble;
429 spin_unlock_irqrestore(&mac->lock, flags);
430
431 zd_chip_set_rts_cts_rate_locked(&mac->chip, rts_rate, short_preamble);
432 mutex_unlock(&mac->chip.mutex);
433
434 try_enable_tx(mac);
435}
436
David Howells6d5aefb2006-12-05 19:36:26 +0000437static void set_basic_rates_work(struct work_struct *work)
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000438{
David Howells6d5aefb2006-12-05 19:36:26 +0000439 struct zd_mac *mac =
440 container_of(work, struct zd_mac, set_basic_rates_work.work);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000441 unsigned long flags;
442 u16 basic_rates;
443
444 mutex_lock(&mac->chip.mutex);
445
446 spin_lock_irqsave(&mac->lock, flags);
447 mac->updating_basic_rates = 0;
448 basic_rates = mac->basic_rates;
449 spin_unlock_irqrestore(&mac->lock, flags);
450
451 zd_chip_set_basic_rates_locked(&mac->chip, basic_rates);
452 mutex_unlock(&mac->chip.mutex);
453
454 try_enable_tx(mac);
455}
456
457static void bssinfo_change(struct net_device *netdev, u32 changes)
458{
459 struct zd_mac *mac = zd_netdev_mac(netdev);
460 struct ieee80211softmac_device *softmac = ieee80211_priv(netdev);
461 struct ieee80211softmac_bss_info *bssinfo = &softmac->bssinfo;
462 int need_set_rts_cts = 0;
463 int need_set_rates = 0;
464 u16 basic_rates;
465 unsigned long flags;
466
467 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
468
469 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_SHORT_PREAMBLE) {
470 spin_lock_irqsave(&mac->lock, flags);
471 mac->short_preamble = bssinfo->short_preamble;
472 spin_unlock_irqrestore(&mac->lock, flags);
473 need_set_rts_cts = 1;
474 }
475
476 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_RATES) {
477 /* Set RTS rate to highest available basic rate */
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800478 u8 hi_rate = ieee80211softmac_highest_supported_rate(softmac,
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000479 &bssinfo->supported_rates, 1);
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800480 hi_rate = rate_to_zd_rate(hi_rate);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000481
482 spin_lock_irqsave(&mac->lock, flags);
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800483 if (hi_rate != mac->rts_rate) {
484 mac->rts_rate = hi_rate;
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000485 need_set_rts_cts = 1;
486 }
487 spin_unlock_irqrestore(&mac->lock, flags);
488
489 /* Set basic rates */
490 need_set_rates = 1;
491 if (bssinfo->supported_rates.count == 0) {
492 /* Allow the device to be flexible */
493 basic_rates = CR_RATES_80211B | CR_RATES_80211G;
494 } else {
495 int i = 0;
496 basic_rates = 0;
497
498 for (i = 0; i < bssinfo->supported_rates.count; i++) {
499 u16 rate = bssinfo->supported_rates.rates[i];
500 if ((rate & IEEE80211_BASIC_RATE_MASK) == 0)
501 continue;
502
503 rate &= ~IEEE80211_BASIC_RATE_MASK;
504 basic_rates |= rate_to_cr_rate(rate);
505 }
506 }
507 spin_lock_irqsave(&mac->lock, flags);
508 mac->basic_rates = basic_rates;
509 spin_unlock_irqrestore(&mac->lock, flags);
510 }
511
512 /* Schedule any changes we made above */
513
514 spin_lock_irqsave(&mac->lock, flags);
515 if (need_set_rts_cts && !mac->updating_rts_rate) {
516 mac->updating_rts_rate = 1;
517 netif_stop_queue(mac->netdev);
David Howells6d5aefb2006-12-05 19:36:26 +0000518 queue_delayed_work(zd_workqueue, &mac->set_rts_cts_work, 0);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000519 }
520 if (need_set_rates && !mac->updating_basic_rates) {
521 mac->updating_basic_rates = 1;
522 netif_stop_queue(mac->netdev);
David Howells6d5aefb2006-12-05 19:36:26 +0000523 queue_delayed_work(zd_workqueue, &mac->set_basic_rates_work,
524 0);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000525 }
526 spin_unlock_irqrestore(&mac->lock, flags);
527}
528
Daniel Drakee85d0912006-06-02 17:11:32 +0100529static void set_channel(struct net_device *netdev, u8 channel)
530{
531 struct zd_mac *mac = zd_netdev_mac(netdev);
532
533 dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
534
535 zd_chip_set_channel(&mac->chip, channel);
536}
537
Daniel Drakee85d0912006-06-02 17:11:32 +0100538int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
539{
540 unsigned long lock_flags;
541 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
542
543 if (ieee->iw_mode == IW_MODE_INFRA)
544 return -EPERM;
545
546 spin_lock_irqsave(&mac->lock, lock_flags);
547 if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
548 spin_unlock_irqrestore(&mac->lock, lock_flags);
549 return -EINVAL;
550 }
551 mac->requested_channel = channel;
552 spin_unlock_irqrestore(&mac->lock, lock_flags);
553 if (netif_running(mac->netdev))
554 return zd_chip_set_channel(&mac->chip, channel);
555 else
556 return 0;
557}
558
Daniel Drake84bc7152006-11-22 00:05:30 +0000559u8 zd_mac_get_channel(struct zd_mac *mac)
Daniel Drakee85d0912006-06-02 17:11:32 +0100560{
Daniel Drake84bc7152006-11-22 00:05:30 +0000561 u8 channel = zd_chip_get_channel(&mac->chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100562
Daniel Drake84bc7152006-11-22 00:05:30 +0000563 dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
564 return channel;
Daniel Drakee85d0912006-06-02 17:11:32 +0100565}
566
567/* If wrong rate is given, we are falling back to the slowest rate: 1MBit/s */
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000568static u8 zd_rate_typed(u8 zd_rate)
Daniel Drakee85d0912006-06-02 17:11:32 +0100569{
570 static const u8 typed_rates[16] = {
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000571 [ZD_CCK_RATE_1M] = ZD_CS_CCK|ZD_CCK_RATE_1M,
572 [ZD_CCK_RATE_2M] = ZD_CS_CCK|ZD_CCK_RATE_2M,
573 [ZD_CCK_RATE_5_5M] = ZD_CS_CCK|ZD_CCK_RATE_5_5M,
574 [ZD_CCK_RATE_11M] = ZD_CS_CCK|ZD_CCK_RATE_11M,
Daniel Drakee85d0912006-06-02 17:11:32 +0100575 [ZD_OFDM_RATE_6M] = ZD_CS_OFDM|ZD_OFDM_RATE_6M,
576 [ZD_OFDM_RATE_9M] = ZD_CS_OFDM|ZD_OFDM_RATE_9M,
577 [ZD_OFDM_RATE_12M] = ZD_CS_OFDM|ZD_OFDM_RATE_12M,
578 [ZD_OFDM_RATE_18M] = ZD_CS_OFDM|ZD_OFDM_RATE_18M,
579 [ZD_OFDM_RATE_24M] = ZD_CS_OFDM|ZD_OFDM_RATE_24M,
580 [ZD_OFDM_RATE_36M] = ZD_CS_OFDM|ZD_OFDM_RATE_36M,
581 [ZD_OFDM_RATE_48M] = ZD_CS_OFDM|ZD_OFDM_RATE_48M,
582 [ZD_OFDM_RATE_54M] = ZD_CS_OFDM|ZD_OFDM_RATE_54M,
583 };
584
585 ZD_ASSERT(ZD_CS_RATE_MASK == 0x0f);
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000586 return typed_rates[zd_rate & ZD_CS_RATE_MASK];
Daniel Drakee85d0912006-06-02 17:11:32 +0100587}
588
589int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
590{
591 struct ieee80211_device *ieee;
592
593 switch (mode) {
594 case IW_MODE_AUTO:
595 case IW_MODE_ADHOC:
596 case IW_MODE_INFRA:
597 mac->netdev->type = ARPHRD_ETHER;
598 break;
599 case IW_MODE_MONITOR:
600 mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
601 break;
602 default:
603 dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
604 return -EINVAL;
605 }
606
607 ieee = zd_mac_to_ieee80211(mac);
608 ZD_ASSERT(!irqs_disabled());
609 spin_lock_irq(&ieee->lock);
610 ieee->iw_mode = mode;
611 spin_unlock_irq(&ieee->lock);
612
613 if (netif_running(mac->netdev))
614 return reset_mode(mac);
615
616 return 0;
617}
618
619int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
620{
621 unsigned long flags;
622 struct ieee80211_device *ieee;
623
624 ieee = zd_mac_to_ieee80211(mac);
625 spin_lock_irqsave(&ieee->lock, flags);
626 *mode = ieee->iw_mode;
627 spin_unlock_irqrestore(&ieee->lock, flags);
628 return 0;
629}
630
631int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
632{
633 int i;
634 const struct channel_range *channel_range;
635 u8 regdomain;
636
637 memset(range, 0, sizeof(*range));
638
639 /* FIXME: Not so important and depends on the mode. For 802.11g
640 * usually this value is used. It seems to be that Bit/s number is
641 * given here.
642 */
643 range->throughput = 27 * 1000 * 1000;
644
645 range->max_qual.qual = 100;
646 range->max_qual.level = 100;
647
648 /* FIXME: Needs still to be tuned. */
649 range->avg_qual.qual = 71;
650 range->avg_qual.level = 80;
651
652 /* FIXME: depends on standard? */
653 range->min_rts = 256;
654 range->max_rts = 2346;
655
656 range->min_frag = MIN_FRAG_THRESHOLD;
657 range->max_frag = MAX_FRAG_THRESHOLD;
658
659 range->max_encoding_tokens = WEP_KEYS;
660 range->num_encoding_sizes = 2;
661 range->encoding_size[0] = 5;
662 range->encoding_size[1] = WEP_KEY_LEN;
663
664 range->we_version_compiled = WIRELESS_EXT;
665 range->we_version_source = 20;
666
Daniel Drakeff9b99b2006-12-01 00:57:11 +0000667 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
668 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
669
Daniel Drakee85d0912006-06-02 17:11:32 +0100670 ZD_ASSERT(!irqs_disabled());
671 spin_lock_irq(&mac->lock);
672 regdomain = mac->regdomain;
673 spin_unlock_irq(&mac->lock);
674 channel_range = zd_channel_range(regdomain);
675
676 range->num_channels = channel_range->end - channel_range->start;
677 range->old_num_channels = range->num_channels;
678 range->num_frequency = range->num_channels;
679 range->old_num_frequency = range->num_frequency;
680
681 for (i = 0; i < range->num_frequency; i++) {
682 struct iw_freq *freq = &range->freq[i];
683 freq->i = channel_range->start + i;
684 zd_channel_to_freq(freq, freq->i);
685 }
686
687 return 0;
688}
689
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000690static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
Daniel Drakee85d0912006-06-02 17:11:32 +0100691{
692 static const u8 rate_divisor[] = {
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000693 [ZD_CCK_RATE_1M] = 1,
694 [ZD_CCK_RATE_2M] = 2,
695 [ZD_CCK_RATE_5_5M] = 11, /* bits must be doubled */
696 [ZD_CCK_RATE_11M] = 11,
Daniel Drakee85d0912006-06-02 17:11:32 +0100697 [ZD_OFDM_RATE_6M] = 6,
698 [ZD_OFDM_RATE_9M] = 9,
699 [ZD_OFDM_RATE_12M] = 12,
700 [ZD_OFDM_RATE_18M] = 18,
701 [ZD_OFDM_RATE_24M] = 24,
702 [ZD_OFDM_RATE_36M] = 36,
703 [ZD_OFDM_RATE_48M] = 48,
704 [ZD_OFDM_RATE_54M] = 54,
705 };
706
707 u32 bits = (u32)tx_length * 8;
708 u32 divisor;
709
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000710 divisor = rate_divisor[zd_rate];
Daniel Drakee85d0912006-06-02 17:11:32 +0100711 if (divisor == 0)
712 return -EINVAL;
713
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000714 switch (zd_rate) {
715 case ZD_CCK_RATE_5_5M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100716 bits = (2*bits) + 10; /* round up to the next integer */
717 break;
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000718 case ZD_CCK_RATE_11M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100719 if (service) {
720 u32 t = bits % 11;
721 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
722 if (0 < t && t <= 3) {
723 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
724 }
725 }
726 bits += 10; /* round up to the next integer */
727 break;
728 }
729
730 return bits/divisor;
731}
732
733enum {
734 R2M_SHORT_PREAMBLE = 0x01,
735 R2M_11A = 0x02,
736};
737
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000738static u8 zd_rate_to_modulation(u8 zd_rate, int flags)
Daniel Drakee85d0912006-06-02 17:11:32 +0100739{
740 u8 modulation;
741
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000742 modulation = zd_rate_typed(zd_rate);
Daniel Drakee85d0912006-06-02 17:11:32 +0100743 if (flags & R2M_SHORT_PREAMBLE) {
744 switch (ZD_CS_RATE(modulation)) {
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000745 case ZD_CCK_RATE_2M:
746 case ZD_CCK_RATE_5_5M:
747 case ZD_CCK_RATE_11M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100748 modulation |= ZD_CS_CCK_PREA_SHORT;
749 return modulation;
750 }
751 }
752 if (flags & R2M_11A) {
753 if (ZD_CS_TYPE(modulation) == ZD_CS_OFDM)
754 modulation |= ZD_CS_OFDM_MODE_11A;
755 }
756 return modulation;
757}
758
759static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
760 struct ieee80211_hdr_4addr *hdr)
761{
762 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
763 u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000764 u8 rate, zd_rate;
Daniel Drakee85d0912006-06-02 17:11:32 +0100765 int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000766 int is_multicast = is_multicast_ether_addr(hdr->addr1);
767 int short_preamble = ieee80211softmac_short_preamble_ok(softmac,
768 is_multicast, is_mgt);
769 int flags = 0;
Daniel Drakee85d0912006-06-02 17:11:32 +0100770
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000771 /* FIXME: 802.11a? */
772 rate = ieee80211softmac_suggest_txrate(softmac, is_multicast, is_mgt);
Daniel Drakee85d0912006-06-02 17:11:32 +0100773
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000774 if (short_preamble)
775 flags |= R2M_SHORT_PREAMBLE;
Daniel Drakee85d0912006-06-02 17:11:32 +0100776
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000777 zd_rate = rate_to_zd_rate(rate);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000778 cs->modulation = zd_rate_to_modulation(zd_rate, flags);
Daniel Drakee85d0912006-06-02 17:11:32 +0100779}
780
781static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
782 struct ieee80211_hdr_4addr *header)
783{
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000784 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
Daniel Drakee85d0912006-06-02 17:11:32 +0100785 unsigned int tx_length = le16_to_cpu(cs->tx_length);
786 u16 fctl = le16_to_cpu(header->frame_ctl);
787 u16 ftype = WLAN_FC_GET_TYPE(fctl);
788 u16 stype = WLAN_FC_GET_STYPE(fctl);
789
790 /*
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000791 * CONTROL TODO:
Daniel Drakee85d0912006-06-02 17:11:32 +0100792 * - if backoff needed, enable bit 0
793 * - if burst (backoff not needed) disable bit 0
Daniel Drakee85d0912006-06-02 17:11:32 +0100794 */
795
796 cs->control = 0;
797
798 /* First fragment */
799 if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
800 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
801
802 /* Multicast */
803 if (is_multicast_ether_addr(header->addr1))
804 cs->control |= ZD_CS_MULTICAST;
805
806 /* PS-POLL */
807 if (stype == IEEE80211_STYPE_PSPOLL)
808 cs->control |= ZD_CS_PS_POLL_FRAME;
809
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000810 /* Unicast data frames over the threshold should have RTS */
Daniel Drakee85d0912006-06-02 17:11:32 +0100811 if (!is_multicast_ether_addr(header->addr1) &&
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000812 ftype != IEEE80211_FTYPE_MGMT &&
813 tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
814 cs->control |= ZD_CS_RTS;
815
816 /* Use CTS-to-self protection if required */
817 if (ZD_CS_TYPE(cs->modulation) == ZD_CS_OFDM &&
818 ieee80211softmac_protection_needed(softmac)) {
819 /* FIXME: avoid sending RTS *and* self-CTS, is that correct? */
820 cs->control &= ~ZD_CS_RTS;
821 cs->control |= ZD_CS_SELF_CTS;
Daniel Drakee85d0912006-06-02 17:11:32 +0100822 }
823
824 /* FIXME: Management frame? */
825}
826
827static int fill_ctrlset(struct zd_mac *mac,
828 struct ieee80211_txb *txb,
829 int frag_num)
830{
831 int r;
832 struct sk_buff *skb = txb->fragments[frag_num];
833 struct ieee80211_hdr_4addr *hdr =
834 (struct ieee80211_hdr_4addr *) skb->data;
835 unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
836 unsigned int next_frag_len;
837 unsigned int packet_length;
838 struct zd_ctrlset *cs = (struct zd_ctrlset *)
839 skb_push(skb, sizeof(struct zd_ctrlset));
840
841 if (frag_num+1 < txb->nr_frags) {
842 next_frag_len = txb->fragments[frag_num+1]->len +
843 IEEE80211_FCS_LEN;
844 } else {
845 next_frag_len = 0;
846 }
847 ZD_ASSERT(frag_len <= 0xffff);
848 ZD_ASSERT(next_frag_len <= 0xffff);
849
850 cs_set_modulation(mac, cs, hdr);
851
852 cs->tx_length = cpu_to_le16(frag_len);
853
854 cs_set_control(mac, cs, hdr);
855
856 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
857 ZD_ASSERT(packet_length <= 0xffff);
858 /* ZD1211B: Computing the length difference this way, gives us
859 * flexibility to compute the packet length.
860 */
861 cs->packet_length = cpu_to_le16(mac->chip.is_zd1211b ?
862 packet_length - frag_len : packet_length);
863
864 /*
865 * CURRENT LENGTH:
866 * - transmit frame length in microseconds
867 * - seems to be derived from frame length
868 * - see Cal_Us_Service() in zdinlinef.h
869 * - if macp->bTxBurstEnable is enabled, then multiply by 4
870 * - bTxBurstEnable is never set in the vendor driver
871 *
872 * SERVICE:
873 * - "for PLCP configuration"
874 * - always 0 except in some situations at 802.11b 11M
875 * - see line 53 of zdinlinef.h
876 */
877 cs->service = 0;
878 r = zd_calc_tx_length_us(&cs->service, ZD_CS_RATE(cs->modulation),
879 le16_to_cpu(cs->tx_length));
880 if (r < 0)
881 return r;
882 cs->current_length = cpu_to_le16(r);
883
884 if (next_frag_len == 0) {
885 cs->next_frame_length = 0;
886 } else {
887 r = zd_calc_tx_length_us(NULL, ZD_CS_RATE(cs->modulation),
888 next_frag_len);
889 if (r < 0)
890 return r;
891 cs->next_frame_length = cpu_to_le16(r);
892 }
893
894 return 0;
895}
896
897static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
898{
899 int i, r;
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000900 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100901
902 for (i = 0; i < txb->nr_frags; i++) {
903 struct sk_buff *skb = txb->fragments[i];
904
905 r = fill_ctrlset(mac, txb, i);
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000906 if (r) {
907 ieee->stats.tx_dropped++;
Daniel Drakee85d0912006-06-02 17:11:32 +0100908 return r;
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000909 }
Daniel Drakee85d0912006-06-02 17:11:32 +0100910 r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000911 if (r) {
912 ieee->stats.tx_dropped++;
Daniel Drakee85d0912006-06-02 17:11:32 +0100913 return r;
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000914 }
Daniel Drakee85d0912006-06-02 17:11:32 +0100915 }
916
917 /* FIXME: shouldn't this be handled by the upper layers? */
918 mac->netdev->trans_start = jiffies;
919
920 ieee80211_txb_free(txb);
921 return 0;
922}
923
924struct zd_rt_hdr {
925 struct ieee80211_radiotap_header rt_hdr;
926 u8 rt_flags;
Ulrich Kunitz99f65f22006-08-01 23:43:30 +0200927 u8 rt_rate;
Daniel Drakee85d0912006-06-02 17:11:32 +0100928 u16 rt_channel;
929 u16 rt_chbitmask;
John W. Linvillea88556a2006-11-28 14:16:37 -0500930} __attribute__((packed));
Daniel Drakee85d0912006-06-02 17:11:32 +0100931
932static void fill_rt_header(void *buffer, struct zd_mac *mac,
933 const struct ieee80211_rx_stats *stats,
934 const struct rx_status *status)
935{
936 struct zd_rt_hdr *hdr = buffer;
937
938 hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
939 hdr->rt_hdr.it_pad = 0;
940 hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
941 hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
942 (1 << IEEE80211_RADIOTAP_CHANNEL) |
943 (1 << IEEE80211_RADIOTAP_RATE));
944
945 hdr->rt_flags = 0;
946 if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
947 hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
948
Ulrich Kunitz99f65f22006-08-01 23:43:30 +0200949 hdr->rt_rate = stats->rate / 5;
950
Daniel Drakee85d0912006-06-02 17:11:32 +0100951 /* FIXME: 802.11a */
952 hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
953 _zd_chip_get_channel(&mac->chip)));
954 hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
955 ((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
956 ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
Daniel Drakee85d0912006-06-02 17:11:32 +0100957}
958
959/* Returns 1 if the data packet is for us and 0 otherwise. */
960static int is_data_packet_for_us(struct ieee80211_device *ieee,
961 struct ieee80211_hdr_4addr *hdr)
962{
963 struct net_device *netdev = ieee->dev;
964 u16 fc = le16_to_cpu(hdr->frame_ctl);
965
966 ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
967
968 switch (ieee->iw_mode) {
969 case IW_MODE_ADHOC:
970 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
Daniel Drake832855d2007-03-11 19:53:40 +0000971 compare_ether_addr(hdr->addr3, ieee->bssid) != 0)
Daniel Drakee85d0912006-06-02 17:11:32 +0100972 return 0;
973 break;
974 case IW_MODE_AUTO:
975 case IW_MODE_INFRA:
976 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
977 IEEE80211_FCTL_FROMDS ||
Daniel Drake832855d2007-03-11 19:53:40 +0000978 compare_ether_addr(hdr->addr2, ieee->bssid) != 0)
Daniel Drakee85d0912006-06-02 17:11:32 +0100979 return 0;
980 break;
981 default:
982 ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
983 return 0;
984 }
985
Daniel Drake832855d2007-03-11 19:53:40 +0000986 return compare_ether_addr(hdr->addr1, netdev->dev_addr) == 0 ||
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000987 (is_multicast_ether_addr(hdr->addr1) &&
Daniel Drake832855d2007-03-11 19:53:40 +0000988 compare_ether_addr(hdr->addr3, netdev->dev_addr) != 0) ||
Daniel Drakee85d0912006-06-02 17:11:32 +0100989 (netdev->flags & IFF_PROMISC);
990}
991
Ulrich Kunitz741fec52006-11-22 00:05:53 +0000992/* Filters received packets. The function returns 1 if the packet should be
993 * forwarded to ieee80211_rx(). If the packet should be ignored the function
994 * returns 0. If an invalid packet is found the function returns -EINVAL.
995 *
996 * The function calls ieee80211_rx_mgt() directly.
Daniel Drakee85d0912006-06-02 17:11:32 +0100997 *
998 * It has been based on ieee80211_rx_any.
999 */
1000static int filter_rx(struct ieee80211_device *ieee,
1001 const u8 *buffer, unsigned int length,
1002 struct ieee80211_rx_stats *stats)
1003{
1004 struct ieee80211_hdr_4addr *hdr;
1005 u16 fc;
1006
1007 if (ieee->iw_mode == IW_MODE_MONITOR)
1008 return 1;
1009
1010 hdr = (struct ieee80211_hdr_4addr *)buffer;
1011 fc = le16_to_cpu(hdr->frame_ctl);
1012 if ((fc & IEEE80211_FCTL_VERS) != 0)
1013 return -EINVAL;
1014
1015 switch (WLAN_FC_GET_TYPE(fc)) {
1016 case IEEE80211_FTYPE_MGMT:
1017 if (length < sizeof(struct ieee80211_hdr_3addr))
1018 return -EINVAL;
1019 ieee80211_rx_mgt(ieee, hdr, stats);
1020 return 0;
1021 case IEEE80211_FTYPE_CTL:
Daniel Drakee85d0912006-06-02 17:11:32 +01001022 return 0;
1023 case IEEE80211_FTYPE_DATA:
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001024 /* Ignore invalid short buffers */
Daniel Drakee85d0912006-06-02 17:11:32 +01001025 if (length < sizeof(struct ieee80211_hdr_3addr))
1026 return -EINVAL;
1027 return is_data_packet_for_us(ieee, hdr);
1028 }
1029
1030 return -EINVAL;
1031}
1032
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001033static void update_qual_rssi(struct zd_mac *mac,
1034 const u8 *buffer, unsigned int length,
1035 u8 qual_percent, u8 rssi_percent)
Daniel Drakee85d0912006-06-02 17:11:32 +01001036{
1037 unsigned long flags;
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001038 struct ieee80211_hdr_3addr *hdr;
1039 int i;
1040
1041 hdr = (struct ieee80211_hdr_3addr *)buffer;
1042 if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
1043 return;
Daniel Drake832855d2007-03-11 19:53:40 +00001044 if (compare_ether_addr(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid) != 0)
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001045 return;
Daniel Drakee85d0912006-06-02 17:11:32 +01001046
1047 spin_lock_irqsave(&mac->lock, flags);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001048 i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
1049 mac->qual_buffer[i] = qual_percent;
1050 mac->rssi_buffer[i] = rssi_percent;
1051 mac->stats_count++;
Daniel Drakee85d0912006-06-02 17:11:32 +01001052 spin_unlock_irqrestore(&mac->lock, flags);
1053}
1054
1055static int fill_rx_stats(struct ieee80211_rx_stats *stats,
1056 const struct rx_status **pstatus,
1057 struct zd_mac *mac,
1058 const u8 *buffer, unsigned int length)
1059{
1060 const struct rx_status *status;
1061
1062 *pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
1063 if (status->frame_status & ZD_RX_ERROR) {
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001064 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1065 ieee->stats.rx_errors++;
1066 if (status->frame_status & ZD_RX_TIMEOUT_ERROR)
1067 ieee->stats.rx_missed_errors++;
1068 else if (status->frame_status & ZD_RX_FIFO_OVERRUN_ERROR)
1069 ieee->stats.rx_fifo_errors++;
1070 else if (status->frame_status & ZD_RX_DECRYPTION_ERROR)
1071 ieee->ieee_stats.rx_discards_undecryptable++;
1072 else if (status->frame_status & ZD_RX_CRC32_ERROR) {
1073 ieee->stats.rx_crc_errors++;
1074 ieee->ieee_stats.rx_fcs_errors++;
1075 }
1076 else if (status->frame_status & ZD_RX_CRC16_ERROR)
1077 ieee->stats.rx_crc_errors++;
Daniel Drakee85d0912006-06-02 17:11:32 +01001078 return -EINVAL;
1079 }
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001080
Daniel Drakee85d0912006-06-02 17:11:32 +01001081 memset(stats, 0, sizeof(struct ieee80211_rx_stats));
1082 stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
1083 + sizeof(struct rx_status));
1084 /* FIXME: 802.11a */
1085 stats->freq = IEEE80211_24GHZ_BAND;
1086 stats->received_channel = _zd_chip_get_channel(&mac->chip);
1087 stats->rssi = zd_rx_strength_percent(status->signal_strength);
1088 stats->signal = zd_rx_qual_percent(buffer,
1089 length - sizeof(struct rx_status),
1090 status);
1091 stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
1092 stats->rate = zd_rx_rate(buffer, status);
1093 if (stats->rate)
1094 stats->mask |= IEEE80211_STATMASK_RATE;
1095
Daniel Drakee85d0912006-06-02 17:11:32 +01001096 return 0;
1097}
1098
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001099static void zd_mac_rx(struct zd_mac *mac, struct sk_buff *skb)
Daniel Drakee85d0912006-06-02 17:11:32 +01001100{
1101 int r;
1102 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1103 struct ieee80211_rx_stats stats;
1104 const struct rx_status *status;
Daniel Drakee85d0912006-06-02 17:11:32 +01001105
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001106 if (skb->len < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
1107 IEEE80211_FCS_LEN + sizeof(struct rx_status))
1108 {
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001109 ieee->stats.rx_errors++;
1110 ieee->stats.rx_length_errors++;
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001111 goto free_skb;
1112 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001113
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001114 r = fill_rx_stats(&stats, &status, mac, skb->data, skb->len);
1115 if (r) {
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001116 /* Only packets with rx errors are included here.
1117 * The error stats have already been set in fill_rx_stats.
1118 */
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001119 goto free_skb;
1120 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001121
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001122 __skb_pull(skb, ZD_PLCP_HEADER_SIZE);
1123 __skb_trim(skb, skb->len -
1124 (IEEE80211_FCS_LEN + sizeof(struct rx_status)));
Daniel Drakee85d0912006-06-02 17:11:32 +01001125
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001126 update_qual_rssi(mac, skb->data, skb->len, stats.signal,
1127 status->signal_strength);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001128
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001129 r = filter_rx(ieee, skb->data, skb->len, &stats);
1130 if (r <= 0) {
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001131 if (r < 0) {
1132 ieee->stats.rx_errors++;
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001133 dev_dbg_f(zd_mac_dev(mac), "Error in packet.\n");
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001134 }
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001135 goto free_skb;
1136 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001137
Daniel Drakee85d0912006-06-02 17:11:32 +01001138 if (ieee->iw_mode == IW_MODE_MONITOR)
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001139 fill_rt_header(skb_push(skb, sizeof(struct zd_rt_hdr)), mac,
Daniel Drakee85d0912006-06-02 17:11:32 +01001140 &stats, status);
Daniel Drakee85d0912006-06-02 17:11:32 +01001141
1142 r = ieee80211_rx(ieee, skb, &stats);
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001143 if (r)
1144 return;
1145free_skb:
1146 /* We are always in a soft irq. */
1147 dev_kfree_skb(skb);
1148}
1149
1150static void do_rx(unsigned long mac_ptr)
1151{
1152 struct zd_mac *mac = (struct zd_mac *)mac_ptr;
1153 struct sk_buff *skb;
1154
1155 while ((skb = skb_dequeue(&mac->rx_queue)) != NULL)
1156 zd_mac_rx(mac, skb);
1157}
1158
1159int zd_mac_rx_irq(struct zd_mac *mac, const u8 *buffer, unsigned int length)
1160{
1161 struct sk_buff *skb;
1162
1163 skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
1164 if (!skb) {
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001165 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001166 dev_warn(zd_mac_dev(mac), "Could not allocate skb.\n");
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001167 ieee->stats.rx_dropped++;
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001168 return -ENOMEM;
1169 }
1170 skb_reserve(skb, sizeof(struct zd_rt_hdr));
1171 memcpy(__skb_put(skb, length), buffer, length);
1172 skb_queue_tail(&mac->rx_queue, skb);
1173 tasklet_schedule(&mac->rx_tasklet);
Daniel Drakee85d0912006-06-02 17:11:32 +01001174 return 0;
1175}
1176
1177static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
1178 int pri)
1179{
1180 return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
1181}
1182
1183static void set_security(struct net_device *netdev,
1184 struct ieee80211_security *sec)
1185{
1186 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
1187 struct ieee80211_security *secinfo = &ieee->sec;
1188 int keyidx;
1189
1190 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
1191
1192 for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
1193 if (sec->flags & (1<<keyidx)) {
1194 secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
1195 secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
1196 memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
1197 SCM_KEY_LEN);
1198 }
1199
1200 if (sec->flags & SEC_ACTIVE_KEY) {
1201 secinfo->active_key = sec->active_key;
1202 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1203 " .active_key = %d\n", sec->active_key);
1204 }
1205 if (sec->flags & SEC_UNICAST_GROUP) {
1206 secinfo->unicast_uses_group = sec->unicast_uses_group;
1207 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1208 " .unicast_uses_group = %d\n",
1209 sec->unicast_uses_group);
1210 }
1211 if (sec->flags & SEC_LEVEL) {
1212 secinfo->level = sec->level;
1213 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1214 " .level = %d\n", sec->level);
1215 }
1216 if (sec->flags & SEC_ENABLED) {
1217 secinfo->enabled = sec->enabled;
1218 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1219 " .enabled = %d\n", sec->enabled);
1220 }
1221 if (sec->flags & SEC_ENCRYPT) {
1222 secinfo->encrypt = sec->encrypt;
1223 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1224 " .encrypt = %d\n", sec->encrypt);
1225 }
1226 if (sec->flags & SEC_AUTH_MODE) {
1227 secinfo->auth_mode = sec->auth_mode;
1228 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1229 " .auth_mode = %d\n", sec->auth_mode);
1230 }
1231}
1232
1233static void ieee_init(struct ieee80211_device *ieee)
1234{
1235 ieee->mode = IEEE_B | IEEE_G;
1236 ieee->freq_band = IEEE80211_24GHZ_BAND;
1237 ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
1238 ieee->tx_headroom = sizeof(struct zd_ctrlset);
1239 ieee->set_security = set_security;
1240 ieee->hard_start_xmit = netdev_tx;
1241
1242 /* Software encryption/decryption for now */
1243 ieee->host_build_iv = 0;
1244 ieee->host_encrypt = 1;
1245 ieee->host_decrypt = 1;
1246
1247 /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
1248 * correctly support AUTO */
1249 ieee->iw_mode = IW_MODE_INFRA;
1250}
1251
1252static void softmac_init(struct ieee80211softmac_device *sm)
1253{
1254 sm->set_channel = set_channel;
Daniel Drakeb1382ed2006-11-22 00:06:48 +00001255 sm->bssinfo_change = bssinfo_change;
Daniel Drakee85d0912006-06-02 17:11:32 +01001256}
1257
1258struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
1259{
1260 struct zd_mac *mac = zd_netdev_mac(ndev);
1261 struct iw_statistics *iw_stats = &mac->iw_stats;
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001262 unsigned int i, count, qual_total, rssi_total;
Daniel Drakee85d0912006-06-02 17:11:32 +01001263
1264 memset(iw_stats, 0, sizeof(struct iw_statistics));
1265 /* We are not setting the status, because ieee->state is not updated
1266 * at all and this driver doesn't track authentication state.
1267 */
1268 spin_lock_irq(&mac->lock);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001269 count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
1270 mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
1271 qual_total = rssi_total = 0;
1272 for (i = 0; i < count; i++) {
1273 qual_total += mac->qual_buffer[i];
1274 rssi_total += mac->rssi_buffer[i];
1275 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001276 spin_unlock_irq(&mac->lock);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001277 iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
1278 if (count > 0) {
1279 iw_stats->qual.qual = qual_total / count;
1280 iw_stats->qual.level = rssi_total / count;
1281 iw_stats->qual.updated |=
1282 IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
1283 } else {
1284 iw_stats->qual.updated |=
1285 IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
1286 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001287 /* TODO: update counter */
1288 return iw_stats;
1289}
1290
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001291#define LINK_LED_WORK_DELAY HZ
1292
David Howellsc4028952006-11-22 14:57:56 +00001293static void link_led_handler(struct work_struct *work)
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001294{
David Howellsc4028952006-11-22 14:57:56 +00001295 struct zd_mac *mac =
1296 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001297 struct zd_chip *chip = &mac->chip;
1298 struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
1299 int is_associated;
1300 int r;
1301
1302 spin_lock_irq(&mac->lock);
John W. Linville41072a12006-10-17 13:47:40 -04001303 is_associated = sm->associnfo.associated != 0;
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001304 spin_unlock_irq(&mac->lock);
1305
1306 r = zd_chip_control_leds(chip,
1307 is_associated ? LED_ASSOCIATED : LED_SCANNING);
1308 if (r)
1309 dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1310
1311 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1312 LINK_LED_WORK_DELAY);
1313}
1314
1315static void housekeeping_init(struct zd_mac *mac)
1316{
David Howellsc4028952006-11-22 14:57:56 +00001317 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001318}
1319
1320static void housekeeping_enable(struct zd_mac *mac)
1321{
1322 dev_dbg_f(zd_mac_dev(mac), "\n");
1323 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1324 0);
1325}
1326
1327static void housekeeping_disable(struct zd_mac *mac)
1328{
1329 dev_dbg_f(zd_mac_dev(mac), "\n");
1330 cancel_rearming_delayed_workqueue(zd_workqueue,
1331 &mac->housekeeping.link_led_work);
1332 zd_chip_control_leds(&mac->chip, LED_OFF);
1333}