i2c: gpio: fault-injector: refactor incomplete transfer
[linux/fpc-iii.git] / drivers / net / wireless / intersil / hostap / hostap_main.c
blob012930d354344633e7f4f3f1e175e6d86ba9066c
1 /*
2 * Host AP (software wireless LAN access point) driver for
3 * Intersil Prism2/2.5/3 - hostap.o module, common routines
5 * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
6 * <j@w1.fi>
7 * Copyright (c) 2002-2005, Jouni Malinen <j@w1.fi>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation. See README and COPYING for
12 * more details.
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/proc_fs.h>
19 #include <linux/if_arp.h>
20 #include <linux/delay.h>
21 #include <linux/random.h>
22 #include <linux/workqueue.h>
23 #include <linux/kmod.h>
24 #include <linux/rtnetlink.h>
25 #include <linux/wireless.h>
26 #include <linux/etherdevice.h>
27 #include <net/net_namespace.h>
28 #include <net/iw_handler.h>
29 #include <net/lib80211.h>
30 #include <linux/uaccess.h>
32 #include "hostap_wlan.h"
33 #include "hostap_80211.h"
34 #include "hostap_ap.h"
35 #include "hostap.h"
37 MODULE_AUTHOR("Jouni Malinen");
38 MODULE_DESCRIPTION("Host AP common routines");
39 MODULE_LICENSE("GPL");
41 #define TX_TIMEOUT (2 * HZ)
43 #define PRISM2_MAX_FRAME_SIZE 2304
44 #define PRISM2_MIN_MTU 256
45 /* FIX: */
46 #define PRISM2_MAX_MTU (PRISM2_MAX_FRAME_SIZE - (6 /* LLC */ + 8 /* WEP */))
49 struct net_device * hostap_add_interface(struct local_info *local,
50 int type, int rtnl_locked,
51 const char *prefix,
52 const char *name)
54 struct net_device *dev, *mdev;
55 struct hostap_interface *iface;
56 int ret;
58 dev = alloc_etherdev(sizeof(struct hostap_interface));
59 if (dev == NULL)
60 return NULL;
62 iface = netdev_priv(dev);
63 iface->dev = dev;
64 iface->local = local;
65 iface->type = type;
66 list_add(&iface->list, &local->hostap_interfaces);
68 mdev = local->dev;
69 eth_hw_addr_inherit(dev, mdev);
70 dev->base_addr = mdev->base_addr;
71 dev->irq = mdev->irq;
72 dev->mem_start = mdev->mem_start;
73 dev->mem_end = mdev->mem_end;
75 hostap_setup_dev(dev, local, type);
76 dev->needs_free_netdev = true;
78 sprintf(dev->name, "%s%s", prefix, name);
79 if (!rtnl_locked)
80 rtnl_lock();
82 SET_NETDEV_DEV(dev, mdev->dev.parent);
83 ret = register_netdevice(dev);
85 if (!rtnl_locked)
86 rtnl_unlock();
88 if (ret < 0) {
89 printk(KERN_WARNING "%s: failed to add new netdevice!\n",
90 dev->name);
91 free_netdev(dev);
92 return NULL;
95 printk(KERN_DEBUG "%s: registered netdevice %s\n",
96 mdev->name, dev->name);
98 return dev;
102 void hostap_remove_interface(struct net_device *dev, int rtnl_locked,
103 int remove_from_list)
105 struct hostap_interface *iface;
107 if (!dev)
108 return;
110 iface = netdev_priv(dev);
112 if (remove_from_list) {
113 list_del(&iface->list);
116 if (dev == iface->local->ddev)
117 iface->local->ddev = NULL;
118 else if (dev == iface->local->apdev)
119 iface->local->apdev = NULL;
120 else if (dev == iface->local->stadev)
121 iface->local->stadev = NULL;
123 if (rtnl_locked)
124 unregister_netdevice(dev);
125 else
126 unregister_netdev(dev);
128 /* 'dev->needs_free_netdev = true' implies device data, including
129 * private data, will be freed when the device is removed */
133 static inline int prism2_wds_special_addr(u8 *addr)
135 if (addr[0] || addr[1] || addr[2] || addr[3] || addr[4] || addr[5])
136 return 0;
138 return 1;
142 int prism2_wds_add(local_info_t *local, u8 *remote_addr,
143 int rtnl_locked)
145 struct net_device *dev;
146 struct list_head *ptr;
147 struct hostap_interface *iface, *empty, *match;
149 empty = match = NULL;
150 read_lock_bh(&local->iface_lock);
151 list_for_each(ptr, &local->hostap_interfaces) {
152 iface = list_entry(ptr, struct hostap_interface, list);
153 if (iface->type != HOSTAP_INTERFACE_WDS)
154 continue;
156 if (prism2_wds_special_addr(iface->u.wds.remote_addr))
157 empty = iface;
158 else if (ether_addr_equal(iface->u.wds.remote_addr, remote_addr)) {
159 match = iface;
160 break;
163 if (!match && empty && !prism2_wds_special_addr(remote_addr)) {
164 /* take pre-allocated entry into use */
165 memcpy(empty->u.wds.remote_addr, remote_addr, ETH_ALEN);
166 read_unlock_bh(&local->iface_lock);
167 printk(KERN_DEBUG "%s: using pre-allocated WDS netdevice %s\n",
168 local->dev->name, empty->dev->name);
169 return 0;
171 read_unlock_bh(&local->iface_lock);
173 if (!prism2_wds_special_addr(remote_addr)) {
174 if (match)
175 return -EEXIST;
176 hostap_add_sta(local->ap, remote_addr);
179 if (local->wds_connections >= local->wds_max_connections)
180 return -ENOBUFS;
182 /* verify that there is room for wds# postfix in the interface name */
183 if (strlen(local->dev->name) >= IFNAMSIZ - 5) {
184 printk(KERN_DEBUG "'%s' too long base device name\n",
185 local->dev->name);
186 return -EINVAL;
189 dev = hostap_add_interface(local, HOSTAP_INTERFACE_WDS, rtnl_locked,
190 local->ddev->name, "wds%d");
191 if (dev == NULL)
192 return -ENOMEM;
194 iface = netdev_priv(dev);
195 memcpy(iface->u.wds.remote_addr, remote_addr, ETH_ALEN);
197 local->wds_connections++;
199 return 0;
203 int prism2_wds_del(local_info_t *local, u8 *remote_addr,
204 int rtnl_locked, int do_not_remove)
206 unsigned long flags;
207 struct list_head *ptr;
208 struct hostap_interface *iface, *selected = NULL;
210 write_lock_irqsave(&local->iface_lock, flags);
211 list_for_each(ptr, &local->hostap_interfaces) {
212 iface = list_entry(ptr, struct hostap_interface, list);
213 if (iface->type != HOSTAP_INTERFACE_WDS)
214 continue;
216 if (ether_addr_equal(iface->u.wds.remote_addr, remote_addr)) {
217 selected = iface;
218 break;
221 if (selected && !do_not_remove)
222 list_del(&selected->list);
223 write_unlock_irqrestore(&local->iface_lock, flags);
225 if (selected) {
226 if (do_not_remove)
227 eth_zero_addr(selected->u.wds.remote_addr);
228 else {
229 hostap_remove_interface(selected->dev, rtnl_locked, 0);
230 local->wds_connections--;
234 return selected ? 0 : -ENODEV;
238 u16 hostap_tx_callback_register(local_info_t *local,
239 void (*func)(struct sk_buff *, int ok, void *),
240 void *data)
242 unsigned long flags;
243 struct hostap_tx_callback_info *entry;
245 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
246 if (entry == NULL)
247 return 0;
249 entry->func = func;
250 entry->data = data;
252 spin_lock_irqsave(&local->lock, flags);
253 entry->idx = local->tx_callback ? local->tx_callback->idx + 1 : 1;
254 entry->next = local->tx_callback;
255 local->tx_callback = entry;
256 spin_unlock_irqrestore(&local->lock, flags);
258 return entry->idx;
262 int hostap_tx_callback_unregister(local_info_t *local, u16 idx)
264 unsigned long flags;
265 struct hostap_tx_callback_info *cb, *prev = NULL;
267 spin_lock_irqsave(&local->lock, flags);
268 cb = local->tx_callback;
269 while (cb != NULL && cb->idx != idx) {
270 prev = cb;
271 cb = cb->next;
273 if (cb) {
274 if (prev == NULL)
275 local->tx_callback = cb->next;
276 else
277 prev->next = cb->next;
278 kfree(cb);
280 spin_unlock_irqrestore(&local->lock, flags);
282 return cb ? 0 : -1;
286 /* val is in host byte order */
287 int hostap_set_word(struct net_device *dev, int rid, u16 val)
289 struct hostap_interface *iface;
290 __le16 tmp = cpu_to_le16(val);
291 iface = netdev_priv(dev);
292 return iface->local->func->set_rid(dev, rid, &tmp, 2);
296 int hostap_set_string(struct net_device *dev, int rid, const char *val)
298 struct hostap_interface *iface;
299 char buf[MAX_SSID_LEN + 2];
300 int len;
302 iface = netdev_priv(dev);
303 len = strlen(val);
304 if (len > MAX_SSID_LEN)
305 return -1;
306 memset(buf, 0, sizeof(buf));
307 buf[0] = len; /* little endian 16 bit word */
308 memcpy(buf + 2, val, len);
310 return iface->local->func->set_rid(dev, rid, &buf, MAX_SSID_LEN + 2);
314 u16 hostap_get_porttype(local_info_t *local)
316 if (local->iw_mode == IW_MODE_ADHOC && local->pseudo_adhoc)
317 return HFA384X_PORTTYPE_PSEUDO_IBSS;
318 if (local->iw_mode == IW_MODE_ADHOC)
319 return HFA384X_PORTTYPE_IBSS;
320 if (local->iw_mode == IW_MODE_INFRA)
321 return HFA384X_PORTTYPE_BSS;
322 if (local->iw_mode == IW_MODE_REPEAT)
323 return HFA384X_PORTTYPE_WDS;
324 if (local->iw_mode == IW_MODE_MONITOR)
325 return HFA384X_PORTTYPE_PSEUDO_IBSS;
326 return HFA384X_PORTTYPE_HOSTAP;
330 int hostap_set_encryption(local_info_t *local)
332 u16 val, old_val;
333 int i, keylen, len, idx;
334 char keybuf[WEP_KEY_LEN + 1];
335 enum { NONE, WEP, OTHER } encrypt_type;
337 idx = local->crypt_info.tx_keyidx;
338 if (local->crypt_info.crypt[idx] == NULL ||
339 local->crypt_info.crypt[idx]->ops == NULL)
340 encrypt_type = NONE;
341 else if (strcmp(local->crypt_info.crypt[idx]->ops->name, "WEP") == 0)
342 encrypt_type = WEP;
343 else
344 encrypt_type = OTHER;
346 if (local->func->get_rid(local->dev, HFA384X_RID_CNFWEPFLAGS, &val, 2,
347 1) < 0) {
348 printk(KERN_DEBUG "Could not read current WEP flags.\n");
349 goto fail;
351 le16_to_cpus(&val);
352 old_val = val;
354 if (encrypt_type != NONE || local->privacy_invoked)
355 val |= HFA384X_WEPFLAGS_PRIVACYINVOKED;
356 else
357 val &= ~HFA384X_WEPFLAGS_PRIVACYINVOKED;
359 if (local->open_wep || encrypt_type == NONE ||
360 ((local->ieee_802_1x || local->wpa) && local->host_decrypt))
361 val &= ~HFA384X_WEPFLAGS_EXCLUDEUNENCRYPTED;
362 else
363 val |= HFA384X_WEPFLAGS_EXCLUDEUNENCRYPTED;
365 if ((encrypt_type != NONE || local->privacy_invoked) &&
366 (encrypt_type == OTHER || local->host_encrypt))
367 val |= HFA384X_WEPFLAGS_HOSTENCRYPT;
368 else
369 val &= ~HFA384X_WEPFLAGS_HOSTENCRYPT;
370 if ((encrypt_type != NONE || local->privacy_invoked) &&
371 (encrypt_type == OTHER || local->host_decrypt))
372 val |= HFA384X_WEPFLAGS_HOSTDECRYPT;
373 else
374 val &= ~HFA384X_WEPFLAGS_HOSTDECRYPT;
376 if (val != old_val &&
377 hostap_set_word(local->dev, HFA384X_RID_CNFWEPFLAGS, val)) {
378 printk(KERN_DEBUG "Could not write new WEP flags (0x%x)\n",
379 val);
380 goto fail;
383 if (encrypt_type != WEP)
384 return 0;
386 /* 104-bit support seems to require that all the keys are set to the
387 * same keylen */
388 keylen = 6; /* first 5 octets */
389 len = local->crypt_info.crypt[idx]->ops->get_key(keybuf, sizeof(keybuf), NULL,
390 local->crypt_info.crypt[idx]->priv);
391 if (idx >= 0 && idx < WEP_KEYS && len > 5)
392 keylen = WEP_KEY_LEN + 1; /* first 13 octets */
394 for (i = 0; i < WEP_KEYS; i++) {
395 memset(keybuf, 0, sizeof(keybuf));
396 if (local->crypt_info.crypt[i]) {
397 (void) local->crypt_info.crypt[i]->ops->get_key(
398 keybuf, sizeof(keybuf),
399 NULL, local->crypt_info.crypt[i]->priv);
401 if (local->func->set_rid(local->dev,
402 HFA384X_RID_CNFDEFAULTKEY0 + i,
403 keybuf, keylen)) {
404 printk(KERN_DEBUG "Could not set key %d (len=%d)\n",
405 i, keylen);
406 goto fail;
409 if (hostap_set_word(local->dev, HFA384X_RID_CNFWEPDEFAULTKEYID, idx)) {
410 printk(KERN_DEBUG "Could not set default keyid %d\n", idx);
411 goto fail;
414 return 0;
416 fail:
417 printk(KERN_DEBUG "%s: encryption setup failed\n", local->dev->name);
418 return -1;
422 int hostap_set_antsel(local_info_t *local)
424 u16 val;
425 int ret = 0;
427 if (local->antsel_tx != HOSTAP_ANTSEL_DO_NOT_TOUCH &&
428 local->func->cmd(local->dev, HFA384X_CMDCODE_READMIF,
429 HFA386X_CR_TX_CONFIGURE,
430 NULL, &val) == 0) {
431 val &= ~(BIT(2) | BIT(1));
432 switch (local->antsel_tx) {
433 case HOSTAP_ANTSEL_DIVERSITY:
434 val |= BIT(1);
435 break;
436 case HOSTAP_ANTSEL_LOW:
437 break;
438 case HOSTAP_ANTSEL_HIGH:
439 val |= BIT(2);
440 break;
443 if (local->func->cmd(local->dev, HFA384X_CMDCODE_WRITEMIF,
444 HFA386X_CR_TX_CONFIGURE, &val, NULL)) {
445 printk(KERN_INFO "%s: setting TX AntSel failed\n",
446 local->dev->name);
447 ret = -1;
451 if (local->antsel_rx != HOSTAP_ANTSEL_DO_NOT_TOUCH &&
452 local->func->cmd(local->dev, HFA384X_CMDCODE_READMIF,
453 HFA386X_CR_RX_CONFIGURE,
454 NULL, &val) == 0) {
455 val &= ~(BIT(1) | BIT(0));
456 switch (local->antsel_rx) {
457 case HOSTAP_ANTSEL_DIVERSITY:
458 break;
459 case HOSTAP_ANTSEL_LOW:
460 val |= BIT(0);
461 break;
462 case HOSTAP_ANTSEL_HIGH:
463 val |= BIT(0) | BIT(1);
464 break;
467 if (local->func->cmd(local->dev, HFA384X_CMDCODE_WRITEMIF,
468 HFA386X_CR_RX_CONFIGURE, &val, NULL)) {
469 printk(KERN_INFO "%s: setting RX AntSel failed\n",
470 local->dev->name);
471 ret = -1;
475 return ret;
479 int hostap_set_roaming(local_info_t *local)
481 u16 val;
483 switch (local->host_roaming) {
484 case 1:
485 val = HFA384X_ROAMING_HOST;
486 break;
487 case 2:
488 val = HFA384X_ROAMING_DISABLED;
489 break;
490 case 0:
491 default:
492 val = HFA384X_ROAMING_FIRMWARE;
493 break;
496 return hostap_set_word(local->dev, HFA384X_RID_CNFROAMINGMODE, val);
500 int hostap_set_auth_algs(local_info_t *local)
502 int val = local->auth_algs;
503 /* At least STA f/w v0.6.2 seems to have issues with cnfAuthentication
504 * set to include both Open and Shared Key flags. It tries to use
505 * Shared Key authentication in that case even if WEP keys are not
506 * configured.. STA f/w v0.7.6 is able to handle such configuration,
507 * but it is unknown when this was fixed between 0.6.2 .. 0.7.6. */
508 if (local->sta_fw_ver < PRISM2_FW_VER(0,7,0) &&
509 val != PRISM2_AUTH_OPEN && val != PRISM2_AUTH_SHARED_KEY)
510 val = PRISM2_AUTH_OPEN;
512 if (hostap_set_word(local->dev, HFA384X_RID_CNFAUTHENTICATION, val)) {
513 printk(KERN_INFO "%s: cnfAuthentication setting to 0x%x "
514 "failed\n", local->dev->name, local->auth_algs);
515 return -EINVAL;
518 return 0;
522 void hostap_dump_rx_header(const char *name, const struct hfa384x_rx_frame *rx)
524 u16 status, fc;
526 status = __le16_to_cpu(rx->status);
528 printk(KERN_DEBUG "%s: RX status=0x%04x (port=%d, type=%d, "
529 "fcserr=%d) silence=%d signal=%d rate=%d rxflow=%d; "
530 "jiffies=%ld\n",
531 name, status, (status >> 8) & 0x07, status >> 13, status & 1,
532 rx->silence, rx->signal, rx->rate, rx->rxflow, jiffies);
534 fc = __le16_to_cpu(rx->frame_control);
535 printk(KERN_DEBUG " FC=0x%04x (type=%d:%d) dur=0x%04x seq=0x%04x "
536 "data_len=%d%s%s\n",
537 fc, (fc & IEEE80211_FCTL_FTYPE) >> 2,
538 (fc & IEEE80211_FCTL_STYPE) >> 4,
539 __le16_to_cpu(rx->duration_id), __le16_to_cpu(rx->seq_ctrl),
540 __le16_to_cpu(rx->data_len),
541 fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
542 fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
544 printk(KERN_DEBUG " A1=%pM A2=%pM A3=%pM A4=%pM\n",
545 rx->addr1, rx->addr2, rx->addr3, rx->addr4);
547 printk(KERN_DEBUG " dst=%pM src=%pM len=%d\n",
548 rx->dst_addr, rx->src_addr,
549 __be16_to_cpu(rx->len));
553 void hostap_dump_tx_header(const char *name, const struct hfa384x_tx_frame *tx)
555 u16 fc;
557 printk(KERN_DEBUG "%s: TX status=0x%04x retry_count=%d tx_rate=%d "
558 "tx_control=0x%04x; jiffies=%ld\n",
559 name, __le16_to_cpu(tx->status), tx->retry_count, tx->tx_rate,
560 __le16_to_cpu(tx->tx_control), jiffies);
562 fc = __le16_to_cpu(tx->frame_control);
563 printk(KERN_DEBUG " FC=0x%04x (type=%d:%d) dur=0x%04x seq=0x%04x "
564 "data_len=%d%s%s\n",
565 fc, (fc & IEEE80211_FCTL_FTYPE) >> 2,
566 (fc & IEEE80211_FCTL_STYPE) >> 4,
567 __le16_to_cpu(tx->duration_id), __le16_to_cpu(tx->seq_ctrl),
568 __le16_to_cpu(tx->data_len),
569 fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
570 fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
572 printk(KERN_DEBUG " A1=%pM A2=%pM A3=%pM A4=%pM\n",
573 tx->addr1, tx->addr2, tx->addr3, tx->addr4);
575 printk(KERN_DEBUG " dst=%pM src=%pM len=%d\n",
576 tx->dst_addr, tx->src_addr,
577 __be16_to_cpu(tx->len));
581 static int hostap_80211_header_parse(const struct sk_buff *skb,
582 unsigned char *haddr)
584 memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
585 return ETH_ALEN;
589 int hostap_80211_get_hdrlen(__le16 fc)
591 if (ieee80211_is_data(fc) && ieee80211_has_a4 (fc))
592 return 30; /* Addr4 */
593 else if (ieee80211_is_cts(fc) || ieee80211_is_ack(fc))
594 return 10;
595 else if (ieee80211_is_ctl(fc))
596 return 16;
598 return 24;
602 static int prism2_close(struct net_device *dev)
604 struct hostap_interface *iface;
605 local_info_t *local;
607 PDEBUG(DEBUG_FLOW, "%s: prism2_close\n", dev->name);
609 iface = netdev_priv(dev);
610 local = iface->local;
612 if (dev == local->ddev) {
613 prism2_sta_deauth(local, WLAN_REASON_DEAUTH_LEAVING);
615 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
616 if (!local->hostapd && dev == local->dev &&
617 (!local->func->card_present || local->func->card_present(local)) &&
618 local->hw_ready && local->ap && local->iw_mode == IW_MODE_MASTER)
619 hostap_deauth_all_stas(dev, local->ap, 1);
620 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
622 if (dev == local->dev) {
623 local->func->hw_shutdown(dev, HOSTAP_HW_ENABLE_CMDCOMPL);
626 if (netif_running(dev)) {
627 netif_stop_queue(dev);
628 netif_device_detach(dev);
631 cancel_work_sync(&local->reset_queue);
632 cancel_work_sync(&local->set_multicast_list_queue);
633 cancel_work_sync(&local->set_tim_queue);
634 #ifndef PRISM2_NO_STATION_MODES
635 cancel_work_sync(&local->info_queue);
636 #endif
637 cancel_work_sync(&local->comms_qual_update);
639 module_put(local->hw_module);
641 local->num_dev_open--;
643 if (dev != local->dev && local->dev->flags & IFF_UP &&
644 local->master_dev_auto_open && local->num_dev_open == 1) {
645 /* Close master radio interface automatically if it was also
646 * opened automatically and we are now closing the last
647 * remaining non-master device. */
648 dev_close(local->dev);
651 return 0;
655 static int prism2_open(struct net_device *dev)
657 struct hostap_interface *iface;
658 local_info_t *local;
660 PDEBUG(DEBUG_FLOW, "%s: prism2_open\n", dev->name);
662 iface = netdev_priv(dev);
663 local = iface->local;
665 if (local->no_pri) {
666 printk(KERN_DEBUG "%s: could not set interface UP - no PRI "
667 "f/w\n", dev->name);
668 return -ENODEV;
671 if ((local->func->card_present && !local->func->card_present(local)) ||
672 local->hw_downloading)
673 return -ENODEV;
675 if (!try_module_get(local->hw_module))
676 return -ENODEV;
677 local->num_dev_open++;
679 if (!local->dev_enabled && local->func->hw_enable(dev, 1)) {
680 printk(KERN_WARNING "%s: could not enable MAC port\n",
681 dev->name);
682 prism2_close(dev);
683 return -ENODEV;
685 if (!local->dev_enabled)
686 prism2_callback(local, PRISM2_CALLBACK_ENABLE);
687 local->dev_enabled = 1;
689 if (dev != local->dev && !(local->dev->flags & IFF_UP)) {
690 /* Master radio interface is needed for all operation, so open
691 * it automatically when any virtual net_device is opened. */
692 local->master_dev_auto_open = 1;
693 dev_open(local->dev);
696 netif_device_attach(dev);
697 netif_start_queue(dev);
699 return 0;
703 static int prism2_set_mac_address(struct net_device *dev, void *p)
705 struct hostap_interface *iface;
706 local_info_t *local;
707 struct list_head *ptr;
708 struct sockaddr *addr = p;
710 iface = netdev_priv(dev);
711 local = iface->local;
713 if (local->func->set_rid(dev, HFA384X_RID_CNFOWNMACADDR, addr->sa_data,
714 ETH_ALEN) < 0 || local->func->reset_port(dev))
715 return -EINVAL;
717 read_lock_bh(&local->iface_lock);
718 list_for_each(ptr, &local->hostap_interfaces) {
719 iface = list_entry(ptr, struct hostap_interface, list);
720 memcpy(iface->dev->dev_addr, addr->sa_data, ETH_ALEN);
722 memcpy(local->dev->dev_addr, addr->sa_data, ETH_ALEN);
723 read_unlock_bh(&local->iface_lock);
725 return 0;
729 /* TODO: to be further implemented as soon as Prism2 fully supports
730 * GroupAddresses and correct documentation is available */
731 void hostap_set_multicast_list_queue(struct work_struct *work)
733 local_info_t *local =
734 container_of(work, local_info_t, set_multicast_list_queue);
735 struct net_device *dev = local->dev;
737 if (hostap_set_word(dev, HFA384X_RID_PROMISCUOUSMODE,
738 local->is_promisc)) {
739 printk(KERN_INFO "%s: %sabling promiscuous mode failed\n",
740 dev->name, local->is_promisc ? "en" : "dis");
745 static void hostap_set_multicast_list(struct net_device *dev)
747 #if 0
748 /* FIX: promiscuous mode seems to be causing a lot of problems with
749 * some station firmware versions (FCSErr frames, invalid MACPort, etc.
750 * corrupted incoming frames). This code is now commented out while the
751 * problems are investigated. */
752 struct hostap_interface *iface;
753 local_info_t *local;
755 iface = netdev_priv(dev);
756 local = iface->local;
757 if ((dev->flags & IFF_ALLMULTI) || (dev->flags & IFF_PROMISC)) {
758 local->is_promisc = 1;
759 } else {
760 local->is_promisc = 0;
763 schedule_work(&local->set_multicast_list_queue);
764 #endif
768 static void prism2_tx_timeout(struct net_device *dev)
770 struct hostap_interface *iface;
771 local_info_t *local;
772 struct hfa384x_regs regs;
774 iface = netdev_priv(dev);
775 local = iface->local;
777 printk(KERN_WARNING "%s Tx timed out! Resetting card\n", dev->name);
778 netif_stop_queue(local->dev);
780 local->func->read_regs(dev, &regs);
781 printk(KERN_DEBUG "%s: CMD=%04x EVSTAT=%04x "
782 "OFFSET0=%04x OFFSET1=%04x SWSUPPORT0=%04x\n",
783 dev->name, regs.cmd, regs.evstat, regs.offset0, regs.offset1,
784 regs.swsupport0);
786 local->func->schedule_reset(local);
789 const struct header_ops hostap_80211_ops = {
790 .create = eth_header,
791 .cache = eth_header_cache,
792 .cache_update = eth_header_cache_update,
793 .parse = hostap_80211_header_parse,
795 EXPORT_SYMBOL(hostap_80211_ops);
798 static const struct net_device_ops hostap_netdev_ops = {
799 .ndo_start_xmit = hostap_data_start_xmit,
801 .ndo_open = prism2_open,
802 .ndo_stop = prism2_close,
803 .ndo_do_ioctl = hostap_ioctl,
804 .ndo_set_mac_address = prism2_set_mac_address,
805 .ndo_set_rx_mode = hostap_set_multicast_list,
806 .ndo_tx_timeout = prism2_tx_timeout,
807 .ndo_validate_addr = eth_validate_addr,
810 static const struct net_device_ops hostap_mgmt_netdev_ops = {
811 .ndo_start_xmit = hostap_mgmt_start_xmit,
813 .ndo_open = prism2_open,
814 .ndo_stop = prism2_close,
815 .ndo_do_ioctl = hostap_ioctl,
816 .ndo_set_mac_address = prism2_set_mac_address,
817 .ndo_set_rx_mode = hostap_set_multicast_list,
818 .ndo_tx_timeout = prism2_tx_timeout,
819 .ndo_validate_addr = eth_validate_addr,
822 static const struct net_device_ops hostap_master_ops = {
823 .ndo_start_xmit = hostap_master_start_xmit,
825 .ndo_open = prism2_open,
826 .ndo_stop = prism2_close,
827 .ndo_do_ioctl = hostap_ioctl,
828 .ndo_set_mac_address = prism2_set_mac_address,
829 .ndo_set_rx_mode = hostap_set_multicast_list,
830 .ndo_tx_timeout = prism2_tx_timeout,
831 .ndo_validate_addr = eth_validate_addr,
834 void hostap_setup_dev(struct net_device *dev, local_info_t *local,
835 int type)
837 struct hostap_interface *iface;
839 iface = netdev_priv(dev);
840 ether_setup(dev);
841 dev->min_mtu = PRISM2_MIN_MTU;
842 dev->max_mtu = PRISM2_MAX_MTU;
843 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
845 /* kernel callbacks */
846 if (iface) {
847 /* Currently, we point to the proper spy_data only on
848 * the main_dev. This could be fixed. Jean II */
849 iface->wireless_data.spy_data = &iface->spy_data;
850 dev->wireless_data = &iface->wireless_data;
852 dev->wireless_handlers = &hostap_iw_handler_def;
853 dev->watchdog_timeo = TX_TIMEOUT;
855 switch(type) {
856 case HOSTAP_INTERFACE_AP:
857 dev->priv_flags |= IFF_NO_QUEUE; /* use main radio device queue */
858 dev->netdev_ops = &hostap_mgmt_netdev_ops;
859 dev->type = ARPHRD_IEEE80211;
860 dev->header_ops = &hostap_80211_ops;
861 break;
862 case HOSTAP_INTERFACE_MASTER:
863 dev->netdev_ops = &hostap_master_ops;
864 break;
865 default:
866 dev->priv_flags |= IFF_NO_QUEUE; /* use main radio device queue */
867 dev->netdev_ops = &hostap_netdev_ops;
870 dev->mtu = local->mtu;
873 dev->ethtool_ops = &prism2_ethtool_ops;
877 static int hostap_enable_hostapd(local_info_t *local, int rtnl_locked)
879 struct net_device *dev = local->dev;
881 if (local->apdev)
882 return -EEXIST;
884 printk(KERN_DEBUG "%s: enabling hostapd mode\n", dev->name);
886 local->apdev = hostap_add_interface(local, HOSTAP_INTERFACE_AP,
887 rtnl_locked, local->ddev->name,
888 "ap");
889 if (local->apdev == NULL)
890 return -ENOMEM;
892 return 0;
896 static int hostap_disable_hostapd(local_info_t *local, int rtnl_locked)
898 struct net_device *dev = local->dev;
900 printk(KERN_DEBUG "%s: disabling hostapd mode\n", dev->name);
902 hostap_remove_interface(local->apdev, rtnl_locked, 1);
903 local->apdev = NULL;
905 return 0;
909 static int hostap_enable_hostapd_sta(local_info_t *local, int rtnl_locked)
911 struct net_device *dev = local->dev;
913 if (local->stadev)
914 return -EEXIST;
916 printk(KERN_DEBUG "%s: enabling hostapd STA mode\n", dev->name);
918 local->stadev = hostap_add_interface(local, HOSTAP_INTERFACE_STA,
919 rtnl_locked, local->ddev->name,
920 "sta");
921 if (local->stadev == NULL)
922 return -ENOMEM;
924 return 0;
928 static int hostap_disable_hostapd_sta(local_info_t *local, int rtnl_locked)
930 struct net_device *dev = local->dev;
932 printk(KERN_DEBUG "%s: disabling hostapd mode\n", dev->name);
934 hostap_remove_interface(local->stadev, rtnl_locked, 1);
935 local->stadev = NULL;
937 return 0;
941 int hostap_set_hostapd(local_info_t *local, int val, int rtnl_locked)
943 int ret;
945 if (val < 0 || val > 1)
946 return -EINVAL;
948 if (local->hostapd == val)
949 return 0;
951 if (val) {
952 ret = hostap_enable_hostapd(local, rtnl_locked);
953 if (ret == 0)
954 local->hostapd = 1;
955 } else {
956 local->hostapd = 0;
957 ret = hostap_disable_hostapd(local, rtnl_locked);
958 if (ret != 0)
959 local->hostapd = 1;
962 return ret;
966 int hostap_set_hostapd_sta(local_info_t *local, int val, int rtnl_locked)
968 int ret;
970 if (val < 0 || val > 1)
971 return -EINVAL;
973 if (local->hostapd_sta == val)
974 return 0;
976 if (val) {
977 ret = hostap_enable_hostapd_sta(local, rtnl_locked);
978 if (ret == 0)
979 local->hostapd_sta = 1;
980 } else {
981 local->hostapd_sta = 0;
982 ret = hostap_disable_hostapd_sta(local, rtnl_locked);
983 if (ret != 0)
984 local->hostapd_sta = 1;
988 return ret;
992 int prism2_update_comms_qual(struct net_device *dev)
994 struct hostap_interface *iface;
995 local_info_t *local;
996 int ret = 0;
997 struct hfa384x_comms_quality sq;
999 iface = netdev_priv(dev);
1000 local = iface->local;
1001 if (!local->sta_fw_ver)
1002 ret = -1;
1003 else if (local->sta_fw_ver >= PRISM2_FW_VER(1,3,1)) {
1004 if (local->func->get_rid(local->dev,
1005 HFA384X_RID_DBMCOMMSQUALITY,
1006 &sq, sizeof(sq), 1) >= 0) {
1007 local->comms_qual = (s16) le16_to_cpu(sq.comm_qual);
1008 local->avg_signal = (s16) le16_to_cpu(sq.signal_level);
1009 local->avg_noise = (s16) le16_to_cpu(sq.noise_level);
1010 local->last_comms_qual_update = jiffies;
1011 } else
1012 ret = -1;
1013 } else {
1014 if (local->func->get_rid(local->dev, HFA384X_RID_COMMSQUALITY,
1015 &sq, sizeof(sq), 1) >= 0) {
1016 local->comms_qual = le16_to_cpu(sq.comm_qual);
1017 local->avg_signal = HFA384X_LEVEL_TO_dBm(
1018 le16_to_cpu(sq.signal_level));
1019 local->avg_noise = HFA384X_LEVEL_TO_dBm(
1020 le16_to_cpu(sq.noise_level));
1021 local->last_comms_qual_update = jiffies;
1022 } else
1023 ret = -1;
1026 return ret;
1030 int prism2_sta_send_mgmt(local_info_t *local, u8 *dst, u16 stype,
1031 u8 *body, size_t bodylen)
1033 struct sk_buff *skb;
1034 struct hostap_ieee80211_mgmt *mgmt;
1035 struct hostap_skb_tx_data *meta;
1036 struct net_device *dev = local->dev;
1038 skb = dev_alloc_skb(IEEE80211_MGMT_HDR_LEN + bodylen);
1039 if (skb == NULL)
1040 return -ENOMEM;
1042 mgmt = skb_put_zero(skb, IEEE80211_MGMT_HDR_LEN);
1043 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
1044 memcpy(mgmt->da, dst, ETH_ALEN);
1045 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1046 memcpy(mgmt->bssid, dst, ETH_ALEN);
1047 if (body)
1048 skb_put_data(skb, body, bodylen);
1050 meta = (struct hostap_skb_tx_data *) skb->cb;
1051 memset(meta, 0, sizeof(*meta));
1052 meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
1053 meta->iface = netdev_priv(dev);
1055 skb->dev = dev;
1056 skb_reset_mac_header(skb);
1057 skb_reset_network_header(skb);
1058 dev_queue_xmit(skb);
1060 return 0;
1064 int prism2_sta_deauth(local_info_t *local, u16 reason)
1066 union iwreq_data wrqu;
1067 int ret;
1068 __le16 val = cpu_to_le16(reason);
1070 if (local->iw_mode != IW_MODE_INFRA ||
1071 is_zero_ether_addr(local->bssid) ||
1072 ether_addr_equal(local->bssid, "\x44\x44\x44\x44\x44\x44"))
1073 return 0;
1075 ret = prism2_sta_send_mgmt(local, local->bssid, IEEE80211_STYPE_DEAUTH,
1076 (u8 *) &val, 2);
1077 eth_zero_addr(wrqu.ap_addr.sa_data);
1078 wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
1079 return ret;
1083 struct proc_dir_entry *hostap_proc;
1085 static int __init hostap_init(void)
1087 if (init_net.proc_net != NULL) {
1088 hostap_proc = proc_mkdir("hostap", init_net.proc_net);
1089 if (!hostap_proc)
1090 printk(KERN_WARNING "Failed to mkdir "
1091 "/proc/net/hostap\n");
1092 } else
1093 hostap_proc = NULL;
1095 return 0;
1099 static void __exit hostap_exit(void)
1101 if (hostap_proc != NULL) {
1102 hostap_proc = NULL;
1103 remove_proc_entry("hostap", init_net.proc_net);
1108 EXPORT_SYMBOL(hostap_set_word);
1109 EXPORT_SYMBOL(hostap_set_string);
1110 EXPORT_SYMBOL(hostap_get_porttype);
1111 EXPORT_SYMBOL(hostap_set_encryption);
1112 EXPORT_SYMBOL(hostap_set_antsel);
1113 EXPORT_SYMBOL(hostap_set_roaming);
1114 EXPORT_SYMBOL(hostap_set_auth_algs);
1115 EXPORT_SYMBOL(hostap_dump_rx_header);
1116 EXPORT_SYMBOL(hostap_dump_tx_header);
1117 EXPORT_SYMBOL(hostap_80211_get_hdrlen);
1118 EXPORT_SYMBOL(hostap_setup_dev);
1119 EXPORT_SYMBOL(hostap_set_multicast_list_queue);
1120 EXPORT_SYMBOL(hostap_set_hostapd);
1121 EXPORT_SYMBOL(hostap_set_hostapd_sta);
1122 EXPORT_SYMBOL(hostap_add_interface);
1123 EXPORT_SYMBOL(hostap_remove_interface);
1124 EXPORT_SYMBOL(prism2_update_comms_qual);
1126 module_init(hostap_init);
1127 module_exit(hostap_exit);