blob: 00ca704ece35b65f0951404d5d1f037869d9921b [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
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;
David Howells6d5aefb2006-12-05 19:36:26 +000053 INIT_DELAYED_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
54 INIT_DELAYED_WORK(&mac->set_basic_rates_work, set_basic_rates_work);
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 Kunitz583afd1e2006-09-13 02:42:38 +010059 housekeeping_init(mac);
Jeff Garzik0ae85132006-12-07 06:30:30 -050060 INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
Daniel Drakee85d0912006-06-02 17:11:32 +010061 return 0;
62}
63
64static int reset_channel(struct zd_mac *mac)
65{
66 int r;
67 unsigned long flags;
68 const struct channel_range *range;
69
70 spin_lock_irqsave(&mac->lock, flags);
71 range = zd_channel_range(mac->regdomain);
72 if (!range->start) {
73 r = -EINVAL;
74 goto out;
75 }
76 mac->requested_channel = range->start;
77 r = 0;
78out:
79 spin_unlock_irqrestore(&mac->lock, flags);
80 return r;
81}
82
83int zd_mac_init_hw(struct zd_mac *mac, u8 device_type)
84{
85 int r;
86 struct zd_chip *chip = &mac->chip;
87 u8 addr[ETH_ALEN];
88 u8 default_regdomain;
89
90 r = zd_chip_enable_int(chip);
91 if (r)
92 goto out;
93 r = zd_chip_init_hw(chip, device_type);
94 if (r)
95 goto disable_int;
96
97 zd_get_e2p_mac_addr(chip, addr);
98 r = zd_write_mac_addr(chip, addr);
99 if (r)
100 goto disable_int;
101 ZD_ASSERT(!irqs_disabled());
102 spin_lock_irq(&mac->lock);
103 memcpy(mac->netdev->dev_addr, addr, ETH_ALEN);
104 spin_unlock_irq(&mac->lock);
105
106 r = zd_read_regdomain(chip, &default_regdomain);
107 if (r)
108 goto disable_int;
109 if (!zd_regdomain_supported(default_regdomain)) {
110 dev_dbg_f(zd_mac_dev(mac),
111 "Regulatory Domain %#04x is not supported.\n",
112 default_regdomain);
113 r = -EINVAL;
114 goto disable_int;
115 }
116 spin_lock_irq(&mac->lock);
117 mac->regdomain = mac->default_regdomain = default_regdomain;
118 spin_unlock_irq(&mac->lock);
119 r = reset_channel(mac);
120 if (r)
121 goto disable_int;
122
Daniel Drake40da08b2006-08-01 23:43:32 +0200123 /* We must inform the device that we are doing encryption/decryption in
124 * software at the moment. */
125 r = zd_set_encryption_type(chip, ENC_SNIFFER);
Daniel Drakee85d0912006-06-02 17:11:32 +0100126 if (r)
127 goto disable_int;
128
129 r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
130 if (r)
131 goto disable_int;
132
133 r = 0;
134disable_int:
135 zd_chip_disable_int(chip);
136out:
137 return r;
138}
139
140void zd_mac_clear(struct zd_mac *mac)
141{
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000142 flush_workqueue(zd_workqueue);
Daniel Drakee85d0912006-06-02 17:11:32 +0100143 zd_chip_clear(&mac->chip);
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100144 ZD_ASSERT(!spin_is_locked(&mac->lock));
145 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
Daniel Drakee85d0912006-06-02 17:11:32 +0100146}
147
148static int reset_mode(struct zd_mac *mac)
149{
150 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
151 struct zd_ioreq32 ioreqs[3] = {
Ulrich Kunitzfde627b2006-08-01 23:43:35 +0200152 { CR_RX_FILTER, STA_RX_FILTER },
Daniel Drakee85d0912006-06-02 17:11:32 +0100153 { CR_SNIFFER_ON, 0U },
Daniel Drakee85d0912006-06-02 17:11:32 +0100154 };
155
156 if (ieee->iw_mode == IW_MODE_MONITOR) {
157 ioreqs[0].value = 0xffffffff;
158 ioreqs[1].value = 0x1;
159 ioreqs[2].value = ENC_SNIFFER;
160 }
161
162 return zd_iowrite32a(&mac->chip, ioreqs, 3);
163}
164
165int zd_mac_open(struct net_device *netdev)
166{
167 struct zd_mac *mac = zd_netdev_mac(netdev);
168 struct zd_chip *chip = &mac->chip;
169 int r;
170
171 r = zd_chip_enable_int(chip);
172 if (r < 0)
173 goto out;
174
175 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
176 if (r < 0)
177 goto disable_int;
178 r = reset_mode(mac);
179 if (r)
180 goto disable_int;
181 r = zd_chip_switch_radio_on(chip);
182 if (r < 0)
183 goto disable_int;
184 r = zd_chip_set_channel(chip, mac->requested_channel);
185 if (r < 0)
186 goto disable_radio;
187 r = zd_chip_enable_rx(chip);
188 if (r < 0)
189 goto disable_radio;
190 r = zd_chip_enable_hwint(chip);
191 if (r < 0)
192 goto disable_rx;
193
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100194 housekeeping_enable(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100195 ieee80211softmac_start(netdev);
196 return 0;
197disable_rx:
198 zd_chip_disable_rx(chip);
199disable_radio:
200 zd_chip_switch_radio_off(chip);
201disable_int:
202 zd_chip_disable_int(chip);
203out:
204 return r;
205}
206
207int zd_mac_stop(struct net_device *netdev)
208{
209 struct zd_mac *mac = zd_netdev_mac(netdev);
210 struct zd_chip *chip = &mac->chip;
211
Daniel Drakec9a4b352006-06-11 23:18:54 +0100212 netif_stop_queue(netdev);
213
Daniel Drakee85d0912006-06-02 17:11:32 +0100214 /*
215 * The order here deliberately is a little different from the open()
216 * method, since we need to make sure there is no opportunity for RX
217 * frames to be processed by softmac after we have stopped it.
218 */
219
220 zd_chip_disable_rx(chip);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100221 housekeeping_disable(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100222 ieee80211softmac_stop(netdev);
223
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000224 /* Ensure no work items are running or queued from this point */
225 cancel_delayed_work(&mac->set_rts_cts_work);
226 cancel_delayed_work(&mac->set_basic_rates_work);
227 flush_workqueue(zd_workqueue);
228 mac->updating_rts_rate = 0;
229 mac->updating_basic_rates = 0;
230
Daniel Drakee85d0912006-06-02 17:11:32 +0100231 zd_chip_disable_hwint(chip);
232 zd_chip_switch_radio_off(chip);
233 zd_chip_disable_int(chip);
234
235 return 0;
236}
237
238int zd_mac_set_mac_address(struct net_device *netdev, void *p)
239{
240 int r;
241 unsigned long flags;
242 struct sockaddr *addr = p;
243 struct zd_mac *mac = zd_netdev_mac(netdev);
244 struct zd_chip *chip = &mac->chip;
245
246 if (!is_valid_ether_addr(addr->sa_data))
247 return -EADDRNOTAVAIL;
248
249 dev_dbg_f(zd_mac_dev(mac),
250 "Setting MAC to " MAC_FMT "\n", MAC_ARG(addr->sa_data));
251
252 r = zd_write_mac_addr(chip, addr->sa_data);
253 if (r)
254 return r;
255
256 spin_lock_irqsave(&mac->lock, flags);
257 memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
258 spin_unlock_irqrestore(&mac->lock, flags);
259
260 return 0;
261}
262
Jeff Garzik0ae85132006-12-07 06:30:30 -0500263static void set_multicast_hash_handler(struct work_struct *work)
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000264{
Jeff Garzik0ae85132006-12-07 06:30:30 -0500265 struct zd_mac *mac = container_of(work, struct zd_mac,
266 set_multicast_hash_work);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000267 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
David Howells6d5aefb2006-12-05 19:36:26 +0000410static void set_rts_cts_work(struct work_struct *work)
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000411{
David Howells6d5aefb2006-12-05 19:36:26 +0000412 struct zd_mac *mac =
413 container_of(work, struct zd_mac, set_rts_cts_work.work);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000414 unsigned long flags;
415 u8 rts_rate;
416 unsigned int short_preamble;
417
418 mutex_lock(&mac->chip.mutex);
419
420 spin_lock_irqsave(&mac->lock, flags);
421 mac->updating_rts_rate = 0;
422 rts_rate = mac->rts_rate;
423 short_preamble = mac->short_preamble;
424 spin_unlock_irqrestore(&mac->lock, flags);
425
426 zd_chip_set_rts_cts_rate_locked(&mac->chip, rts_rate, short_preamble);
427 mutex_unlock(&mac->chip.mutex);
428
429 try_enable_tx(mac);
430}
431
David Howells6d5aefb2006-12-05 19:36:26 +0000432static void set_basic_rates_work(struct work_struct *work)
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000433{
David Howells6d5aefb2006-12-05 19:36:26 +0000434 struct zd_mac *mac =
435 container_of(work, struct zd_mac, set_basic_rates_work.work);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000436 unsigned long flags;
437 u16 basic_rates;
438
439 mutex_lock(&mac->chip.mutex);
440
441 spin_lock_irqsave(&mac->lock, flags);
442 mac->updating_basic_rates = 0;
443 basic_rates = mac->basic_rates;
444 spin_unlock_irqrestore(&mac->lock, flags);
445
446 zd_chip_set_basic_rates_locked(&mac->chip, basic_rates);
447 mutex_unlock(&mac->chip.mutex);
448
449 try_enable_tx(mac);
450}
451
452static void bssinfo_change(struct net_device *netdev, u32 changes)
453{
454 struct zd_mac *mac = zd_netdev_mac(netdev);
455 struct ieee80211softmac_device *softmac = ieee80211_priv(netdev);
456 struct ieee80211softmac_bss_info *bssinfo = &softmac->bssinfo;
457 int need_set_rts_cts = 0;
458 int need_set_rates = 0;
459 u16 basic_rates;
460 unsigned long flags;
461
462 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
463
464 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_SHORT_PREAMBLE) {
465 spin_lock_irqsave(&mac->lock, flags);
466 mac->short_preamble = bssinfo->short_preamble;
467 spin_unlock_irqrestore(&mac->lock, flags);
468 need_set_rts_cts = 1;
469 }
470
471 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_RATES) {
472 /* Set RTS rate to highest available basic rate */
473 u8 rate = ieee80211softmac_highest_supported_rate(softmac,
474 &bssinfo->supported_rates, 1);
475 rate = rate_to_zd_rate(rate);
476
477 spin_lock_irqsave(&mac->lock, flags);
478 if (rate != mac->rts_rate) {
479 mac->rts_rate = rate;
480 need_set_rts_cts = 1;
481 }
482 spin_unlock_irqrestore(&mac->lock, flags);
483
484 /* Set basic rates */
485 need_set_rates = 1;
486 if (bssinfo->supported_rates.count == 0) {
487 /* Allow the device to be flexible */
488 basic_rates = CR_RATES_80211B | CR_RATES_80211G;
489 } else {
490 int i = 0;
491 basic_rates = 0;
492
493 for (i = 0; i < bssinfo->supported_rates.count; i++) {
494 u16 rate = bssinfo->supported_rates.rates[i];
495 if ((rate & IEEE80211_BASIC_RATE_MASK) == 0)
496 continue;
497
498 rate &= ~IEEE80211_BASIC_RATE_MASK;
499 basic_rates |= rate_to_cr_rate(rate);
500 }
501 }
502 spin_lock_irqsave(&mac->lock, flags);
503 mac->basic_rates = basic_rates;
504 spin_unlock_irqrestore(&mac->lock, flags);
505 }
506
507 /* Schedule any changes we made above */
508
509 spin_lock_irqsave(&mac->lock, flags);
510 if (need_set_rts_cts && !mac->updating_rts_rate) {
511 mac->updating_rts_rate = 1;
512 netif_stop_queue(mac->netdev);
David Howells6d5aefb2006-12-05 19:36:26 +0000513 queue_delayed_work(zd_workqueue, &mac->set_rts_cts_work, 0);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000514 }
515 if (need_set_rates && !mac->updating_basic_rates) {
516 mac->updating_basic_rates = 1;
517 netif_stop_queue(mac->netdev);
David Howells6d5aefb2006-12-05 19:36:26 +0000518 queue_delayed_work(zd_workqueue, &mac->set_basic_rates_work,
519 0);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000520 }
521 spin_unlock_irqrestore(&mac->lock, flags);
522}
523
Daniel Drakee85d0912006-06-02 17:11:32 +0100524static void set_channel(struct net_device *netdev, u8 channel)
525{
526 struct zd_mac *mac = zd_netdev_mac(netdev);
527
528 dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
529
530 zd_chip_set_channel(&mac->chip, channel);
531}
532
Daniel Drakee85d0912006-06-02 17:11:32 +0100533int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
534{
535 unsigned long lock_flags;
536 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
537
538 if (ieee->iw_mode == IW_MODE_INFRA)
539 return -EPERM;
540
541 spin_lock_irqsave(&mac->lock, lock_flags);
542 if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
543 spin_unlock_irqrestore(&mac->lock, lock_flags);
544 return -EINVAL;
545 }
546 mac->requested_channel = channel;
547 spin_unlock_irqrestore(&mac->lock, lock_flags);
548 if (netif_running(mac->netdev))
549 return zd_chip_set_channel(&mac->chip, channel);
550 else
551 return 0;
552}
553
Daniel Drake84bc7152006-11-22 00:05:30 +0000554u8 zd_mac_get_channel(struct zd_mac *mac)
Daniel Drakee85d0912006-06-02 17:11:32 +0100555{
Daniel Drake84bc7152006-11-22 00:05:30 +0000556 u8 channel = zd_chip_get_channel(&mac->chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100557
Daniel Drake84bc7152006-11-22 00:05:30 +0000558 dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
559 return channel;
Daniel Drakee85d0912006-06-02 17:11:32 +0100560}
561
562/* If wrong rate is given, we are falling back to the slowest rate: 1MBit/s */
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000563static u8 zd_rate_typed(u8 zd_rate)
Daniel Drakee85d0912006-06-02 17:11:32 +0100564{
565 static const u8 typed_rates[16] = {
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000566 [ZD_CCK_RATE_1M] = ZD_CS_CCK|ZD_CCK_RATE_1M,
567 [ZD_CCK_RATE_2M] = ZD_CS_CCK|ZD_CCK_RATE_2M,
568 [ZD_CCK_RATE_5_5M] = ZD_CS_CCK|ZD_CCK_RATE_5_5M,
569 [ZD_CCK_RATE_11M] = ZD_CS_CCK|ZD_CCK_RATE_11M,
Daniel Drakee85d0912006-06-02 17:11:32 +0100570 [ZD_OFDM_RATE_6M] = ZD_CS_OFDM|ZD_OFDM_RATE_6M,
571 [ZD_OFDM_RATE_9M] = ZD_CS_OFDM|ZD_OFDM_RATE_9M,
572 [ZD_OFDM_RATE_12M] = ZD_CS_OFDM|ZD_OFDM_RATE_12M,
573 [ZD_OFDM_RATE_18M] = ZD_CS_OFDM|ZD_OFDM_RATE_18M,
574 [ZD_OFDM_RATE_24M] = ZD_CS_OFDM|ZD_OFDM_RATE_24M,
575 [ZD_OFDM_RATE_36M] = ZD_CS_OFDM|ZD_OFDM_RATE_36M,
576 [ZD_OFDM_RATE_48M] = ZD_CS_OFDM|ZD_OFDM_RATE_48M,
577 [ZD_OFDM_RATE_54M] = ZD_CS_OFDM|ZD_OFDM_RATE_54M,
578 };
579
580 ZD_ASSERT(ZD_CS_RATE_MASK == 0x0f);
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000581 return typed_rates[zd_rate & ZD_CS_RATE_MASK];
Daniel Drakee85d0912006-06-02 17:11:32 +0100582}
583
584int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
585{
586 struct ieee80211_device *ieee;
587
588 switch (mode) {
589 case IW_MODE_AUTO:
590 case IW_MODE_ADHOC:
591 case IW_MODE_INFRA:
592 mac->netdev->type = ARPHRD_ETHER;
593 break;
594 case IW_MODE_MONITOR:
595 mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
596 break;
597 default:
598 dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
599 return -EINVAL;
600 }
601
602 ieee = zd_mac_to_ieee80211(mac);
603 ZD_ASSERT(!irqs_disabled());
604 spin_lock_irq(&ieee->lock);
605 ieee->iw_mode = mode;
606 spin_unlock_irq(&ieee->lock);
607
608 if (netif_running(mac->netdev))
609 return reset_mode(mac);
610
611 return 0;
612}
613
614int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
615{
616 unsigned long flags;
617 struct ieee80211_device *ieee;
618
619 ieee = zd_mac_to_ieee80211(mac);
620 spin_lock_irqsave(&ieee->lock, flags);
621 *mode = ieee->iw_mode;
622 spin_unlock_irqrestore(&ieee->lock, flags);
623 return 0;
624}
625
626int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
627{
628 int i;
629 const struct channel_range *channel_range;
630 u8 regdomain;
631
632 memset(range, 0, sizeof(*range));
633
634 /* FIXME: Not so important and depends on the mode. For 802.11g
635 * usually this value is used. It seems to be that Bit/s number is
636 * given here.
637 */
638 range->throughput = 27 * 1000 * 1000;
639
640 range->max_qual.qual = 100;
641 range->max_qual.level = 100;
642
643 /* FIXME: Needs still to be tuned. */
644 range->avg_qual.qual = 71;
645 range->avg_qual.level = 80;
646
647 /* FIXME: depends on standard? */
648 range->min_rts = 256;
649 range->max_rts = 2346;
650
651 range->min_frag = MIN_FRAG_THRESHOLD;
652 range->max_frag = MAX_FRAG_THRESHOLD;
653
654 range->max_encoding_tokens = WEP_KEYS;
655 range->num_encoding_sizes = 2;
656 range->encoding_size[0] = 5;
657 range->encoding_size[1] = WEP_KEY_LEN;
658
659 range->we_version_compiled = WIRELESS_EXT;
660 range->we_version_source = 20;
661
Daniel Drakeff9b99b2006-12-01 00:57:11 +0000662 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
663 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
664
Daniel Drakee85d0912006-06-02 17:11:32 +0100665 ZD_ASSERT(!irqs_disabled());
666 spin_lock_irq(&mac->lock);
667 regdomain = mac->regdomain;
668 spin_unlock_irq(&mac->lock);
669 channel_range = zd_channel_range(regdomain);
670
671 range->num_channels = channel_range->end - channel_range->start;
672 range->old_num_channels = range->num_channels;
673 range->num_frequency = range->num_channels;
674 range->old_num_frequency = range->num_frequency;
675
676 for (i = 0; i < range->num_frequency; i++) {
677 struct iw_freq *freq = &range->freq[i];
678 freq->i = channel_range->start + i;
679 zd_channel_to_freq(freq, freq->i);
680 }
681
682 return 0;
683}
684
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000685static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
Daniel Drakee85d0912006-06-02 17:11:32 +0100686{
687 static const u8 rate_divisor[] = {
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000688 [ZD_CCK_RATE_1M] = 1,
689 [ZD_CCK_RATE_2M] = 2,
690 [ZD_CCK_RATE_5_5M] = 11, /* bits must be doubled */
691 [ZD_CCK_RATE_11M] = 11,
Daniel Drakee85d0912006-06-02 17:11:32 +0100692 [ZD_OFDM_RATE_6M] = 6,
693 [ZD_OFDM_RATE_9M] = 9,
694 [ZD_OFDM_RATE_12M] = 12,
695 [ZD_OFDM_RATE_18M] = 18,
696 [ZD_OFDM_RATE_24M] = 24,
697 [ZD_OFDM_RATE_36M] = 36,
698 [ZD_OFDM_RATE_48M] = 48,
699 [ZD_OFDM_RATE_54M] = 54,
700 };
701
702 u32 bits = (u32)tx_length * 8;
703 u32 divisor;
704
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000705 divisor = rate_divisor[zd_rate];
Daniel Drakee85d0912006-06-02 17:11:32 +0100706 if (divisor == 0)
707 return -EINVAL;
708
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000709 switch (zd_rate) {
710 case ZD_CCK_RATE_5_5M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100711 bits = (2*bits) + 10; /* round up to the next integer */
712 break;
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000713 case ZD_CCK_RATE_11M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100714 if (service) {
715 u32 t = bits % 11;
716 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
717 if (0 < t && t <= 3) {
718 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
719 }
720 }
721 bits += 10; /* round up to the next integer */
722 break;
723 }
724
725 return bits/divisor;
726}
727
728enum {
729 R2M_SHORT_PREAMBLE = 0x01,
730 R2M_11A = 0x02,
731};
732
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000733static u8 zd_rate_to_modulation(u8 zd_rate, int flags)
Daniel Drakee85d0912006-06-02 17:11:32 +0100734{
735 u8 modulation;
736
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000737 modulation = zd_rate_typed(zd_rate);
Daniel Drakee85d0912006-06-02 17:11:32 +0100738 if (flags & R2M_SHORT_PREAMBLE) {
739 switch (ZD_CS_RATE(modulation)) {
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000740 case ZD_CCK_RATE_2M:
741 case ZD_CCK_RATE_5_5M:
742 case ZD_CCK_RATE_11M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100743 modulation |= ZD_CS_CCK_PREA_SHORT;
744 return modulation;
745 }
746 }
747 if (flags & R2M_11A) {
748 if (ZD_CS_TYPE(modulation) == ZD_CS_OFDM)
749 modulation |= ZD_CS_OFDM_MODE_11A;
750 }
751 return modulation;
752}
753
754static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
755 struct ieee80211_hdr_4addr *hdr)
756{
757 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
758 u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000759 u8 rate, zd_rate;
Daniel Drakee85d0912006-06-02 17:11:32 +0100760 int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000761 int is_multicast = is_multicast_ether_addr(hdr->addr1);
762 int short_preamble = ieee80211softmac_short_preamble_ok(softmac,
763 is_multicast, is_mgt);
764 int flags = 0;
Daniel Drakee85d0912006-06-02 17:11:32 +0100765
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000766 /* FIXME: 802.11a? */
767 rate = ieee80211softmac_suggest_txrate(softmac, is_multicast, is_mgt);
Daniel Drakee85d0912006-06-02 17:11:32 +0100768
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000769 if (short_preamble)
770 flags |= R2M_SHORT_PREAMBLE;
Daniel Drakee85d0912006-06-02 17:11:32 +0100771
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000772 zd_rate = rate_to_zd_rate(rate);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000773 cs->modulation = zd_rate_to_modulation(zd_rate, flags);
Daniel Drakee85d0912006-06-02 17:11:32 +0100774}
775
776static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
777 struct ieee80211_hdr_4addr *header)
778{
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000779 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
Daniel Drakee85d0912006-06-02 17:11:32 +0100780 unsigned int tx_length = le16_to_cpu(cs->tx_length);
781 u16 fctl = le16_to_cpu(header->frame_ctl);
782 u16 ftype = WLAN_FC_GET_TYPE(fctl);
783 u16 stype = WLAN_FC_GET_STYPE(fctl);
784
785 /*
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000786 * CONTROL TODO:
Daniel Drakee85d0912006-06-02 17:11:32 +0100787 * - if backoff needed, enable bit 0
788 * - if burst (backoff not needed) disable bit 0
Daniel Drakee85d0912006-06-02 17:11:32 +0100789 */
790
791 cs->control = 0;
792
793 /* First fragment */
794 if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
795 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
796
797 /* Multicast */
798 if (is_multicast_ether_addr(header->addr1))
799 cs->control |= ZD_CS_MULTICAST;
800
801 /* PS-POLL */
802 if (stype == IEEE80211_STYPE_PSPOLL)
803 cs->control |= ZD_CS_PS_POLL_FRAME;
804
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000805 /* Unicast data frames over the threshold should have RTS */
Daniel Drakee85d0912006-06-02 17:11:32 +0100806 if (!is_multicast_ether_addr(header->addr1) &&
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000807 ftype != IEEE80211_FTYPE_MGMT &&
808 tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
809 cs->control |= ZD_CS_RTS;
810
811 /* Use CTS-to-self protection if required */
812 if (ZD_CS_TYPE(cs->modulation) == ZD_CS_OFDM &&
813 ieee80211softmac_protection_needed(softmac)) {
814 /* FIXME: avoid sending RTS *and* self-CTS, is that correct? */
815 cs->control &= ~ZD_CS_RTS;
816 cs->control |= ZD_CS_SELF_CTS;
Daniel Drakee85d0912006-06-02 17:11:32 +0100817 }
818
819 /* FIXME: Management frame? */
820}
821
822static int fill_ctrlset(struct zd_mac *mac,
823 struct ieee80211_txb *txb,
824 int frag_num)
825{
826 int r;
827 struct sk_buff *skb = txb->fragments[frag_num];
828 struct ieee80211_hdr_4addr *hdr =
829 (struct ieee80211_hdr_4addr *) skb->data;
830 unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
831 unsigned int next_frag_len;
832 unsigned int packet_length;
833 struct zd_ctrlset *cs = (struct zd_ctrlset *)
834 skb_push(skb, sizeof(struct zd_ctrlset));
835
836 if (frag_num+1 < txb->nr_frags) {
837 next_frag_len = txb->fragments[frag_num+1]->len +
838 IEEE80211_FCS_LEN;
839 } else {
840 next_frag_len = 0;
841 }
842 ZD_ASSERT(frag_len <= 0xffff);
843 ZD_ASSERT(next_frag_len <= 0xffff);
844
845 cs_set_modulation(mac, cs, hdr);
846
847 cs->tx_length = cpu_to_le16(frag_len);
848
849 cs_set_control(mac, cs, hdr);
850
851 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
852 ZD_ASSERT(packet_length <= 0xffff);
853 /* ZD1211B: Computing the length difference this way, gives us
854 * flexibility to compute the packet length.
855 */
856 cs->packet_length = cpu_to_le16(mac->chip.is_zd1211b ?
857 packet_length - frag_len : packet_length);
858
859 /*
860 * CURRENT LENGTH:
861 * - transmit frame length in microseconds
862 * - seems to be derived from frame length
863 * - see Cal_Us_Service() in zdinlinef.h
864 * - if macp->bTxBurstEnable is enabled, then multiply by 4
865 * - bTxBurstEnable is never set in the vendor driver
866 *
867 * SERVICE:
868 * - "for PLCP configuration"
869 * - always 0 except in some situations at 802.11b 11M
870 * - see line 53 of zdinlinef.h
871 */
872 cs->service = 0;
873 r = zd_calc_tx_length_us(&cs->service, ZD_CS_RATE(cs->modulation),
874 le16_to_cpu(cs->tx_length));
875 if (r < 0)
876 return r;
877 cs->current_length = cpu_to_le16(r);
878
879 if (next_frag_len == 0) {
880 cs->next_frame_length = 0;
881 } else {
882 r = zd_calc_tx_length_us(NULL, ZD_CS_RATE(cs->modulation),
883 next_frag_len);
884 if (r < 0)
885 return r;
886 cs->next_frame_length = cpu_to_le16(r);
887 }
888
889 return 0;
890}
891
892static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
893{
894 int i, r;
895
896 for (i = 0; i < txb->nr_frags; i++) {
897 struct sk_buff *skb = txb->fragments[i];
898
899 r = fill_ctrlset(mac, txb, i);
900 if (r)
901 return r;
902 r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
903 if (r)
904 return r;
905 }
906
907 /* FIXME: shouldn't this be handled by the upper layers? */
908 mac->netdev->trans_start = jiffies;
909
910 ieee80211_txb_free(txb);
911 return 0;
912}
913
914struct zd_rt_hdr {
915 struct ieee80211_radiotap_header rt_hdr;
916 u8 rt_flags;
Ulrich Kunitz99f65f22006-08-01 23:43:30 +0200917 u8 rt_rate;
Daniel Drakee85d0912006-06-02 17:11:32 +0100918 u16 rt_channel;
919 u16 rt_chbitmask;
John W. Linvillea88556a2006-11-28 14:16:37 -0500920} __attribute__((packed));
Daniel Drakee85d0912006-06-02 17:11:32 +0100921
922static void fill_rt_header(void *buffer, struct zd_mac *mac,
923 const struct ieee80211_rx_stats *stats,
924 const struct rx_status *status)
925{
926 struct zd_rt_hdr *hdr = buffer;
927
928 hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
929 hdr->rt_hdr.it_pad = 0;
930 hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
931 hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
932 (1 << IEEE80211_RADIOTAP_CHANNEL) |
933 (1 << IEEE80211_RADIOTAP_RATE));
934
935 hdr->rt_flags = 0;
936 if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
937 hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
938
Ulrich Kunitz99f65f22006-08-01 23:43:30 +0200939 hdr->rt_rate = stats->rate / 5;
940
Daniel Drakee85d0912006-06-02 17:11:32 +0100941 /* FIXME: 802.11a */
942 hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
943 _zd_chip_get_channel(&mac->chip)));
944 hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
945 ((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
946 ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
Daniel Drakee85d0912006-06-02 17:11:32 +0100947}
948
949/* Returns 1 if the data packet is for us and 0 otherwise. */
950static int is_data_packet_for_us(struct ieee80211_device *ieee,
951 struct ieee80211_hdr_4addr *hdr)
952{
953 struct net_device *netdev = ieee->dev;
954 u16 fc = le16_to_cpu(hdr->frame_ctl);
955
956 ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
957
958 switch (ieee->iw_mode) {
959 case IW_MODE_ADHOC:
960 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
961 memcmp(hdr->addr3, ieee->bssid, ETH_ALEN) != 0)
962 return 0;
963 break;
964 case IW_MODE_AUTO:
965 case IW_MODE_INFRA:
966 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
967 IEEE80211_FCTL_FROMDS ||
968 memcmp(hdr->addr2, ieee->bssid, ETH_ALEN) != 0)
969 return 0;
970 break;
971 default:
972 ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
973 return 0;
974 }
975
976 return memcmp(hdr->addr1, netdev->dev_addr, ETH_ALEN) == 0 ||
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000977 (is_multicast_ether_addr(hdr->addr1) &&
978 memcmp(hdr->addr3, netdev->dev_addr, ETH_ALEN) != 0) ||
Daniel Drakee85d0912006-06-02 17:11:32 +0100979 (netdev->flags & IFF_PROMISC);
980}
981
Ulrich Kunitz741fec52006-11-22 00:05:53 +0000982/* Filters received packets. The function returns 1 if the packet should be
983 * forwarded to ieee80211_rx(). If the packet should be ignored the function
984 * returns 0. If an invalid packet is found the function returns -EINVAL.
985 *
986 * The function calls ieee80211_rx_mgt() directly.
Daniel Drakee85d0912006-06-02 17:11:32 +0100987 *
988 * It has been based on ieee80211_rx_any.
989 */
990static int filter_rx(struct ieee80211_device *ieee,
991 const u8 *buffer, unsigned int length,
992 struct ieee80211_rx_stats *stats)
993{
994 struct ieee80211_hdr_4addr *hdr;
995 u16 fc;
996
997 if (ieee->iw_mode == IW_MODE_MONITOR)
998 return 1;
999
1000 hdr = (struct ieee80211_hdr_4addr *)buffer;
1001 fc = le16_to_cpu(hdr->frame_ctl);
1002 if ((fc & IEEE80211_FCTL_VERS) != 0)
1003 return -EINVAL;
1004
1005 switch (WLAN_FC_GET_TYPE(fc)) {
1006 case IEEE80211_FTYPE_MGMT:
1007 if (length < sizeof(struct ieee80211_hdr_3addr))
1008 return -EINVAL;
1009 ieee80211_rx_mgt(ieee, hdr, stats);
1010 return 0;
1011 case IEEE80211_FTYPE_CTL:
Daniel Drakee85d0912006-06-02 17:11:32 +01001012 return 0;
1013 case IEEE80211_FTYPE_DATA:
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001014 /* Ignore invalid short buffers */
Daniel Drakee85d0912006-06-02 17:11:32 +01001015 if (length < sizeof(struct ieee80211_hdr_3addr))
1016 return -EINVAL;
1017 return is_data_packet_for_us(ieee, hdr);
1018 }
1019
1020 return -EINVAL;
1021}
1022
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001023static void update_qual_rssi(struct zd_mac *mac,
1024 const u8 *buffer, unsigned int length,
1025 u8 qual_percent, u8 rssi_percent)
Daniel Drakee85d0912006-06-02 17:11:32 +01001026{
1027 unsigned long flags;
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001028 struct ieee80211_hdr_3addr *hdr;
1029 int i;
1030
1031 hdr = (struct ieee80211_hdr_3addr *)buffer;
1032 if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
1033 return;
1034 if (memcmp(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid, ETH_ALEN) != 0)
1035 return;
Daniel Drakee85d0912006-06-02 17:11:32 +01001036
1037 spin_lock_irqsave(&mac->lock, flags);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001038 i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
1039 mac->qual_buffer[i] = qual_percent;
1040 mac->rssi_buffer[i] = rssi_percent;
1041 mac->stats_count++;
Daniel Drakee85d0912006-06-02 17:11:32 +01001042 spin_unlock_irqrestore(&mac->lock, flags);
1043}
1044
1045static int fill_rx_stats(struct ieee80211_rx_stats *stats,
1046 const struct rx_status **pstatus,
1047 struct zd_mac *mac,
1048 const u8 *buffer, unsigned int length)
1049{
1050 const struct rx_status *status;
1051
1052 *pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
1053 if (status->frame_status & ZD_RX_ERROR) {
1054 /* FIXME: update? */
1055 return -EINVAL;
1056 }
1057 memset(stats, 0, sizeof(struct ieee80211_rx_stats));
1058 stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
1059 + sizeof(struct rx_status));
1060 /* FIXME: 802.11a */
1061 stats->freq = IEEE80211_24GHZ_BAND;
1062 stats->received_channel = _zd_chip_get_channel(&mac->chip);
1063 stats->rssi = zd_rx_strength_percent(status->signal_strength);
1064 stats->signal = zd_rx_qual_percent(buffer,
1065 length - sizeof(struct rx_status),
1066 status);
1067 stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
1068 stats->rate = zd_rx_rate(buffer, status);
1069 if (stats->rate)
1070 stats->mask |= IEEE80211_STATMASK_RATE;
1071
Daniel Drakee85d0912006-06-02 17:11:32 +01001072 return 0;
1073}
1074
1075int zd_mac_rx(struct zd_mac *mac, const u8 *buffer, unsigned int length)
1076{
1077 int r;
1078 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1079 struct ieee80211_rx_stats stats;
1080 const struct rx_status *status;
1081 struct sk_buff *skb;
1082
1083 if (length < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
1084 IEEE80211_FCS_LEN + sizeof(struct rx_status))
1085 return -EINVAL;
1086
1087 r = fill_rx_stats(&stats, &status, mac, buffer, length);
1088 if (r)
1089 return r;
1090
1091 length -= ZD_PLCP_HEADER_SIZE+IEEE80211_FCS_LEN+
1092 sizeof(struct rx_status);
1093 buffer += ZD_PLCP_HEADER_SIZE;
1094
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001095 update_qual_rssi(mac, buffer, length, stats.signal, stats.rssi);
1096
Daniel Drakee85d0912006-06-02 17:11:32 +01001097 r = filter_rx(ieee, buffer, length, &stats);
1098 if (r <= 0)
1099 return r;
1100
1101 skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
1102 if (!skb)
1103 return -ENOMEM;
1104 if (ieee->iw_mode == IW_MODE_MONITOR)
1105 fill_rt_header(skb_put(skb, sizeof(struct zd_rt_hdr)), mac,
1106 &stats, status);
1107 memcpy(skb_put(skb, length), buffer, length);
1108
1109 r = ieee80211_rx(ieee, skb, &stats);
Daniel Drake383956a2006-12-01 00:56:50 +00001110 if (!r)
1111 dev_kfree_skb_any(skb);
Daniel Drakee85d0912006-06-02 17:11:32 +01001112 return 0;
1113}
1114
1115static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
1116 int pri)
1117{
1118 return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
1119}
1120
1121static void set_security(struct net_device *netdev,
1122 struct ieee80211_security *sec)
1123{
1124 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
1125 struct ieee80211_security *secinfo = &ieee->sec;
1126 int keyidx;
1127
1128 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
1129
1130 for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
1131 if (sec->flags & (1<<keyidx)) {
1132 secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
1133 secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
1134 memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
1135 SCM_KEY_LEN);
1136 }
1137
1138 if (sec->flags & SEC_ACTIVE_KEY) {
1139 secinfo->active_key = sec->active_key;
1140 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1141 " .active_key = %d\n", sec->active_key);
1142 }
1143 if (sec->flags & SEC_UNICAST_GROUP) {
1144 secinfo->unicast_uses_group = sec->unicast_uses_group;
1145 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1146 " .unicast_uses_group = %d\n",
1147 sec->unicast_uses_group);
1148 }
1149 if (sec->flags & SEC_LEVEL) {
1150 secinfo->level = sec->level;
1151 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1152 " .level = %d\n", sec->level);
1153 }
1154 if (sec->flags & SEC_ENABLED) {
1155 secinfo->enabled = sec->enabled;
1156 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1157 " .enabled = %d\n", sec->enabled);
1158 }
1159 if (sec->flags & SEC_ENCRYPT) {
1160 secinfo->encrypt = sec->encrypt;
1161 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1162 " .encrypt = %d\n", sec->encrypt);
1163 }
1164 if (sec->flags & SEC_AUTH_MODE) {
1165 secinfo->auth_mode = sec->auth_mode;
1166 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1167 " .auth_mode = %d\n", sec->auth_mode);
1168 }
1169}
1170
1171static void ieee_init(struct ieee80211_device *ieee)
1172{
1173 ieee->mode = IEEE_B | IEEE_G;
1174 ieee->freq_band = IEEE80211_24GHZ_BAND;
1175 ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
1176 ieee->tx_headroom = sizeof(struct zd_ctrlset);
1177 ieee->set_security = set_security;
1178 ieee->hard_start_xmit = netdev_tx;
1179
1180 /* Software encryption/decryption for now */
1181 ieee->host_build_iv = 0;
1182 ieee->host_encrypt = 1;
1183 ieee->host_decrypt = 1;
1184
1185 /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
1186 * correctly support AUTO */
1187 ieee->iw_mode = IW_MODE_INFRA;
1188}
1189
1190static void softmac_init(struct ieee80211softmac_device *sm)
1191{
1192 sm->set_channel = set_channel;
Daniel Drakeb1382ed2006-11-22 00:06:48 +00001193 sm->bssinfo_change = bssinfo_change;
Daniel Drakee85d0912006-06-02 17:11:32 +01001194}
1195
1196struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
1197{
1198 struct zd_mac *mac = zd_netdev_mac(ndev);
1199 struct iw_statistics *iw_stats = &mac->iw_stats;
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001200 unsigned int i, count, qual_total, rssi_total;
Daniel Drakee85d0912006-06-02 17:11:32 +01001201
1202 memset(iw_stats, 0, sizeof(struct iw_statistics));
1203 /* We are not setting the status, because ieee->state is not updated
1204 * at all and this driver doesn't track authentication state.
1205 */
1206 spin_lock_irq(&mac->lock);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001207 count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
1208 mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
1209 qual_total = rssi_total = 0;
1210 for (i = 0; i < count; i++) {
1211 qual_total += mac->qual_buffer[i];
1212 rssi_total += mac->rssi_buffer[i];
1213 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001214 spin_unlock_irq(&mac->lock);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001215 iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
1216 if (count > 0) {
1217 iw_stats->qual.qual = qual_total / count;
1218 iw_stats->qual.level = rssi_total / count;
1219 iw_stats->qual.updated |=
1220 IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
1221 } else {
1222 iw_stats->qual.updated |=
1223 IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
1224 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001225 /* TODO: update counter */
1226 return iw_stats;
1227}
1228
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001229#define LINK_LED_WORK_DELAY HZ
1230
David Howellsc4028952006-11-22 14:57:56 +00001231static void link_led_handler(struct work_struct *work)
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001232{
David Howellsc4028952006-11-22 14:57:56 +00001233 struct zd_mac *mac =
1234 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001235 struct zd_chip *chip = &mac->chip;
1236 struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
1237 int is_associated;
1238 int r;
1239
1240 spin_lock_irq(&mac->lock);
John W. Linville41072a12006-10-17 13:47:40 -04001241 is_associated = sm->associnfo.associated != 0;
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001242 spin_unlock_irq(&mac->lock);
1243
1244 r = zd_chip_control_leds(chip,
1245 is_associated ? LED_ASSOCIATED : LED_SCANNING);
1246 if (r)
1247 dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1248
1249 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1250 LINK_LED_WORK_DELAY);
1251}
1252
1253static void housekeeping_init(struct zd_mac *mac)
1254{
David Howellsc4028952006-11-22 14:57:56 +00001255 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001256}
1257
1258static void housekeeping_enable(struct zd_mac *mac)
1259{
1260 dev_dbg_f(zd_mac_dev(mac), "\n");
1261 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1262 0);
1263}
1264
1265static void housekeeping_disable(struct zd_mac *mac)
1266{
1267 dev_dbg_f(zd_mac_dev(mac), "\n");
1268 cancel_rearming_delayed_workqueue(zd_workqueue,
1269 &mac->housekeeping.link_led_work);
1270 zd_chip_control_leds(&mac->chip, LED_OFF);
1271}