blob: 70707816db3dd67bd20315ac981ad815d4f4907a [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)) {
116 dev_dbg_f(zd_mac_dev(mac),
117 "Regulatory Domain %#04x is not supported.\n",
118 default_regdomain);
119 r = -EINVAL;
120 goto disable_int;
121 }
122 spin_lock_irq(&mac->lock);
123 mac->regdomain = mac->default_regdomain = default_regdomain;
124 spin_unlock_irq(&mac->lock);
125 r = reset_channel(mac);
126 if (r)
127 goto disable_int;
128
Daniel Drake40da08b2006-08-01 23:43:32 +0200129 /* We must inform the device that we are doing encryption/decryption in
130 * software at the moment. */
131 r = zd_set_encryption_type(chip, ENC_SNIFFER);
Daniel Drakee85d0912006-06-02 17:11:32 +0100132 if (r)
133 goto disable_int;
134
135 r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
136 if (r)
137 goto disable_int;
138
139 r = 0;
140disable_int:
141 zd_chip_disable_int(chip);
142out:
143 return r;
144}
145
146void zd_mac_clear(struct zd_mac *mac)
147{
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000148 flush_workqueue(zd_workqueue);
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800149 skb_queue_purge(&mac->rx_queue);
150 tasklet_kill(&mac->rx_tasklet);
Daniel Drakee85d0912006-06-02 17:11:32 +0100151 zd_chip_clear(&mac->chip);
Ulrich Kunitzc48cf122006-08-12 18:00:17 +0100152 ZD_ASSERT(!spin_is_locked(&mac->lock));
153 ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
Daniel Drakee85d0912006-06-02 17:11:32 +0100154}
155
156static int reset_mode(struct zd_mac *mac)
157{
158 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
Ulrich Kunitzfa8e29c2007-01-29 00:59:40 +0000159 struct zd_ioreq32 ioreqs[] = {
Ulrich Kunitzfde627b2006-08-01 23:43:35 +0200160 { CR_RX_FILTER, STA_RX_FILTER },
Daniel Drakee85d0912006-06-02 17:11:32 +0100161 { CR_SNIFFER_ON, 0U },
Daniel Drakee85d0912006-06-02 17:11:32 +0100162 };
163
164 if (ieee->iw_mode == IW_MODE_MONITOR) {
165 ioreqs[0].value = 0xffffffff;
166 ioreqs[1].value = 0x1;
Daniel Drakee85d0912006-06-02 17:11:32 +0100167 }
168
Ulrich Kunitzfa8e29c2007-01-29 00:59:40 +0000169 return zd_iowrite32a(&mac->chip, ioreqs, ARRAY_SIZE(ioreqs));
Daniel Drakee85d0912006-06-02 17:11:32 +0100170}
171
172int zd_mac_open(struct net_device *netdev)
173{
174 struct zd_mac *mac = zd_netdev_mac(netdev);
175 struct zd_chip *chip = &mac->chip;
176 int r;
177
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800178 tasklet_enable(&mac->rx_tasklet);
179
Daniel Drakee85d0912006-06-02 17:11:32 +0100180 r = zd_chip_enable_int(chip);
181 if (r < 0)
182 goto out;
183
184 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
185 if (r < 0)
186 goto disable_int;
187 r = reset_mode(mac);
188 if (r)
189 goto disable_int;
190 r = zd_chip_switch_radio_on(chip);
191 if (r < 0)
192 goto disable_int;
193 r = zd_chip_set_channel(chip, mac->requested_channel);
194 if (r < 0)
195 goto disable_radio;
196 r = zd_chip_enable_rx(chip);
197 if (r < 0)
198 goto disable_radio;
199 r = zd_chip_enable_hwint(chip);
200 if (r < 0)
201 goto disable_rx;
202
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100203 housekeeping_enable(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100204 ieee80211softmac_start(netdev);
205 return 0;
206disable_rx:
207 zd_chip_disable_rx(chip);
208disable_radio:
209 zd_chip_switch_radio_off(chip);
210disable_int:
211 zd_chip_disable_int(chip);
212out:
213 return r;
214}
215
216int zd_mac_stop(struct net_device *netdev)
217{
218 struct zd_mac *mac = zd_netdev_mac(netdev);
219 struct zd_chip *chip = &mac->chip;
220
Daniel Drakec9a4b352006-06-11 23:18:54 +0100221 netif_stop_queue(netdev);
222
Daniel Drakee85d0912006-06-02 17:11:32 +0100223 /*
224 * The order here deliberately is a little different from the open()
225 * method, since we need to make sure there is no opportunity for RX
226 * frames to be processed by softmac after we have stopped it.
227 */
228
229 zd_chip_disable_rx(chip);
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800230 skb_queue_purge(&mac->rx_queue);
231 tasklet_disable(&mac->rx_tasklet);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +0100232 housekeeping_disable(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100233 ieee80211softmac_stop(netdev);
234
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000235 /* Ensure no work items are running or queued from this point */
236 cancel_delayed_work(&mac->set_rts_cts_work);
237 cancel_delayed_work(&mac->set_basic_rates_work);
238 flush_workqueue(zd_workqueue);
239 mac->updating_rts_rate = 0;
240 mac->updating_basic_rates = 0;
241
Daniel Drakee85d0912006-06-02 17:11:32 +0100242 zd_chip_disable_hwint(chip);
243 zd_chip_switch_radio_off(chip);
244 zd_chip_disable_int(chip);
245
246 return 0;
247}
248
249int zd_mac_set_mac_address(struct net_device *netdev, void *p)
250{
251 int r;
252 unsigned long flags;
253 struct sockaddr *addr = p;
254 struct zd_mac *mac = zd_netdev_mac(netdev);
255 struct zd_chip *chip = &mac->chip;
256
257 if (!is_valid_ether_addr(addr->sa_data))
258 return -EADDRNOTAVAIL;
259
260 dev_dbg_f(zd_mac_dev(mac),
261 "Setting MAC to " MAC_FMT "\n", MAC_ARG(addr->sa_data));
262
263 r = zd_write_mac_addr(chip, addr->sa_data);
264 if (r)
265 return r;
266
267 spin_lock_irqsave(&mac->lock, flags);
268 memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
269 spin_unlock_irqrestore(&mac->lock, flags);
270
271 return 0;
272}
273
Jeff Garzik0ae85132006-12-07 06:30:30 -0500274static void set_multicast_hash_handler(struct work_struct *work)
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000275{
Jeff Garzik0ae85132006-12-07 06:30:30 -0500276 struct zd_mac *mac = container_of(work, struct zd_mac,
277 set_multicast_hash_work);
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000278 struct zd_mc_hash hash;
279
280 spin_lock_irq(&mac->lock);
281 hash = mac->multicast_hash;
282 spin_unlock_irq(&mac->lock);
283
284 zd_chip_set_multicast_hash(&mac->chip, &hash);
285}
286
287void zd_mac_set_multicast_list(struct net_device *dev)
288{
289 struct zd_mc_hash hash;
290 struct zd_mac *mac = zd_netdev_mac(dev);
291 struct dev_mc_list *mc;
292 unsigned long flags;
293
294 if (dev->flags & (IFF_PROMISC|IFF_ALLMULTI)) {
295 zd_mc_add_all(&hash);
296 } else {
297 zd_mc_clear(&hash);
298 for (mc = dev->mc_list; mc; mc = mc->next) {
299 dev_dbg_f(zd_mac_dev(mac), "mc addr " MAC_FMT "\n",
300 MAC_ARG(mc->dmi_addr));
301 zd_mc_add_addr(&hash, mc->dmi_addr);
302 }
303 }
304
305 spin_lock_irqsave(&mac->lock, flags);
306 mac->multicast_hash = hash;
307 spin_unlock_irqrestore(&mac->lock, flags);
308 queue_work(zd_workqueue, &mac->set_multicast_hash_work);
309}
310
Daniel Drakee85d0912006-06-02 17:11:32 +0100311int zd_mac_set_regdomain(struct zd_mac *mac, u8 regdomain)
312{
313 int r;
314 u8 channel;
315
316 ZD_ASSERT(!irqs_disabled());
317 spin_lock_irq(&mac->lock);
318 if (regdomain == 0) {
319 regdomain = mac->default_regdomain;
320 }
321 if (!zd_regdomain_supported(regdomain)) {
322 spin_unlock_irq(&mac->lock);
323 return -EINVAL;
324 }
325 mac->regdomain = regdomain;
326 channel = mac->requested_channel;
327 spin_unlock_irq(&mac->lock);
328
329 r = zd_geo_init(zd_mac_to_ieee80211(mac), regdomain);
330 if (r)
331 return r;
332 if (!zd_regdomain_supports_channel(regdomain, channel)) {
333 r = reset_channel(mac);
334 if (r)
335 return r;
336 }
337
338 return 0;
339}
340
341u8 zd_mac_get_regdomain(struct zd_mac *mac)
342{
343 unsigned long flags;
344 u8 regdomain;
345
346 spin_lock_irqsave(&mac->lock, flags);
347 regdomain = mac->regdomain;
348 spin_unlock_irqrestore(&mac->lock, flags);
349 return regdomain;
350}
351
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000352/* Fallback to lowest rate, if rate is unknown. */
353static u8 rate_to_zd_rate(u8 rate)
354{
355 switch (rate) {
356 case IEEE80211_CCK_RATE_2MB:
357 return ZD_CCK_RATE_2M;
358 case IEEE80211_CCK_RATE_5MB:
359 return ZD_CCK_RATE_5_5M;
360 case IEEE80211_CCK_RATE_11MB:
361 return ZD_CCK_RATE_11M;
362 case IEEE80211_OFDM_RATE_6MB:
363 return ZD_OFDM_RATE_6M;
364 case IEEE80211_OFDM_RATE_9MB:
365 return ZD_OFDM_RATE_9M;
366 case IEEE80211_OFDM_RATE_12MB:
367 return ZD_OFDM_RATE_12M;
368 case IEEE80211_OFDM_RATE_18MB:
369 return ZD_OFDM_RATE_18M;
370 case IEEE80211_OFDM_RATE_24MB:
371 return ZD_OFDM_RATE_24M;
372 case IEEE80211_OFDM_RATE_36MB:
373 return ZD_OFDM_RATE_36M;
374 case IEEE80211_OFDM_RATE_48MB:
375 return ZD_OFDM_RATE_48M;
376 case IEEE80211_OFDM_RATE_54MB:
377 return ZD_OFDM_RATE_54M;
378 }
379 return ZD_CCK_RATE_1M;
380}
381
382static u16 rate_to_cr_rate(u8 rate)
383{
384 switch (rate) {
385 case IEEE80211_CCK_RATE_2MB:
386 return CR_RATE_1M;
387 case IEEE80211_CCK_RATE_5MB:
388 return CR_RATE_5_5M;
389 case IEEE80211_CCK_RATE_11MB:
390 return CR_RATE_11M;
391 case IEEE80211_OFDM_RATE_6MB:
392 return CR_RATE_6M;
393 case IEEE80211_OFDM_RATE_9MB:
394 return CR_RATE_9M;
395 case IEEE80211_OFDM_RATE_12MB:
396 return CR_RATE_12M;
397 case IEEE80211_OFDM_RATE_18MB:
398 return CR_RATE_18M;
399 case IEEE80211_OFDM_RATE_24MB:
400 return CR_RATE_24M;
401 case IEEE80211_OFDM_RATE_36MB:
402 return CR_RATE_36M;
403 case IEEE80211_OFDM_RATE_48MB:
404 return CR_RATE_48M;
405 case IEEE80211_OFDM_RATE_54MB:
406 return CR_RATE_54M;
407 }
408 return CR_RATE_1M;
409}
410
411static void try_enable_tx(struct zd_mac *mac)
412{
413 unsigned long flags;
414
415 spin_lock_irqsave(&mac->lock, flags);
416 if (mac->updating_rts_rate == 0 && mac->updating_basic_rates == 0)
417 netif_wake_queue(mac->netdev);
418 spin_unlock_irqrestore(&mac->lock, flags);
419}
420
David Howells6d5aefb2006-12-05 19:36:26 +0000421static void set_rts_cts_work(struct work_struct *work)
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000422{
David Howells6d5aefb2006-12-05 19:36:26 +0000423 struct zd_mac *mac =
424 container_of(work, struct zd_mac, set_rts_cts_work.work);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000425 unsigned long flags;
426 u8 rts_rate;
427 unsigned int short_preamble;
428
429 mutex_lock(&mac->chip.mutex);
430
431 spin_lock_irqsave(&mac->lock, flags);
432 mac->updating_rts_rate = 0;
433 rts_rate = mac->rts_rate;
434 short_preamble = mac->short_preamble;
435 spin_unlock_irqrestore(&mac->lock, flags);
436
437 zd_chip_set_rts_cts_rate_locked(&mac->chip, rts_rate, short_preamble);
438 mutex_unlock(&mac->chip.mutex);
439
440 try_enable_tx(mac);
441}
442
David Howells6d5aefb2006-12-05 19:36:26 +0000443static void set_basic_rates_work(struct work_struct *work)
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000444{
David Howells6d5aefb2006-12-05 19:36:26 +0000445 struct zd_mac *mac =
446 container_of(work, struct zd_mac, set_basic_rates_work.work);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000447 unsigned long flags;
448 u16 basic_rates;
449
450 mutex_lock(&mac->chip.mutex);
451
452 spin_lock_irqsave(&mac->lock, flags);
453 mac->updating_basic_rates = 0;
454 basic_rates = mac->basic_rates;
455 spin_unlock_irqrestore(&mac->lock, flags);
456
457 zd_chip_set_basic_rates_locked(&mac->chip, basic_rates);
458 mutex_unlock(&mac->chip.mutex);
459
460 try_enable_tx(mac);
461}
462
463static void bssinfo_change(struct net_device *netdev, u32 changes)
464{
465 struct zd_mac *mac = zd_netdev_mac(netdev);
466 struct ieee80211softmac_device *softmac = ieee80211_priv(netdev);
467 struct ieee80211softmac_bss_info *bssinfo = &softmac->bssinfo;
468 int need_set_rts_cts = 0;
469 int need_set_rates = 0;
470 u16 basic_rates;
471 unsigned long flags;
472
473 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
474
475 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_SHORT_PREAMBLE) {
476 spin_lock_irqsave(&mac->lock, flags);
477 mac->short_preamble = bssinfo->short_preamble;
478 spin_unlock_irqrestore(&mac->lock, flags);
479 need_set_rts_cts = 1;
480 }
481
482 if (changes & IEEE80211SOFTMAC_BSSINFOCHG_RATES) {
483 /* Set RTS rate to highest available basic rate */
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800484 u8 hi_rate = ieee80211softmac_highest_supported_rate(softmac,
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000485 &bssinfo->supported_rates, 1);
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800486 hi_rate = rate_to_zd_rate(hi_rate);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000487
488 spin_lock_irqsave(&mac->lock, flags);
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -0800489 if (hi_rate != mac->rts_rate) {
490 mac->rts_rate = hi_rate;
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000491 need_set_rts_cts = 1;
492 }
493 spin_unlock_irqrestore(&mac->lock, flags);
494
495 /* Set basic rates */
496 need_set_rates = 1;
497 if (bssinfo->supported_rates.count == 0) {
498 /* Allow the device to be flexible */
499 basic_rates = CR_RATES_80211B | CR_RATES_80211G;
500 } else {
501 int i = 0;
502 basic_rates = 0;
503
504 for (i = 0; i < bssinfo->supported_rates.count; i++) {
505 u16 rate = bssinfo->supported_rates.rates[i];
506 if ((rate & IEEE80211_BASIC_RATE_MASK) == 0)
507 continue;
508
509 rate &= ~IEEE80211_BASIC_RATE_MASK;
510 basic_rates |= rate_to_cr_rate(rate);
511 }
512 }
513 spin_lock_irqsave(&mac->lock, flags);
514 mac->basic_rates = basic_rates;
515 spin_unlock_irqrestore(&mac->lock, flags);
516 }
517
518 /* Schedule any changes we made above */
519
520 spin_lock_irqsave(&mac->lock, flags);
521 if (need_set_rts_cts && !mac->updating_rts_rate) {
522 mac->updating_rts_rate = 1;
523 netif_stop_queue(mac->netdev);
David Howells6d5aefb2006-12-05 19:36:26 +0000524 queue_delayed_work(zd_workqueue, &mac->set_rts_cts_work, 0);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000525 }
526 if (need_set_rates && !mac->updating_basic_rates) {
527 mac->updating_basic_rates = 1;
528 netif_stop_queue(mac->netdev);
David Howells6d5aefb2006-12-05 19:36:26 +0000529 queue_delayed_work(zd_workqueue, &mac->set_basic_rates_work,
530 0);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000531 }
532 spin_unlock_irqrestore(&mac->lock, flags);
533}
534
Daniel Drakee85d0912006-06-02 17:11:32 +0100535static void set_channel(struct net_device *netdev, u8 channel)
536{
537 struct zd_mac *mac = zd_netdev_mac(netdev);
538
539 dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
540
541 zd_chip_set_channel(&mac->chip, channel);
542}
543
Daniel Drakee85d0912006-06-02 17:11:32 +0100544int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
545{
546 unsigned long lock_flags;
547 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
548
549 if (ieee->iw_mode == IW_MODE_INFRA)
550 return -EPERM;
551
552 spin_lock_irqsave(&mac->lock, lock_flags);
553 if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
554 spin_unlock_irqrestore(&mac->lock, lock_flags);
555 return -EINVAL;
556 }
557 mac->requested_channel = channel;
558 spin_unlock_irqrestore(&mac->lock, lock_flags);
559 if (netif_running(mac->netdev))
560 return zd_chip_set_channel(&mac->chip, channel);
561 else
562 return 0;
563}
564
Daniel Drake84bc7152006-11-22 00:05:30 +0000565u8 zd_mac_get_channel(struct zd_mac *mac)
Daniel Drakee85d0912006-06-02 17:11:32 +0100566{
Daniel Drake84bc7152006-11-22 00:05:30 +0000567 u8 channel = zd_chip_get_channel(&mac->chip);
Daniel Drakee85d0912006-06-02 17:11:32 +0100568
Daniel Drake84bc7152006-11-22 00:05:30 +0000569 dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
570 return channel;
Daniel Drakee85d0912006-06-02 17:11:32 +0100571}
572
573/* If wrong rate is given, we are falling back to the slowest rate: 1MBit/s */
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000574static u8 zd_rate_typed(u8 zd_rate)
Daniel Drakee85d0912006-06-02 17:11:32 +0100575{
576 static const u8 typed_rates[16] = {
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000577 [ZD_CCK_RATE_1M] = ZD_CS_CCK|ZD_CCK_RATE_1M,
578 [ZD_CCK_RATE_2M] = ZD_CS_CCK|ZD_CCK_RATE_2M,
579 [ZD_CCK_RATE_5_5M] = ZD_CS_CCK|ZD_CCK_RATE_5_5M,
580 [ZD_CCK_RATE_11M] = ZD_CS_CCK|ZD_CCK_RATE_11M,
Daniel Drakee85d0912006-06-02 17:11:32 +0100581 [ZD_OFDM_RATE_6M] = ZD_CS_OFDM|ZD_OFDM_RATE_6M,
582 [ZD_OFDM_RATE_9M] = ZD_CS_OFDM|ZD_OFDM_RATE_9M,
583 [ZD_OFDM_RATE_12M] = ZD_CS_OFDM|ZD_OFDM_RATE_12M,
584 [ZD_OFDM_RATE_18M] = ZD_CS_OFDM|ZD_OFDM_RATE_18M,
585 [ZD_OFDM_RATE_24M] = ZD_CS_OFDM|ZD_OFDM_RATE_24M,
586 [ZD_OFDM_RATE_36M] = ZD_CS_OFDM|ZD_OFDM_RATE_36M,
587 [ZD_OFDM_RATE_48M] = ZD_CS_OFDM|ZD_OFDM_RATE_48M,
588 [ZD_OFDM_RATE_54M] = ZD_CS_OFDM|ZD_OFDM_RATE_54M,
589 };
590
591 ZD_ASSERT(ZD_CS_RATE_MASK == 0x0f);
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000592 return typed_rates[zd_rate & ZD_CS_RATE_MASK];
Daniel Drakee85d0912006-06-02 17:11:32 +0100593}
594
595int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
596{
597 struct ieee80211_device *ieee;
598
599 switch (mode) {
600 case IW_MODE_AUTO:
601 case IW_MODE_ADHOC:
602 case IW_MODE_INFRA:
603 mac->netdev->type = ARPHRD_ETHER;
604 break;
605 case IW_MODE_MONITOR:
606 mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
607 break;
608 default:
609 dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
610 return -EINVAL;
611 }
612
613 ieee = zd_mac_to_ieee80211(mac);
614 ZD_ASSERT(!irqs_disabled());
615 spin_lock_irq(&ieee->lock);
616 ieee->iw_mode = mode;
617 spin_unlock_irq(&ieee->lock);
618
619 if (netif_running(mac->netdev))
620 return reset_mode(mac);
621
622 return 0;
623}
624
625int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
626{
627 unsigned long flags;
628 struct ieee80211_device *ieee;
629
630 ieee = zd_mac_to_ieee80211(mac);
631 spin_lock_irqsave(&ieee->lock, flags);
632 *mode = ieee->iw_mode;
633 spin_unlock_irqrestore(&ieee->lock, flags);
634 return 0;
635}
636
637int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
638{
639 int i;
640 const struct channel_range *channel_range;
641 u8 regdomain;
642
643 memset(range, 0, sizeof(*range));
644
645 /* FIXME: Not so important and depends on the mode. For 802.11g
646 * usually this value is used. It seems to be that Bit/s number is
647 * given here.
648 */
649 range->throughput = 27 * 1000 * 1000;
650
651 range->max_qual.qual = 100;
652 range->max_qual.level = 100;
653
654 /* FIXME: Needs still to be tuned. */
655 range->avg_qual.qual = 71;
656 range->avg_qual.level = 80;
657
658 /* FIXME: depends on standard? */
659 range->min_rts = 256;
660 range->max_rts = 2346;
661
662 range->min_frag = MIN_FRAG_THRESHOLD;
663 range->max_frag = MAX_FRAG_THRESHOLD;
664
665 range->max_encoding_tokens = WEP_KEYS;
666 range->num_encoding_sizes = 2;
667 range->encoding_size[0] = 5;
668 range->encoding_size[1] = WEP_KEY_LEN;
669
670 range->we_version_compiled = WIRELESS_EXT;
671 range->we_version_source = 20;
672
Daniel Drakeff9b99b2006-12-01 00:57:11 +0000673 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
674 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
675
Daniel Drakee85d0912006-06-02 17:11:32 +0100676 ZD_ASSERT(!irqs_disabled());
677 spin_lock_irq(&mac->lock);
678 regdomain = mac->regdomain;
679 spin_unlock_irq(&mac->lock);
680 channel_range = zd_channel_range(regdomain);
681
682 range->num_channels = channel_range->end - channel_range->start;
683 range->old_num_channels = range->num_channels;
684 range->num_frequency = range->num_channels;
685 range->old_num_frequency = range->num_frequency;
686
687 for (i = 0; i < range->num_frequency; i++) {
688 struct iw_freq *freq = &range->freq[i];
689 freq->i = channel_range->start + i;
690 zd_channel_to_freq(freq, freq->i);
691 }
692
693 return 0;
694}
695
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000696static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
Daniel Drakee85d0912006-06-02 17:11:32 +0100697{
698 static const u8 rate_divisor[] = {
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000699 [ZD_CCK_RATE_1M] = 1,
700 [ZD_CCK_RATE_2M] = 2,
701 [ZD_CCK_RATE_5_5M] = 11, /* bits must be doubled */
702 [ZD_CCK_RATE_11M] = 11,
Daniel Drakee85d0912006-06-02 17:11:32 +0100703 [ZD_OFDM_RATE_6M] = 6,
704 [ZD_OFDM_RATE_9M] = 9,
705 [ZD_OFDM_RATE_12M] = 12,
706 [ZD_OFDM_RATE_18M] = 18,
707 [ZD_OFDM_RATE_24M] = 24,
708 [ZD_OFDM_RATE_36M] = 36,
709 [ZD_OFDM_RATE_48M] = 48,
710 [ZD_OFDM_RATE_54M] = 54,
711 };
712
713 u32 bits = (u32)tx_length * 8;
714 u32 divisor;
715
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000716 divisor = rate_divisor[zd_rate];
Daniel Drakee85d0912006-06-02 17:11:32 +0100717 if (divisor == 0)
718 return -EINVAL;
719
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000720 switch (zd_rate) {
721 case ZD_CCK_RATE_5_5M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100722 bits = (2*bits) + 10; /* round up to the next integer */
723 break;
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000724 case ZD_CCK_RATE_11M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100725 if (service) {
726 u32 t = bits % 11;
727 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
728 if (0 < t && t <= 3) {
729 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
730 }
731 }
732 bits += 10; /* round up to the next integer */
733 break;
734 }
735
736 return bits/divisor;
737}
738
739enum {
740 R2M_SHORT_PREAMBLE = 0x01,
741 R2M_11A = 0x02,
742};
743
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000744static u8 zd_rate_to_modulation(u8 zd_rate, int flags)
Daniel Drakee85d0912006-06-02 17:11:32 +0100745{
746 u8 modulation;
747
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000748 modulation = zd_rate_typed(zd_rate);
Daniel Drakee85d0912006-06-02 17:11:32 +0100749 if (flags & R2M_SHORT_PREAMBLE) {
750 switch (ZD_CS_RATE(modulation)) {
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000751 case ZD_CCK_RATE_2M:
752 case ZD_CCK_RATE_5_5M:
753 case ZD_CCK_RATE_11M:
Daniel Drakee85d0912006-06-02 17:11:32 +0100754 modulation |= ZD_CS_CCK_PREA_SHORT;
755 return modulation;
756 }
757 }
758 if (flags & R2M_11A) {
759 if (ZD_CS_TYPE(modulation) == ZD_CS_OFDM)
760 modulation |= ZD_CS_OFDM_MODE_11A;
761 }
762 return modulation;
763}
764
765static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
766 struct ieee80211_hdr_4addr *hdr)
767{
768 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
769 u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000770 u8 rate, zd_rate;
Daniel Drakee85d0912006-06-02 17:11:32 +0100771 int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000772 int is_multicast = is_multicast_ether_addr(hdr->addr1);
773 int short_preamble = ieee80211softmac_short_preamble_ok(softmac,
774 is_multicast, is_mgt);
775 int flags = 0;
Daniel Drakee85d0912006-06-02 17:11:32 +0100776
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000777 /* FIXME: 802.11a? */
778 rate = ieee80211softmac_suggest_txrate(softmac, is_multicast, is_mgt);
Daniel Drakee85d0912006-06-02 17:11:32 +0100779
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000780 if (short_preamble)
781 flags |= R2M_SHORT_PREAMBLE;
Daniel Drakee85d0912006-06-02 17:11:32 +0100782
Daniel Drakeb1cd84162006-11-22 00:06:38 +0000783 zd_rate = rate_to_zd_rate(rate);
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000784 cs->modulation = zd_rate_to_modulation(zd_rate, flags);
Daniel Drakee85d0912006-06-02 17:11:32 +0100785}
786
787static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
788 struct ieee80211_hdr_4addr *header)
789{
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000790 struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
Daniel Drakee85d0912006-06-02 17:11:32 +0100791 unsigned int tx_length = le16_to_cpu(cs->tx_length);
792 u16 fctl = le16_to_cpu(header->frame_ctl);
793 u16 ftype = WLAN_FC_GET_TYPE(fctl);
794 u16 stype = WLAN_FC_GET_STYPE(fctl);
795
796 /*
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000797 * CONTROL TODO:
Daniel Drakee85d0912006-06-02 17:11:32 +0100798 * - if backoff needed, enable bit 0
799 * - if burst (backoff not needed) disable bit 0
Daniel Drakee85d0912006-06-02 17:11:32 +0100800 */
801
802 cs->control = 0;
803
804 /* First fragment */
805 if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
806 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
807
808 /* Multicast */
809 if (is_multicast_ether_addr(header->addr1))
810 cs->control |= ZD_CS_MULTICAST;
811
812 /* PS-POLL */
813 if (stype == IEEE80211_STYPE_PSPOLL)
814 cs->control |= ZD_CS_PS_POLL_FRAME;
815
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000816 /* Unicast data frames over the threshold should have RTS */
Daniel Drakee85d0912006-06-02 17:11:32 +0100817 if (!is_multicast_ether_addr(header->addr1) &&
Daniel Drakeb1382ed2006-11-22 00:06:48 +0000818 ftype != IEEE80211_FTYPE_MGMT &&
819 tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
820 cs->control |= ZD_CS_RTS;
821
822 /* Use CTS-to-self protection if required */
823 if (ZD_CS_TYPE(cs->modulation) == ZD_CS_OFDM &&
824 ieee80211softmac_protection_needed(softmac)) {
825 /* FIXME: avoid sending RTS *and* self-CTS, is that correct? */
826 cs->control &= ~ZD_CS_RTS;
827 cs->control |= ZD_CS_SELF_CTS;
Daniel Drakee85d0912006-06-02 17:11:32 +0100828 }
829
830 /* FIXME: Management frame? */
831}
832
833static int fill_ctrlset(struct zd_mac *mac,
834 struct ieee80211_txb *txb,
835 int frag_num)
836{
837 int r;
838 struct sk_buff *skb = txb->fragments[frag_num];
839 struct ieee80211_hdr_4addr *hdr =
840 (struct ieee80211_hdr_4addr *) skb->data;
841 unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
842 unsigned int next_frag_len;
843 unsigned int packet_length;
844 struct zd_ctrlset *cs = (struct zd_ctrlset *)
845 skb_push(skb, sizeof(struct zd_ctrlset));
846
847 if (frag_num+1 < txb->nr_frags) {
848 next_frag_len = txb->fragments[frag_num+1]->len +
849 IEEE80211_FCS_LEN;
850 } else {
851 next_frag_len = 0;
852 }
853 ZD_ASSERT(frag_len <= 0xffff);
854 ZD_ASSERT(next_frag_len <= 0xffff);
855
856 cs_set_modulation(mac, cs, hdr);
857
858 cs->tx_length = cpu_to_le16(frag_len);
859
860 cs_set_control(mac, cs, hdr);
861
862 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
863 ZD_ASSERT(packet_length <= 0xffff);
864 /* ZD1211B: Computing the length difference this way, gives us
865 * flexibility to compute the packet length.
866 */
867 cs->packet_length = cpu_to_le16(mac->chip.is_zd1211b ?
868 packet_length - frag_len : packet_length);
869
870 /*
871 * CURRENT LENGTH:
872 * - transmit frame length in microseconds
873 * - seems to be derived from frame length
874 * - see Cal_Us_Service() in zdinlinef.h
875 * - if macp->bTxBurstEnable is enabled, then multiply by 4
876 * - bTxBurstEnable is never set in the vendor driver
877 *
878 * SERVICE:
879 * - "for PLCP configuration"
880 * - always 0 except in some situations at 802.11b 11M
881 * - see line 53 of zdinlinef.h
882 */
883 cs->service = 0;
884 r = zd_calc_tx_length_us(&cs->service, ZD_CS_RATE(cs->modulation),
885 le16_to_cpu(cs->tx_length));
886 if (r < 0)
887 return r;
888 cs->current_length = cpu_to_le16(r);
889
890 if (next_frag_len == 0) {
891 cs->next_frame_length = 0;
892 } else {
893 r = zd_calc_tx_length_us(NULL, ZD_CS_RATE(cs->modulation),
894 next_frag_len);
895 if (r < 0)
896 return r;
897 cs->next_frame_length = cpu_to_le16(r);
898 }
899
900 return 0;
901}
902
903static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
904{
905 int i, r;
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000906 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
Daniel Drakee85d0912006-06-02 17:11:32 +0100907
908 for (i = 0; i < txb->nr_frags; i++) {
909 struct sk_buff *skb = txb->fragments[i];
910
911 r = fill_ctrlset(mac, txb, i);
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000912 if (r) {
913 ieee->stats.tx_dropped++;
Daniel Drakee85d0912006-06-02 17:11:32 +0100914 return r;
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000915 }
Daniel Drakee85d0912006-06-02 17:11:32 +0100916 r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000917 if (r) {
918 ieee->stats.tx_dropped++;
Daniel Drakee85d0912006-06-02 17:11:32 +0100919 return r;
Ulrich Kunitz22d34052007-01-29 01:00:03 +0000920 }
Daniel Drakee85d0912006-06-02 17:11:32 +0100921 }
922
923 /* FIXME: shouldn't this be handled by the upper layers? */
924 mac->netdev->trans_start = jiffies;
925
926 ieee80211_txb_free(txb);
927 return 0;
928}
929
930struct zd_rt_hdr {
931 struct ieee80211_radiotap_header rt_hdr;
932 u8 rt_flags;
Ulrich Kunitz99f65f22006-08-01 23:43:30 +0200933 u8 rt_rate;
Daniel Drakee85d0912006-06-02 17:11:32 +0100934 u16 rt_channel;
935 u16 rt_chbitmask;
John W. Linvillea88556a2006-11-28 14:16:37 -0500936} __attribute__((packed));
Daniel Drakee85d0912006-06-02 17:11:32 +0100937
938static void fill_rt_header(void *buffer, struct zd_mac *mac,
939 const struct ieee80211_rx_stats *stats,
940 const struct rx_status *status)
941{
942 struct zd_rt_hdr *hdr = buffer;
943
944 hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
945 hdr->rt_hdr.it_pad = 0;
946 hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
947 hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
948 (1 << IEEE80211_RADIOTAP_CHANNEL) |
949 (1 << IEEE80211_RADIOTAP_RATE));
950
951 hdr->rt_flags = 0;
952 if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
953 hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
954
Ulrich Kunitz99f65f22006-08-01 23:43:30 +0200955 hdr->rt_rate = stats->rate / 5;
956
Daniel Drakee85d0912006-06-02 17:11:32 +0100957 /* FIXME: 802.11a */
958 hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
959 _zd_chip_get_channel(&mac->chip)));
960 hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
961 ((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
962 ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
Daniel Drakee85d0912006-06-02 17:11:32 +0100963}
964
965/* Returns 1 if the data packet is for us and 0 otherwise. */
966static int is_data_packet_for_us(struct ieee80211_device *ieee,
967 struct ieee80211_hdr_4addr *hdr)
968{
969 struct net_device *netdev = ieee->dev;
970 u16 fc = le16_to_cpu(hdr->frame_ctl);
971
972 ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
973
974 switch (ieee->iw_mode) {
975 case IW_MODE_ADHOC:
976 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
977 memcmp(hdr->addr3, ieee->bssid, ETH_ALEN) != 0)
978 return 0;
979 break;
980 case IW_MODE_AUTO:
981 case IW_MODE_INFRA:
982 if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
983 IEEE80211_FCTL_FROMDS ||
984 memcmp(hdr->addr2, ieee->bssid, ETH_ALEN) != 0)
985 return 0;
986 break;
987 default:
988 ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
989 return 0;
990 }
991
992 return memcmp(hdr->addr1, netdev->dev_addr, ETH_ALEN) == 0 ||
Ulrich Kunitz9cdac962006-12-01 00:58:07 +0000993 (is_multicast_ether_addr(hdr->addr1) &&
994 memcmp(hdr->addr3, netdev->dev_addr, ETH_ALEN) != 0) ||
Daniel Drakee85d0912006-06-02 17:11:32 +0100995 (netdev->flags & IFF_PROMISC);
996}
997
Ulrich Kunitz741fec52006-11-22 00:05:53 +0000998/* Filters received packets. The function returns 1 if the packet should be
999 * forwarded to ieee80211_rx(). If the packet should be ignored the function
1000 * returns 0. If an invalid packet is found the function returns -EINVAL.
1001 *
1002 * The function calls ieee80211_rx_mgt() directly.
Daniel Drakee85d0912006-06-02 17:11:32 +01001003 *
1004 * It has been based on ieee80211_rx_any.
1005 */
1006static int filter_rx(struct ieee80211_device *ieee,
1007 const u8 *buffer, unsigned int length,
1008 struct ieee80211_rx_stats *stats)
1009{
1010 struct ieee80211_hdr_4addr *hdr;
1011 u16 fc;
1012
1013 if (ieee->iw_mode == IW_MODE_MONITOR)
1014 return 1;
1015
1016 hdr = (struct ieee80211_hdr_4addr *)buffer;
1017 fc = le16_to_cpu(hdr->frame_ctl);
1018 if ((fc & IEEE80211_FCTL_VERS) != 0)
1019 return -EINVAL;
1020
1021 switch (WLAN_FC_GET_TYPE(fc)) {
1022 case IEEE80211_FTYPE_MGMT:
1023 if (length < sizeof(struct ieee80211_hdr_3addr))
1024 return -EINVAL;
1025 ieee80211_rx_mgt(ieee, hdr, stats);
1026 return 0;
1027 case IEEE80211_FTYPE_CTL:
Daniel Drakee85d0912006-06-02 17:11:32 +01001028 return 0;
1029 case IEEE80211_FTYPE_DATA:
Ulrich Kunitz741fec52006-11-22 00:05:53 +00001030 /* Ignore invalid short buffers */
Daniel Drakee85d0912006-06-02 17:11:32 +01001031 if (length < sizeof(struct ieee80211_hdr_3addr))
1032 return -EINVAL;
1033 return is_data_packet_for_us(ieee, hdr);
1034 }
1035
1036 return -EINVAL;
1037}
1038
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001039static void update_qual_rssi(struct zd_mac *mac,
1040 const u8 *buffer, unsigned int length,
1041 u8 qual_percent, u8 rssi_percent)
Daniel Drakee85d0912006-06-02 17:11:32 +01001042{
1043 unsigned long flags;
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001044 struct ieee80211_hdr_3addr *hdr;
1045 int i;
1046
1047 hdr = (struct ieee80211_hdr_3addr *)buffer;
1048 if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
1049 return;
1050 if (memcmp(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid, ETH_ALEN) != 0)
1051 return;
Daniel Drakee85d0912006-06-02 17:11:32 +01001052
1053 spin_lock_irqsave(&mac->lock, flags);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001054 i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
1055 mac->qual_buffer[i] = qual_percent;
1056 mac->rssi_buffer[i] = rssi_percent;
1057 mac->stats_count++;
Daniel Drakee85d0912006-06-02 17:11:32 +01001058 spin_unlock_irqrestore(&mac->lock, flags);
1059}
1060
1061static int fill_rx_stats(struct ieee80211_rx_stats *stats,
1062 const struct rx_status **pstatus,
1063 struct zd_mac *mac,
1064 const u8 *buffer, unsigned int length)
1065{
1066 const struct rx_status *status;
1067
1068 *pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
1069 if (status->frame_status & ZD_RX_ERROR) {
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001070 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1071 ieee->stats.rx_errors++;
1072 if (status->frame_status & ZD_RX_TIMEOUT_ERROR)
1073 ieee->stats.rx_missed_errors++;
1074 else if (status->frame_status & ZD_RX_FIFO_OVERRUN_ERROR)
1075 ieee->stats.rx_fifo_errors++;
1076 else if (status->frame_status & ZD_RX_DECRYPTION_ERROR)
1077 ieee->ieee_stats.rx_discards_undecryptable++;
1078 else if (status->frame_status & ZD_RX_CRC32_ERROR) {
1079 ieee->stats.rx_crc_errors++;
1080 ieee->ieee_stats.rx_fcs_errors++;
1081 }
1082 else if (status->frame_status & ZD_RX_CRC16_ERROR)
1083 ieee->stats.rx_crc_errors++;
Daniel Drakee85d0912006-06-02 17:11:32 +01001084 return -EINVAL;
1085 }
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001086
Daniel Drakee85d0912006-06-02 17:11:32 +01001087 memset(stats, 0, sizeof(struct ieee80211_rx_stats));
1088 stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
1089 + sizeof(struct rx_status));
1090 /* FIXME: 802.11a */
1091 stats->freq = IEEE80211_24GHZ_BAND;
1092 stats->received_channel = _zd_chip_get_channel(&mac->chip);
1093 stats->rssi = zd_rx_strength_percent(status->signal_strength);
1094 stats->signal = zd_rx_qual_percent(buffer,
1095 length - sizeof(struct rx_status),
1096 status);
1097 stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
1098 stats->rate = zd_rx_rate(buffer, status);
1099 if (stats->rate)
1100 stats->mask |= IEEE80211_STATMASK_RATE;
1101
Daniel Drakee85d0912006-06-02 17:11:32 +01001102 return 0;
1103}
1104
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001105static void zd_mac_rx(struct zd_mac *mac, struct sk_buff *skb)
Daniel Drakee85d0912006-06-02 17:11:32 +01001106{
1107 int r;
1108 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
1109 struct ieee80211_rx_stats stats;
1110 const struct rx_status *status;
Daniel Drakee85d0912006-06-02 17:11:32 +01001111
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001112 if (skb->len < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
1113 IEEE80211_FCS_LEN + sizeof(struct rx_status))
1114 {
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001115 ieee->stats.rx_errors++;
1116 ieee->stats.rx_length_errors++;
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001117 dev_dbg_f(zd_mac_dev(mac), "Packet with length %u to small.\n",
1118 skb->len);
1119 goto free_skb;
1120 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001121
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001122 r = fill_rx_stats(&stats, &status, mac, skb->data, skb->len);
1123 if (r) {
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001124 /* Only packets with rx errors are included here.
1125 * The error stats have already been set in fill_rx_stats.
1126 */
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001127 goto free_skb;
1128 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001129
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001130 __skb_pull(skb, ZD_PLCP_HEADER_SIZE);
1131 __skb_trim(skb, skb->len -
1132 (IEEE80211_FCS_LEN + sizeof(struct rx_status)));
Daniel Drakee85d0912006-06-02 17:11:32 +01001133
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001134 update_qual_rssi(mac, skb->data, skb->len, stats.signal,
1135 status->signal_strength);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001136
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001137 r = filter_rx(ieee, skb->data, skb->len, &stats);
1138 if (r <= 0) {
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001139 if (r < 0) {
1140 ieee->stats.rx_errors++;
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001141 dev_dbg_f(zd_mac_dev(mac), "Error in packet.\n");
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001142 }
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001143 goto free_skb;
1144 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001145
Daniel Drakee85d0912006-06-02 17:11:32 +01001146 if (ieee->iw_mode == IW_MODE_MONITOR)
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001147 fill_rt_header(skb_push(skb, sizeof(struct zd_rt_hdr)), mac,
Daniel Drakee85d0912006-06-02 17:11:32 +01001148 &stats, status);
Daniel Drakee85d0912006-06-02 17:11:32 +01001149
1150 r = ieee80211_rx(ieee, skb, &stats);
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001151 if (r)
1152 return;
1153free_skb:
1154 /* We are always in a soft irq. */
1155 dev_kfree_skb(skb);
1156}
1157
1158static void do_rx(unsigned long mac_ptr)
1159{
1160 struct zd_mac *mac = (struct zd_mac *)mac_ptr;
1161 struct sk_buff *skb;
1162
1163 while ((skb = skb_dequeue(&mac->rx_queue)) != NULL)
1164 zd_mac_rx(mac, skb);
1165}
1166
1167int zd_mac_rx_irq(struct zd_mac *mac, const u8 *buffer, unsigned int length)
1168{
1169 struct sk_buff *skb;
1170
1171 skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
1172 if (!skb) {
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001173 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001174 dev_warn(zd_mac_dev(mac), "Could not allocate skb.\n");
Ulrich Kunitz22d34052007-01-29 01:00:03 +00001175 ieee->stats.rx_dropped++;
Ulrich Kunitz4d1feab2006-12-10 11:13:12 -08001176 return -ENOMEM;
1177 }
1178 skb_reserve(skb, sizeof(struct zd_rt_hdr));
1179 memcpy(__skb_put(skb, length), buffer, length);
1180 skb_queue_tail(&mac->rx_queue, skb);
1181 tasklet_schedule(&mac->rx_tasklet);
Daniel Drakee85d0912006-06-02 17:11:32 +01001182 return 0;
1183}
1184
1185static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
1186 int pri)
1187{
1188 return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
1189}
1190
1191static void set_security(struct net_device *netdev,
1192 struct ieee80211_security *sec)
1193{
1194 struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
1195 struct ieee80211_security *secinfo = &ieee->sec;
1196 int keyidx;
1197
1198 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
1199
1200 for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
1201 if (sec->flags & (1<<keyidx)) {
1202 secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
1203 secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
1204 memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
1205 SCM_KEY_LEN);
1206 }
1207
1208 if (sec->flags & SEC_ACTIVE_KEY) {
1209 secinfo->active_key = sec->active_key;
1210 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1211 " .active_key = %d\n", sec->active_key);
1212 }
1213 if (sec->flags & SEC_UNICAST_GROUP) {
1214 secinfo->unicast_uses_group = sec->unicast_uses_group;
1215 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1216 " .unicast_uses_group = %d\n",
1217 sec->unicast_uses_group);
1218 }
1219 if (sec->flags & SEC_LEVEL) {
1220 secinfo->level = sec->level;
1221 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1222 " .level = %d\n", sec->level);
1223 }
1224 if (sec->flags & SEC_ENABLED) {
1225 secinfo->enabled = sec->enabled;
1226 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1227 " .enabled = %d\n", sec->enabled);
1228 }
1229 if (sec->flags & SEC_ENCRYPT) {
1230 secinfo->encrypt = sec->encrypt;
1231 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1232 " .encrypt = %d\n", sec->encrypt);
1233 }
1234 if (sec->flags & SEC_AUTH_MODE) {
1235 secinfo->auth_mode = sec->auth_mode;
1236 dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
1237 " .auth_mode = %d\n", sec->auth_mode);
1238 }
1239}
1240
1241static void ieee_init(struct ieee80211_device *ieee)
1242{
1243 ieee->mode = IEEE_B | IEEE_G;
1244 ieee->freq_band = IEEE80211_24GHZ_BAND;
1245 ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
1246 ieee->tx_headroom = sizeof(struct zd_ctrlset);
1247 ieee->set_security = set_security;
1248 ieee->hard_start_xmit = netdev_tx;
1249
1250 /* Software encryption/decryption for now */
1251 ieee->host_build_iv = 0;
1252 ieee->host_encrypt = 1;
1253 ieee->host_decrypt = 1;
1254
1255 /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
1256 * correctly support AUTO */
1257 ieee->iw_mode = IW_MODE_INFRA;
1258}
1259
1260static void softmac_init(struct ieee80211softmac_device *sm)
1261{
1262 sm->set_channel = set_channel;
Daniel Drakeb1382ed2006-11-22 00:06:48 +00001263 sm->bssinfo_change = bssinfo_change;
Daniel Drakee85d0912006-06-02 17:11:32 +01001264}
1265
1266struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
1267{
1268 struct zd_mac *mac = zd_netdev_mac(ndev);
1269 struct iw_statistics *iw_stats = &mac->iw_stats;
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001270 unsigned int i, count, qual_total, rssi_total;
Daniel Drakee85d0912006-06-02 17:11:32 +01001271
1272 memset(iw_stats, 0, sizeof(struct iw_statistics));
1273 /* We are not setting the status, because ieee->state is not updated
1274 * at all and this driver doesn't track authentication state.
1275 */
1276 spin_lock_irq(&mac->lock);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001277 count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
1278 mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
1279 qual_total = rssi_total = 0;
1280 for (i = 0; i < count; i++) {
1281 qual_total += mac->qual_buffer[i];
1282 rssi_total += mac->rssi_buffer[i];
1283 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001284 spin_unlock_irq(&mac->lock);
Ulrich Kunitzdb888ae2006-08-29 23:50:29 +01001285 iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
1286 if (count > 0) {
1287 iw_stats->qual.qual = qual_total / count;
1288 iw_stats->qual.level = rssi_total / count;
1289 iw_stats->qual.updated |=
1290 IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
1291 } else {
1292 iw_stats->qual.updated |=
1293 IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
1294 }
Daniel Drakee85d0912006-06-02 17:11:32 +01001295 /* TODO: update counter */
1296 return iw_stats;
1297}
1298
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001299#define LINK_LED_WORK_DELAY HZ
1300
David Howellsc4028952006-11-22 14:57:56 +00001301static void link_led_handler(struct work_struct *work)
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001302{
David Howellsc4028952006-11-22 14:57:56 +00001303 struct zd_mac *mac =
1304 container_of(work, struct zd_mac, housekeeping.link_led_work.work);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001305 struct zd_chip *chip = &mac->chip;
1306 struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
1307 int is_associated;
1308 int r;
1309
1310 spin_lock_irq(&mac->lock);
John W. Linville41072a12006-10-17 13:47:40 -04001311 is_associated = sm->associnfo.associated != 0;
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001312 spin_unlock_irq(&mac->lock);
1313
1314 r = zd_chip_control_leds(chip,
1315 is_associated ? LED_ASSOCIATED : LED_SCANNING);
1316 if (r)
1317 dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1318
1319 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1320 LINK_LED_WORK_DELAY);
1321}
1322
1323static void housekeeping_init(struct zd_mac *mac)
1324{
David Howellsc4028952006-11-22 14:57:56 +00001325 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
Ulrich Kunitz583afd1e2006-09-13 02:42:38 +01001326}
1327
1328static void housekeeping_enable(struct zd_mac *mac)
1329{
1330 dev_dbg_f(zd_mac_dev(mac), "\n");
1331 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1332 0);
1333}
1334
1335static void housekeeping_disable(struct zd_mac *mac)
1336{
1337 dev_dbg_f(zd_mac_dev(mac), "\n");
1338 cancel_rearming_delayed_workqueue(zd_workqueue,
1339 &mac->housekeeping.link_led_work);
1340 zd_chip_control_leds(&mac->chip, LED_OFF);
1341}