libertas: down_interruptible() can return -EINTR, not EINTR
[pandora-kernel.git] / drivers / net / wireless / hostap / hostap_main.c
1 /*
2  * Host AP (software wireless LAN access point) driver for
3  * Intersil Prism2/2.5/3 - hostap.o module, common routines
4  *
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>
8  *
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.
13  */
14
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 <asm/uaccess.h>
31
32 #include "hostap_wlan.h"
33 #include "hostap_80211.h"
34 #include "hostap_ap.h"
35 #include "hostap.h"
36
37 MODULE_AUTHOR("Jouni Malinen");
38 MODULE_DESCRIPTION("Host AP common routines");
39 MODULE_LICENSE("GPL");
40
41 #define TX_TIMEOUT (2 * HZ)
42
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 */))
47
48
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)
53 {
54         struct net_device *dev, *mdev;
55         struct hostap_interface *iface;
56         int ret;
57
58         dev = alloc_etherdev(sizeof(struct hostap_interface));
59         if (dev == NULL)
60                 return NULL;
61
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);
67
68         mdev = local->dev;
69         memcpy(dev->dev_addr, mdev->dev_addr, ETH_ALEN);
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;
74
75         hostap_setup_dev(dev, local, type);
76         dev->destructor = free_netdev;
77
78         sprintf(dev->name, "%s%s", prefix, name);
79         if (!rtnl_locked)
80                 rtnl_lock();
81
82         ret = 0;
83         if (strchr(dev->name, '%'))
84                 ret = dev_alloc_name(dev, dev->name);
85
86         SET_NETDEV_DEV(dev, mdev->dev.parent);
87         if (ret >= 0)
88                 ret = register_netdevice(dev);
89
90         if (!rtnl_locked)
91                 rtnl_unlock();
92
93         if (ret < 0) {
94                 printk(KERN_WARNING "%s: failed to add new netdevice!\n",
95                        dev->name);
96                 free_netdev(dev);
97                 return NULL;
98         }
99
100         printk(KERN_DEBUG "%s: registered netdevice %s\n",
101                mdev->name, dev->name);
102
103         return dev;
104 }
105
106
107 void hostap_remove_interface(struct net_device *dev, int rtnl_locked,
108                              int remove_from_list)
109 {
110         struct hostap_interface *iface;
111
112         if (!dev)
113                 return;
114
115         iface = netdev_priv(dev);
116
117         if (remove_from_list) {
118                 list_del(&iface->list);
119         }
120
121         if (dev == iface->local->ddev)
122                 iface->local->ddev = NULL;
123         else if (dev == iface->local->apdev)
124                 iface->local->apdev = NULL;
125         else if (dev == iface->local->stadev)
126                 iface->local->stadev = NULL;
127
128         if (rtnl_locked)
129                 unregister_netdevice(dev);
130         else
131                 unregister_netdev(dev);
132
133         /* dev->destructor = free_netdev() will free the device data, including
134          * private data, when removing the device */
135 }
136
137
138 static inline int prism2_wds_special_addr(u8 *addr)
139 {
140         if (addr[0] || addr[1] || addr[2] || addr[3] || addr[4] || addr[5])
141                 return 0;
142
143         return 1;
144 }
145
146
147 int prism2_wds_add(local_info_t *local, u8 *remote_addr,
148                    int rtnl_locked)
149 {
150         struct net_device *dev;
151         struct list_head *ptr;
152         struct hostap_interface *iface, *empty, *match;
153
154         empty = match = NULL;
155         read_lock_bh(&local->iface_lock);
156         list_for_each(ptr, &local->hostap_interfaces) {
157                 iface = list_entry(ptr, struct hostap_interface, list);
158                 if (iface->type != HOSTAP_INTERFACE_WDS)
159                         continue;
160
161                 if (prism2_wds_special_addr(iface->u.wds.remote_addr))
162                         empty = iface;
163                 else if (memcmp(iface->u.wds.remote_addr, remote_addr,
164                                 ETH_ALEN) == 0) {
165                         match = iface;
166                         break;
167                 }
168         }
169         if (!match && empty && !prism2_wds_special_addr(remote_addr)) {
170                 /* take pre-allocated entry into use */
171                 memcpy(empty->u.wds.remote_addr, remote_addr, ETH_ALEN);
172                 read_unlock_bh(&local->iface_lock);
173                 printk(KERN_DEBUG "%s: using pre-allocated WDS netdevice %s\n",
174                        local->dev->name, empty->dev->name);
175                 return 0;
176         }
177         read_unlock_bh(&local->iface_lock);
178
179         if (!prism2_wds_special_addr(remote_addr)) {
180                 if (match)
181                         return -EEXIST;
182                 hostap_add_sta(local->ap, remote_addr);
183         }
184
185         if (local->wds_connections >= local->wds_max_connections)
186                 return -ENOBUFS;
187
188         /* verify that there is room for wds# postfix in the interface name */
189         if (strlen(local->dev->name) >= IFNAMSIZ - 5) {
190                 printk(KERN_DEBUG "'%s' too long base device name\n",
191                        local->dev->name);
192                 return -EINVAL;
193         }
194
195         dev = hostap_add_interface(local, HOSTAP_INTERFACE_WDS, rtnl_locked,
196                                    local->ddev->name, "wds%d");
197         if (dev == NULL)
198                 return -ENOMEM;
199
200         iface = netdev_priv(dev);
201         memcpy(iface->u.wds.remote_addr, remote_addr, ETH_ALEN);
202
203         local->wds_connections++;
204
205         return 0;
206 }
207
208
209 int prism2_wds_del(local_info_t *local, u8 *remote_addr,
210                    int rtnl_locked, int do_not_remove)
211 {
212         unsigned long flags;
213         struct list_head *ptr;
214         struct hostap_interface *iface, *selected = NULL;
215
216         write_lock_irqsave(&local->iface_lock, flags);
217         list_for_each(ptr, &local->hostap_interfaces) {
218                 iface = list_entry(ptr, struct hostap_interface, list);
219                 if (iface->type != HOSTAP_INTERFACE_WDS)
220                         continue;
221
222                 if (memcmp(iface->u.wds.remote_addr, remote_addr,
223                            ETH_ALEN) == 0) {
224                         selected = iface;
225                         break;
226                 }
227         }
228         if (selected && !do_not_remove)
229                 list_del(&selected->list);
230         write_unlock_irqrestore(&local->iface_lock, flags);
231
232         if (selected) {
233                 if (do_not_remove)
234                         memset(selected->u.wds.remote_addr, 0, ETH_ALEN);
235                 else {
236                         hostap_remove_interface(selected->dev, rtnl_locked, 0);
237                         local->wds_connections--;
238                 }
239         }
240
241         return selected ? 0 : -ENODEV;
242 }
243
244
245 u16 hostap_tx_callback_register(local_info_t *local,
246                                 void (*func)(struct sk_buff *, int ok, void *),
247                                 void *data)
248 {
249         unsigned long flags;
250         struct hostap_tx_callback_info *entry;
251
252         entry = kmalloc(sizeof(*entry),
253                                                            GFP_ATOMIC);
254         if (entry == NULL)
255                 return 0;
256
257         entry->func = func;
258         entry->data = data;
259
260         spin_lock_irqsave(&local->lock, flags);
261         entry->idx = local->tx_callback ? local->tx_callback->idx + 1 : 1;
262         entry->next = local->tx_callback;
263         local->tx_callback = entry;
264         spin_unlock_irqrestore(&local->lock, flags);
265
266         return entry->idx;
267 }
268
269
270 int hostap_tx_callback_unregister(local_info_t *local, u16 idx)
271 {
272         unsigned long flags;
273         struct hostap_tx_callback_info *cb, *prev = NULL;
274
275         spin_lock_irqsave(&local->lock, flags);
276         cb = local->tx_callback;
277         while (cb != NULL && cb->idx != idx) {
278                 prev = cb;
279                 cb = cb->next;
280         }
281         if (cb) {
282                 if (prev == NULL)
283                         local->tx_callback = cb->next;
284                 else
285                         prev->next = cb->next;
286                 kfree(cb);
287         }
288         spin_unlock_irqrestore(&local->lock, flags);
289
290         return cb ? 0 : -1;
291 }
292
293
294 /* val is in host byte order */
295 int hostap_set_word(struct net_device *dev, int rid, u16 val)
296 {
297         struct hostap_interface *iface;
298         __le16 tmp = cpu_to_le16(val);
299         iface = netdev_priv(dev);
300         return iface->local->func->set_rid(dev, rid, &tmp, 2);
301 }
302
303
304 int hostap_set_string(struct net_device *dev, int rid, const char *val)
305 {
306         struct hostap_interface *iface;
307         char buf[MAX_SSID_LEN + 2];
308         int len;
309
310         iface = netdev_priv(dev);
311         len = strlen(val);
312         if (len > MAX_SSID_LEN)
313                 return -1;
314         memset(buf, 0, sizeof(buf));
315         buf[0] = len; /* little endian 16 bit word */
316         memcpy(buf + 2, val, len);
317
318         return iface->local->func->set_rid(dev, rid, &buf, MAX_SSID_LEN + 2);
319 }
320
321
322 u16 hostap_get_porttype(local_info_t *local)
323 {
324         if (local->iw_mode == IW_MODE_ADHOC && local->pseudo_adhoc)
325                 return HFA384X_PORTTYPE_PSEUDO_IBSS;
326         if (local->iw_mode == IW_MODE_ADHOC)
327                 return HFA384X_PORTTYPE_IBSS;
328         if (local->iw_mode == IW_MODE_INFRA)
329                 return HFA384X_PORTTYPE_BSS;
330         if (local->iw_mode == IW_MODE_REPEAT)
331                 return HFA384X_PORTTYPE_WDS;
332         if (local->iw_mode == IW_MODE_MONITOR)
333                 return HFA384X_PORTTYPE_PSEUDO_IBSS;
334         return HFA384X_PORTTYPE_HOSTAP;
335 }
336
337
338 int hostap_set_encryption(local_info_t *local)
339 {
340         u16 val, old_val;
341         int i, keylen, len, idx;
342         char keybuf[WEP_KEY_LEN + 1];
343         enum { NONE, WEP, OTHER } encrypt_type;
344
345         idx = local->crypt_info.tx_keyidx;
346         if (local->crypt_info.crypt[idx] == NULL ||
347             local->crypt_info.crypt[idx]->ops == NULL)
348                 encrypt_type = NONE;
349         else if (strcmp(local->crypt_info.crypt[idx]->ops->name, "WEP") == 0)
350                 encrypt_type = WEP;
351         else
352                 encrypt_type = OTHER;
353
354         if (local->func->get_rid(local->dev, HFA384X_RID_CNFWEPFLAGS, &val, 2,
355                                  1) < 0) {
356                 printk(KERN_DEBUG "Could not read current WEP flags.\n");
357                 goto fail;
358         }
359         le16_to_cpus(&val);
360         old_val = val;
361
362         if (encrypt_type != NONE || local->privacy_invoked)
363                 val |= HFA384X_WEPFLAGS_PRIVACYINVOKED;
364         else
365                 val &= ~HFA384X_WEPFLAGS_PRIVACYINVOKED;
366
367         if (local->open_wep || encrypt_type == NONE ||
368             ((local->ieee_802_1x || local->wpa) && local->host_decrypt))
369                 val &= ~HFA384X_WEPFLAGS_EXCLUDEUNENCRYPTED;
370         else
371                 val |= HFA384X_WEPFLAGS_EXCLUDEUNENCRYPTED;
372
373         if ((encrypt_type != NONE || local->privacy_invoked) &&
374             (encrypt_type == OTHER || local->host_encrypt))
375                 val |= HFA384X_WEPFLAGS_HOSTENCRYPT;
376         else
377                 val &= ~HFA384X_WEPFLAGS_HOSTENCRYPT;
378         if ((encrypt_type != NONE || local->privacy_invoked) &&
379             (encrypt_type == OTHER || local->host_decrypt))
380                 val |= HFA384X_WEPFLAGS_HOSTDECRYPT;
381         else
382                 val &= ~HFA384X_WEPFLAGS_HOSTDECRYPT;
383
384         if (val != old_val &&
385             hostap_set_word(local->dev, HFA384X_RID_CNFWEPFLAGS, val)) {
386                 printk(KERN_DEBUG "Could not write new WEP flags (0x%x)\n",
387                        val);
388                 goto fail;
389         }
390
391         if (encrypt_type != WEP)
392                 return 0;
393
394         /* 104-bit support seems to require that all the keys are set to the
395          * same keylen */
396         keylen = 6; /* first 5 octets */
397         len = local->crypt_info.crypt[idx]->ops->get_key(keybuf, sizeof(keybuf), NULL,
398                                                            local->crypt_info.crypt[idx]->priv);
399         if (idx >= 0 && idx < WEP_KEYS && len > 5)
400                 keylen = WEP_KEY_LEN + 1; /* first 13 octets */
401
402         for (i = 0; i < WEP_KEYS; i++) {
403                 memset(keybuf, 0, sizeof(keybuf));
404                 if (local->crypt_info.crypt[i]) {
405                         (void) local->crypt_info.crypt[i]->ops->get_key(
406                                 keybuf, sizeof(keybuf),
407                                 NULL, local->crypt_info.crypt[i]->priv);
408                 }
409                 if (local->func->set_rid(local->dev,
410                                          HFA384X_RID_CNFDEFAULTKEY0 + i,
411                                          keybuf, keylen)) {
412                         printk(KERN_DEBUG "Could not set key %d (len=%d)\n",
413                                i, keylen);
414                         goto fail;
415                 }
416         }
417         if (hostap_set_word(local->dev, HFA384X_RID_CNFWEPDEFAULTKEYID, idx)) {
418                 printk(KERN_DEBUG "Could not set default keyid %d\n", idx);
419                 goto fail;
420         }
421
422         return 0;
423
424  fail:
425         printk(KERN_DEBUG "%s: encryption setup failed\n", local->dev->name);
426         return -1;
427 }
428
429
430 int hostap_set_antsel(local_info_t *local)
431 {
432         u16 val;
433         int ret = 0;
434
435         if (local->antsel_tx != HOSTAP_ANTSEL_DO_NOT_TOUCH &&
436             local->func->cmd(local->dev, HFA384X_CMDCODE_READMIF,
437                              HFA386X_CR_TX_CONFIGURE,
438                              NULL, &val) == 0) {
439                 val &= ~(BIT(2) | BIT(1));
440                 switch (local->antsel_tx) {
441                 case HOSTAP_ANTSEL_DIVERSITY:
442                         val |= BIT(1);
443                         break;
444                 case HOSTAP_ANTSEL_LOW:
445                         break;
446                 case HOSTAP_ANTSEL_HIGH:
447                         val |= BIT(2);
448                         break;
449                 }
450
451                 if (local->func->cmd(local->dev, HFA384X_CMDCODE_WRITEMIF,
452                                      HFA386X_CR_TX_CONFIGURE, &val, NULL)) {
453                         printk(KERN_INFO "%s: setting TX AntSel failed\n",
454                                local->dev->name);
455                         ret = -1;
456                 }
457         }
458
459         if (local->antsel_rx != HOSTAP_ANTSEL_DO_NOT_TOUCH &&
460             local->func->cmd(local->dev, HFA384X_CMDCODE_READMIF,
461                              HFA386X_CR_RX_CONFIGURE,
462                              NULL, &val) == 0) {
463                 val &= ~(BIT(1) | BIT(0));
464                 switch (local->antsel_rx) {
465                 case HOSTAP_ANTSEL_DIVERSITY:
466                         break;
467                 case HOSTAP_ANTSEL_LOW:
468                         val |= BIT(0);
469                         break;
470                 case HOSTAP_ANTSEL_HIGH:
471                         val |= BIT(0) | BIT(1);
472                         break;
473                 }
474
475                 if (local->func->cmd(local->dev, HFA384X_CMDCODE_WRITEMIF,
476                                      HFA386X_CR_RX_CONFIGURE, &val, NULL)) {
477                         printk(KERN_INFO "%s: setting RX AntSel failed\n",
478                                local->dev->name);
479                         ret = -1;
480                 }
481         }
482
483         return ret;
484 }
485
486
487 int hostap_set_roaming(local_info_t *local)
488 {
489         u16 val;
490
491         switch (local->host_roaming) {
492         case 1:
493                 val = HFA384X_ROAMING_HOST;
494                 break;
495         case 2:
496                 val = HFA384X_ROAMING_DISABLED;
497                 break;
498         case 0:
499         default:
500                 val = HFA384X_ROAMING_FIRMWARE;
501                 break;
502         }
503
504         return hostap_set_word(local->dev, HFA384X_RID_CNFROAMINGMODE, val);
505 }
506
507
508 int hostap_set_auth_algs(local_info_t *local)
509 {
510         int val = local->auth_algs;
511         /* At least STA f/w v0.6.2 seems to have issues with cnfAuthentication
512          * set to include both Open and Shared Key flags. It tries to use
513          * Shared Key authentication in that case even if WEP keys are not
514          * configured.. STA f/w v0.7.6 is able to handle such configuration,
515          * but it is unknown when this was fixed between 0.6.2 .. 0.7.6. */
516         if (local->sta_fw_ver < PRISM2_FW_VER(0,7,0) &&
517             val != PRISM2_AUTH_OPEN && val != PRISM2_AUTH_SHARED_KEY)
518                 val = PRISM2_AUTH_OPEN;
519
520         if (hostap_set_word(local->dev, HFA384X_RID_CNFAUTHENTICATION, val)) {
521                 printk(KERN_INFO "%s: cnfAuthentication setting to 0x%x "
522                        "failed\n", local->dev->name, local->auth_algs);
523                 return -EINVAL;
524         }
525
526         return 0;
527 }
528
529
530 void hostap_dump_rx_header(const char *name, const struct hfa384x_rx_frame *rx)
531 {
532         u16 status, fc;
533
534         status = __le16_to_cpu(rx->status);
535
536         printk(KERN_DEBUG "%s: RX status=0x%04x (port=%d, type=%d, "
537                "fcserr=%d) silence=%d signal=%d rate=%d rxflow=%d; "
538                "jiffies=%ld\n",
539                name, status, (status >> 8) & 0x07, status >> 13, status & 1,
540                rx->silence, rx->signal, rx->rate, rx->rxflow, jiffies);
541
542         fc = __le16_to_cpu(rx->frame_control);
543         printk(KERN_DEBUG "   FC=0x%04x (type=%d:%d) dur=0x%04x seq=0x%04x "
544                "data_len=%d%s%s\n",
545                fc, (fc & IEEE80211_FCTL_FTYPE) >> 2,
546                (fc & IEEE80211_FCTL_STYPE) >> 4,
547                __le16_to_cpu(rx->duration_id), __le16_to_cpu(rx->seq_ctrl),
548                __le16_to_cpu(rx->data_len),
549                fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
550                fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
551
552         printk(KERN_DEBUG "   A1=%pM A2=%pM A3=%pM A4=%pM\n",
553                rx->addr1, rx->addr2, rx->addr3, rx->addr4);
554
555         printk(KERN_DEBUG "   dst=%pM src=%pM len=%d\n",
556                rx->dst_addr, rx->src_addr,
557                __be16_to_cpu(rx->len));
558 }
559
560
561 void hostap_dump_tx_header(const char *name, const struct hfa384x_tx_frame *tx)
562 {
563         u16 fc;
564
565         printk(KERN_DEBUG "%s: TX status=0x%04x retry_count=%d tx_rate=%d "
566                "tx_control=0x%04x; jiffies=%ld\n",
567                name, __le16_to_cpu(tx->status), tx->retry_count, tx->tx_rate,
568                __le16_to_cpu(tx->tx_control), jiffies);
569
570         fc = __le16_to_cpu(tx->frame_control);
571         printk(KERN_DEBUG "   FC=0x%04x (type=%d:%d) dur=0x%04x seq=0x%04x "
572                "data_len=%d%s%s\n",
573                fc, (fc & IEEE80211_FCTL_FTYPE) >> 2,
574                (fc & IEEE80211_FCTL_STYPE) >> 4,
575                __le16_to_cpu(tx->duration_id), __le16_to_cpu(tx->seq_ctrl),
576                __le16_to_cpu(tx->data_len),
577                fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
578                fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
579
580         printk(KERN_DEBUG "   A1=%pM A2=%pM A3=%pM A4=%pM\n",
581                tx->addr1, tx->addr2, tx->addr3, tx->addr4);
582
583         printk(KERN_DEBUG "   dst=%pM src=%pM len=%d\n",
584                tx->dst_addr, tx->src_addr,
585                __be16_to_cpu(tx->len));
586 }
587
588
589 static int hostap_80211_header_parse(const struct sk_buff *skb,
590                                      unsigned char *haddr)
591 {
592         memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
593         return ETH_ALEN;
594 }
595
596
597 int hostap_80211_get_hdrlen(__le16 fc)
598 {
599         if (ieee80211_is_data(fc) && ieee80211_has_a4 (fc))
600                 return 30; /* Addr4 */
601         else if (ieee80211_is_cts(fc) || ieee80211_is_ack(fc))
602                 return 10;
603         else if (ieee80211_is_ctl(fc))
604                 return 16;
605
606         return 24;
607 }
608
609
610 static int prism2_close(struct net_device *dev)
611 {
612         struct hostap_interface *iface;
613         local_info_t *local;
614
615         PDEBUG(DEBUG_FLOW, "%s: prism2_close\n", dev->name);
616
617         iface = netdev_priv(dev);
618         local = iface->local;
619
620         if (dev == local->ddev) {
621                 prism2_sta_deauth(local, WLAN_REASON_DEAUTH_LEAVING);
622         }
623 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
624         if (!local->hostapd && dev == local->dev &&
625             (!local->func->card_present || local->func->card_present(local)) &&
626             local->hw_ready && local->ap && local->iw_mode == IW_MODE_MASTER)
627                 hostap_deauth_all_stas(dev, local->ap, 1);
628 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
629
630         if (dev == local->dev) {
631                 local->func->hw_shutdown(dev, HOSTAP_HW_ENABLE_CMDCOMPL);
632         }
633
634         if (netif_running(dev)) {
635                 netif_stop_queue(dev);
636                 netif_device_detach(dev);
637         }
638
639         cancel_work_sync(&local->reset_queue);
640         cancel_work_sync(&local->set_multicast_list_queue);
641         cancel_work_sync(&local->set_tim_queue);
642 #ifndef PRISM2_NO_STATION_MODES
643         cancel_work_sync(&local->info_queue);
644 #endif
645         cancel_work_sync(&local->comms_qual_update);
646
647         module_put(local->hw_module);
648
649         local->num_dev_open--;
650
651         if (dev != local->dev && local->dev->flags & IFF_UP &&
652             local->master_dev_auto_open && local->num_dev_open == 1) {
653                 /* Close master radio interface automatically if it was also
654                  * opened automatically and we are now closing the last
655                  * remaining non-master device. */
656                 dev_close(local->dev);
657         }
658
659         return 0;
660 }
661
662
663 static int prism2_open(struct net_device *dev)
664 {
665         struct hostap_interface *iface;
666         local_info_t *local;
667
668         PDEBUG(DEBUG_FLOW, "%s: prism2_open\n", dev->name);
669
670         iface = netdev_priv(dev);
671         local = iface->local;
672
673         if (local->no_pri) {
674                 printk(KERN_DEBUG "%s: could not set interface UP - no PRI "
675                        "f/w\n", dev->name);
676                 return 1;
677         }
678
679         if ((local->func->card_present && !local->func->card_present(local)) ||
680             local->hw_downloading)
681                 return -ENODEV;
682
683         if (!try_module_get(local->hw_module))
684                 return -ENODEV;
685         local->num_dev_open++;
686
687         if (!local->dev_enabled && local->func->hw_enable(dev, 1)) {
688                 printk(KERN_WARNING "%s: could not enable MAC port\n",
689                        dev->name);
690                 prism2_close(dev);
691                 return 1;
692         }
693         if (!local->dev_enabled)
694                 prism2_callback(local, PRISM2_CALLBACK_ENABLE);
695         local->dev_enabled = 1;
696
697         if (dev != local->dev && !(local->dev->flags & IFF_UP)) {
698                 /* Master radio interface is needed for all operation, so open
699                  * it automatically when any virtual net_device is opened. */
700                 local->master_dev_auto_open = 1;
701                 dev_open(local->dev);
702         }
703
704         netif_device_attach(dev);
705         netif_start_queue(dev);
706
707         return 0;
708 }
709
710
711 static int prism2_set_mac_address(struct net_device *dev, void *p)
712 {
713         struct hostap_interface *iface;
714         local_info_t *local;
715         struct list_head *ptr;
716         struct sockaddr *addr = p;
717
718         iface = netdev_priv(dev);
719         local = iface->local;
720
721         if (local->func->set_rid(dev, HFA384X_RID_CNFOWNMACADDR, addr->sa_data,
722                                  ETH_ALEN) < 0 || local->func->reset_port(dev))
723                 return -EINVAL;
724
725         read_lock_bh(&local->iface_lock);
726         list_for_each(ptr, &local->hostap_interfaces) {
727                 iface = list_entry(ptr, struct hostap_interface, list);
728                 memcpy(iface->dev->dev_addr, addr->sa_data, ETH_ALEN);
729         }
730         memcpy(local->dev->dev_addr, addr->sa_data, ETH_ALEN);
731         read_unlock_bh(&local->iface_lock);
732
733         return 0;
734 }
735
736
737 /* TODO: to be further implemented as soon as Prism2 fully supports
738  *       GroupAddresses and correct documentation is available */
739 void hostap_set_multicast_list_queue(struct work_struct *work)
740 {
741         local_info_t *local =
742                 container_of(work, local_info_t, set_multicast_list_queue);
743         struct net_device *dev = local->dev;
744
745         if (hostap_set_word(dev, HFA384X_RID_PROMISCUOUSMODE,
746                             local->is_promisc)) {
747                 printk(KERN_INFO "%s: %sabling promiscuous mode failed\n",
748                        dev->name, local->is_promisc ? "en" : "dis");
749         }
750 }
751
752
753 static void hostap_set_multicast_list(struct net_device *dev)
754 {
755 #if 0
756         /* FIX: promiscuous mode seems to be causing a lot of problems with
757          * some station firmware versions (FCSErr frames, invalid MACPort, etc.
758          * corrupted incoming frames). This code is now commented out while the
759          * problems are investigated. */
760         struct hostap_interface *iface;
761         local_info_t *local;
762
763         iface = netdev_priv(dev);
764         local = iface->local;
765         if ((dev->flags & IFF_ALLMULTI) || (dev->flags & IFF_PROMISC)) {
766                 local->is_promisc = 1;
767         } else {
768                 local->is_promisc = 0;
769         }
770
771         schedule_work(&local->set_multicast_list_queue);
772 #endif
773 }
774
775
776 static int prism2_change_mtu(struct net_device *dev, int new_mtu)
777 {
778         if (new_mtu < PRISM2_MIN_MTU || new_mtu > PRISM2_MAX_MTU)
779                 return -EINVAL;
780
781         dev->mtu = new_mtu;
782         return 0;
783 }
784
785
786 static void prism2_tx_timeout(struct net_device *dev)
787 {
788         struct hostap_interface *iface;
789         local_info_t *local;
790         struct hfa384x_regs regs;
791
792         iface = netdev_priv(dev);
793         local = iface->local;
794
795         printk(KERN_WARNING "%s Tx timed out! Resetting card\n", dev->name);
796         netif_stop_queue(local->dev);
797
798         local->func->read_regs(dev, &regs);
799         printk(KERN_DEBUG "%s: CMD=%04x EVSTAT=%04x "
800                "OFFSET0=%04x OFFSET1=%04x SWSUPPORT0=%04x\n",
801                dev->name, regs.cmd, regs.evstat, regs.offset0, regs.offset1,
802                regs.swsupport0);
803
804         local->func->schedule_reset(local);
805 }
806
807 const struct header_ops hostap_80211_ops = {
808         .create         = eth_header,
809         .rebuild        = eth_rebuild_header,
810         .cache          = eth_header_cache,
811         .cache_update   = eth_header_cache_update,
812         .parse          = hostap_80211_header_parse,
813 };
814 EXPORT_SYMBOL(hostap_80211_ops);
815
816
817 static const struct net_device_ops hostap_netdev_ops = {
818         .ndo_start_xmit         = hostap_data_start_xmit,
819
820         .ndo_open               = prism2_open,
821         .ndo_stop               = prism2_close,
822         .ndo_do_ioctl           = hostap_ioctl,
823         .ndo_set_mac_address    = prism2_set_mac_address,
824         .ndo_set_multicast_list = hostap_set_multicast_list,
825         .ndo_change_mtu         = prism2_change_mtu,
826         .ndo_tx_timeout         = prism2_tx_timeout,
827         .ndo_validate_addr      = eth_validate_addr,
828 };
829
830 static const struct net_device_ops hostap_mgmt_netdev_ops = {
831         .ndo_start_xmit         = hostap_mgmt_start_xmit,
832
833         .ndo_open               = prism2_open,
834         .ndo_stop               = prism2_close,
835         .ndo_do_ioctl           = hostap_ioctl,
836         .ndo_set_mac_address    = prism2_set_mac_address,
837         .ndo_set_multicast_list = hostap_set_multicast_list,
838         .ndo_change_mtu         = prism2_change_mtu,
839         .ndo_tx_timeout         = prism2_tx_timeout,
840         .ndo_validate_addr      = eth_validate_addr,
841 };
842
843 static const struct net_device_ops hostap_master_ops = {
844         .ndo_start_xmit         = hostap_master_start_xmit,
845
846         .ndo_open               = prism2_open,
847         .ndo_stop               = prism2_close,
848         .ndo_do_ioctl           = hostap_ioctl,
849         .ndo_set_mac_address    = prism2_set_mac_address,
850         .ndo_set_multicast_list = hostap_set_multicast_list,
851         .ndo_change_mtu         = prism2_change_mtu,
852         .ndo_tx_timeout         = prism2_tx_timeout,
853         .ndo_validate_addr      = eth_validate_addr,
854 };
855
856 void hostap_setup_dev(struct net_device *dev, local_info_t *local,
857                       int type)
858 {
859         struct hostap_interface *iface;
860
861         iface = netdev_priv(dev);
862         ether_setup(dev);
863
864         /* kernel callbacks */
865         if (iface) {
866                 /* Currently, we point to the proper spy_data only on
867                  * the main_dev. This could be fixed. Jean II */
868                 iface->wireless_data.spy_data = &iface->spy_data;
869                 dev->wireless_data = &iface->wireless_data;
870         }
871         dev->wireless_handlers = &hostap_iw_handler_def;
872         dev->watchdog_timeo = TX_TIMEOUT;
873
874         switch(type) {
875         case HOSTAP_INTERFACE_AP:
876                 dev->tx_queue_len = 0;  /* use main radio device queue */
877                 dev->netdev_ops = &hostap_mgmt_netdev_ops;
878                 dev->type = ARPHRD_IEEE80211;
879                 dev->header_ops = &hostap_80211_ops;
880                 break;
881         case HOSTAP_INTERFACE_MASTER:
882                 dev->netdev_ops = &hostap_master_ops;
883                 break;
884         default:
885                 dev->tx_queue_len = 0;  /* use main radio device queue */
886                 dev->netdev_ops = &hostap_netdev_ops;
887         }
888
889         dev->mtu = local->mtu;
890
891
892         SET_ETHTOOL_OPS(dev, &prism2_ethtool_ops);
893
894         netif_stop_queue(dev);
895 }
896
897 static int hostap_enable_hostapd(local_info_t *local, int rtnl_locked)
898 {
899         struct net_device *dev = local->dev;
900
901         if (local->apdev)
902                 return -EEXIST;
903
904         printk(KERN_DEBUG "%s: enabling hostapd mode\n", dev->name);
905
906         local->apdev = hostap_add_interface(local, HOSTAP_INTERFACE_AP,
907                                             rtnl_locked, local->ddev->name,
908                                             "ap");
909         if (local->apdev == NULL)
910                 return -ENOMEM;
911
912         return 0;
913 }
914
915
916 static int hostap_disable_hostapd(local_info_t *local, int rtnl_locked)
917 {
918         struct net_device *dev = local->dev;
919
920         printk(KERN_DEBUG "%s: disabling hostapd mode\n", dev->name);
921
922         hostap_remove_interface(local->apdev, rtnl_locked, 1);
923         local->apdev = NULL;
924
925         return 0;
926 }
927
928
929 static int hostap_enable_hostapd_sta(local_info_t *local, int rtnl_locked)
930 {
931         struct net_device *dev = local->dev;
932
933         if (local->stadev)
934                 return -EEXIST;
935
936         printk(KERN_DEBUG "%s: enabling hostapd STA mode\n", dev->name);
937
938         local->stadev = hostap_add_interface(local, HOSTAP_INTERFACE_STA,
939                                              rtnl_locked, local->ddev->name,
940                                              "sta");
941         if (local->stadev == NULL)
942                 return -ENOMEM;
943
944         return 0;
945 }
946
947
948 static int hostap_disable_hostapd_sta(local_info_t *local, int rtnl_locked)
949 {
950         struct net_device *dev = local->dev;
951
952         printk(KERN_DEBUG "%s: disabling hostapd mode\n", dev->name);
953
954         hostap_remove_interface(local->stadev, rtnl_locked, 1);
955         local->stadev = NULL;
956
957         return 0;
958 }
959
960
961 int hostap_set_hostapd(local_info_t *local, int val, int rtnl_locked)
962 {
963         int ret;
964
965         if (val < 0 || val > 1)
966                 return -EINVAL;
967
968         if (local->hostapd == val)
969                 return 0;
970
971         if (val) {
972                 ret = hostap_enable_hostapd(local, rtnl_locked);
973                 if (ret == 0)
974                         local->hostapd = 1;
975         } else {
976                 local->hostapd = 0;
977                 ret = hostap_disable_hostapd(local, rtnl_locked);
978                 if (ret != 0)
979                         local->hostapd = 1;
980         }
981
982         return ret;
983 }
984
985
986 int hostap_set_hostapd_sta(local_info_t *local, int val, int rtnl_locked)
987 {
988         int ret;
989
990         if (val < 0 || val > 1)
991                 return -EINVAL;
992
993         if (local->hostapd_sta == val)
994                 return 0;
995
996         if (val) {
997                 ret = hostap_enable_hostapd_sta(local, rtnl_locked);
998                 if (ret == 0)
999                         local->hostapd_sta = 1;
1000         } else {
1001                 local->hostapd_sta = 0;
1002                 ret = hostap_disable_hostapd_sta(local, rtnl_locked);
1003                 if (ret != 0)
1004                         local->hostapd_sta = 1;
1005         }
1006
1007
1008         return ret;
1009 }
1010
1011
1012 int prism2_update_comms_qual(struct net_device *dev)
1013 {
1014         struct hostap_interface *iface;
1015         local_info_t *local;
1016         int ret = 0;
1017         struct hfa384x_comms_quality sq;
1018
1019         iface = netdev_priv(dev);
1020         local = iface->local;
1021         if (!local->sta_fw_ver)
1022                 ret = -1;
1023         else if (local->sta_fw_ver >= PRISM2_FW_VER(1,3,1)) {
1024                 if (local->func->get_rid(local->dev,
1025                                          HFA384X_RID_DBMCOMMSQUALITY,
1026                                          &sq, sizeof(sq), 1) >= 0) {
1027                         local->comms_qual = (s16) le16_to_cpu(sq.comm_qual);
1028                         local->avg_signal = (s16) le16_to_cpu(sq.signal_level);
1029                         local->avg_noise = (s16) le16_to_cpu(sq.noise_level);
1030                         local->last_comms_qual_update = jiffies;
1031                 } else
1032                         ret = -1;
1033         } else {
1034                 if (local->func->get_rid(local->dev, HFA384X_RID_COMMSQUALITY,
1035                                          &sq, sizeof(sq), 1) >= 0) {
1036                         local->comms_qual = le16_to_cpu(sq.comm_qual);
1037                         local->avg_signal = HFA384X_LEVEL_TO_dBm(
1038                                 le16_to_cpu(sq.signal_level));
1039                         local->avg_noise = HFA384X_LEVEL_TO_dBm(
1040                                 le16_to_cpu(sq.noise_level));
1041                         local->last_comms_qual_update = jiffies;
1042                 } else
1043                         ret = -1;
1044         }
1045
1046         return ret;
1047 }
1048
1049
1050 int prism2_sta_send_mgmt(local_info_t *local, u8 *dst, u16 stype,
1051                          u8 *body, size_t bodylen)
1052 {
1053         struct sk_buff *skb;
1054         struct hostap_ieee80211_mgmt *mgmt;
1055         struct hostap_skb_tx_data *meta;
1056         struct net_device *dev = local->dev;
1057
1058         skb = dev_alloc_skb(IEEE80211_MGMT_HDR_LEN + bodylen);
1059         if (skb == NULL)
1060                 return -ENOMEM;
1061
1062         mgmt = (struct hostap_ieee80211_mgmt *)
1063                 skb_put(skb, IEEE80211_MGMT_HDR_LEN);
1064         memset(mgmt, 0, IEEE80211_MGMT_HDR_LEN);
1065         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
1066         memcpy(mgmt->da, dst, ETH_ALEN);
1067         memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1068         memcpy(mgmt->bssid, dst, ETH_ALEN);
1069         if (body)
1070                 memcpy(skb_put(skb, bodylen), body, bodylen);
1071
1072         meta = (struct hostap_skb_tx_data *) skb->cb;
1073         memset(meta, 0, sizeof(*meta));
1074         meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
1075         meta->iface = netdev_priv(dev);
1076
1077         skb->dev = dev;
1078         skb_reset_mac_header(skb);
1079         skb_reset_network_header(skb);
1080         dev_queue_xmit(skb);
1081
1082         return 0;
1083 }
1084
1085
1086 int prism2_sta_deauth(local_info_t *local, u16 reason)
1087 {
1088         union iwreq_data wrqu;
1089         int ret;
1090         __le16 val = cpu_to_le16(reason);
1091
1092         if (local->iw_mode != IW_MODE_INFRA ||
1093             memcmp(local->bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) == 0 ||
1094             memcmp(local->bssid, "\x44\x44\x44\x44\x44\x44", ETH_ALEN) == 0)
1095                 return 0;
1096
1097         ret = prism2_sta_send_mgmt(local, local->bssid, IEEE80211_STYPE_DEAUTH,
1098                                    (u8 *) &val, 2);
1099         memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
1100         wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
1101         return ret;
1102 }
1103
1104
1105 struct proc_dir_entry *hostap_proc;
1106
1107 static int __init hostap_init(void)
1108 {
1109         if (init_net.proc_net != NULL) {
1110                 hostap_proc = proc_mkdir("hostap", init_net.proc_net);
1111                 if (!hostap_proc)
1112                         printk(KERN_WARNING "Failed to mkdir "
1113                                "/proc/net/hostap\n");
1114         } else
1115                 hostap_proc = NULL;
1116
1117         return 0;
1118 }
1119
1120
1121 static void __exit hostap_exit(void)
1122 {
1123         if (hostap_proc != NULL) {
1124                 hostap_proc = NULL;
1125                 remove_proc_entry("hostap", init_net.proc_net);
1126         }
1127 }
1128
1129
1130 EXPORT_SYMBOL(hostap_set_word);
1131 EXPORT_SYMBOL(hostap_set_string);
1132 EXPORT_SYMBOL(hostap_get_porttype);
1133 EXPORT_SYMBOL(hostap_set_encryption);
1134 EXPORT_SYMBOL(hostap_set_antsel);
1135 EXPORT_SYMBOL(hostap_set_roaming);
1136 EXPORT_SYMBOL(hostap_set_auth_algs);
1137 EXPORT_SYMBOL(hostap_dump_rx_header);
1138 EXPORT_SYMBOL(hostap_dump_tx_header);
1139 EXPORT_SYMBOL(hostap_80211_get_hdrlen);
1140 EXPORT_SYMBOL(hostap_setup_dev);
1141 EXPORT_SYMBOL(hostap_set_multicast_list_queue);
1142 EXPORT_SYMBOL(hostap_set_hostapd);
1143 EXPORT_SYMBOL(hostap_set_hostapd_sta);
1144 EXPORT_SYMBOL(hostap_add_interface);
1145 EXPORT_SYMBOL(hostap_remove_interface);
1146 EXPORT_SYMBOL(prism2_update_comms_qual);
1147
1148 module_init(hostap_init);
1149 module_exit(hostap_exit);