Back to home page

OSCL-LXR

 
 

    


0001 /* main.c - (formerly known as dldwd_cs.c, orinoco_cs.c and orinoco.c)
0002  *
0003  * A driver for Hermes or Prism 2 chipset based PCMCIA wireless
0004  * adaptors, with Lucent/Agere, Intersil or Symbol firmware.
0005  *
0006  * Current maintainers (as of 29 September 2003) are:
0007  *  Pavel Roskin <proski AT gnu.org>
0008  * and  David Gibson <hermes AT gibson.dropbear.id.au>
0009  *
0010  * (C) Copyright David Gibson, IBM Corporation 2001-2003.
0011  * Copyright (C) 2000 David Gibson, Linuxcare Australia.
0012  *  With some help from :
0013  * Copyright (C) 2001 Jean Tourrilhes, HP Labs
0014  * Copyright (C) 2001 Benjamin Herrenschmidt
0015  *
0016  * Based on dummy_cs.c 1.27 2000/06/12 21:27:25
0017  *
0018  * Portions based on wvlan_cs.c 1.0.6, Copyright Andreas Neuhaus <andy
0019  * AT fasta.fh-dortmund.de>
0020  *      http://www.stud.fh-dortmund.de/~andy/wvlan/
0021  *
0022  * The contents of this file are subject to the Mozilla Public License
0023  * Version 1.1 (the "License"); you may not use this file except in
0024  * compliance with the License. You may obtain a copy of the License
0025  * at http://www.mozilla.org/MPL/
0026  *
0027  * Software distributed under the License is distributed on an "AS IS"
0028  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
0029  * the License for the specific language governing rights and
0030  * limitations under the License.
0031  *
0032  * The initial developer of the original code is David A. Hinds
0033  * <dahinds AT users.sourceforge.net>.  Portions created by David
0034  * A. Hinds are Copyright (C) 1999 David A. Hinds.  All Rights
0035  * Reserved.
0036  *
0037  * Alternatively, the contents of this file may be used under the
0038  * terms of the GNU General Public License version 2 (the "GPL"), in
0039  * which case the provisions of the GPL are applicable instead of the
0040  * above.  If you wish to allow the use of your version of this file
0041  * only under the terms of the GPL and not to allow others to use your
0042  * version of this file under the MPL, indicate your decision by
0043  * deleting the provisions above and replace them with the notice and
0044  * other provisions required by the GPL.  If you do not delete the
0045  * provisions above, a recipient may use your version of this file
0046  * under either the MPL or the GPL.  */
0047 
0048 /*
0049  * TODO
0050  *  o Handle de-encapsulation within network layer, provide 802.11
0051  *    headers (patch from Thomas 'Dent' Mirlacher)
0052  *  o Fix possible races in SPY handling.
0053  *  o Disconnect wireless extensions from fundamental configuration.
0054  *  o (maybe) Software WEP support (patch from Stano Meduna).
0055  *  o (maybe) Use multiple Tx buffers - driver handling queue
0056  *    rather than firmware.
0057  */
0058 
0059 /* Locking and synchronization:
0060  *
0061  * The basic principle is that everything is serialized through a
0062  * single spinlock, priv->lock.  The lock is used in user, bh and irq
0063  * context, so when taken outside hardirq context it should always be
0064  * taken with interrupts disabled.  The lock protects both the
0065  * hardware and the struct orinoco_private.
0066  *
0067  * Another flag, priv->hw_unavailable indicates that the hardware is
0068  * unavailable for an extended period of time (e.g. suspended, or in
0069  * the middle of a hard reset).  This flag is protected by the
0070  * spinlock.  All code which touches the hardware should check the
0071  * flag after taking the lock, and if it is set, give up on whatever
0072  * they are doing and drop the lock again.  The orinoco_lock()
0073  * function handles this (it unlocks and returns -EBUSY if
0074  * hw_unavailable is non-zero).
0075  */
0076 
0077 #define DRIVER_NAME "orinoco"
0078 
0079 #include <linux/module.h>
0080 #include <linux/kernel.h>
0081 #include <linux/slab.h>
0082 #include <linux/init.h>
0083 #include <linux/delay.h>
0084 #include <linux/device.h>
0085 #include <linux/netdevice.h>
0086 #include <linux/etherdevice.h>
0087 #include <linux/suspend.h>
0088 #include <linux/if_arp.h>
0089 #include <linux/wireless.h>
0090 #include <linux/ieee80211.h>
0091 #include <net/iw_handler.h>
0092 #include <net/cfg80211.h>
0093 
0094 #include "hermes_rid.h"
0095 #include "hermes_dld.h"
0096 #include "hw.h"
0097 #include "scan.h"
0098 #include "mic.h"
0099 #include "fw.h"
0100 #include "wext.h"
0101 #include "cfg.h"
0102 #include "main.h"
0103 
0104 #include "orinoco.h"
0105 
0106 /********************************************************************/
0107 /* Module information                                               */
0108 /********************************************************************/
0109 
0110 MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & "
0111           "David Gibson <hermes@gibson.dropbear.id.au>");
0112 MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based "
0113            "and similar wireless cards");
0114 MODULE_LICENSE("Dual MPL/GPL");
0115 
0116 /* Level of debugging. Used in the macros in orinoco.h */
0117 #ifdef ORINOCO_DEBUG
0118 int orinoco_debug = ORINOCO_DEBUG;
0119 EXPORT_SYMBOL(orinoco_debug);
0120 module_param(orinoco_debug, int, 0644);
0121 MODULE_PARM_DESC(orinoco_debug, "Debug level");
0122 #endif
0123 
0124 static bool suppress_linkstatus; /* = 0 */
0125 module_param(suppress_linkstatus, bool, 0644);
0126 MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
0127 
0128 static int ignore_disconnect; /* = 0 */
0129 module_param(ignore_disconnect, int, 0644);
0130 MODULE_PARM_DESC(ignore_disconnect,
0131          "Don't report lost link to the network layer");
0132 
0133 int force_monitor; /* = 0 */
0134 module_param(force_monitor, int, 0644);
0135 MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
0136 
0137 /********************************************************************/
0138 /* Internal constants                                               */
0139 /********************************************************************/
0140 
0141 /* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
0142 static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
0143 #define ENCAPS_OVERHEAD     (sizeof(encaps_hdr) + 2)
0144 
0145 #define ORINOCO_MIN_MTU     256
0146 #define ORINOCO_MAX_MTU     (IEEE80211_MAX_DATA_LEN - ENCAPS_OVERHEAD)
0147 
0148 #define MAX_IRQLOOPS_PER_IRQ    10
0149 #define MAX_IRQLOOPS_PER_JIFFY  (20000 / HZ)    /* Based on a guestimate of
0150                          * how many events the
0151                          * device could
0152                          * legitimately generate */
0153 
0154 #define DUMMY_FID       0xFFFF
0155 
0156 /*#define MAX_MULTICAST(priv)   (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
0157   HERMES_MAX_MULTICAST : 0)*/
0158 #define MAX_MULTICAST(priv) (HERMES_MAX_MULTICAST)
0159 
0160 #define ORINOCO_INTEN       (HERMES_EV_RX | HERMES_EV_ALLOC \
0161                  | HERMES_EV_TX | HERMES_EV_TXEXC \
0162                  | HERMES_EV_WTERR | HERMES_EV_INFO \
0163                  | HERMES_EV_INFDROP)
0164 
0165 /********************************************************************/
0166 /* Data types                                                       */
0167 /********************************************************************/
0168 
0169 /* Beginning of the Tx descriptor, used in TxExc handling */
0170 struct hermes_txexc_data {
0171     struct hermes_tx_descriptor desc;
0172     __le16 frame_ctl;
0173     __le16 duration_id;
0174     u8 addr1[ETH_ALEN];
0175 } __packed;
0176 
0177 /* Rx frame header except compatibility 802.3 header */
0178 struct hermes_rx_descriptor {
0179     /* Control */
0180     __le16 status;
0181     __le32 time;
0182     u8 silence;
0183     u8 signal;
0184     u8 rate;
0185     u8 rxflow;
0186     __le32 reserved;
0187 
0188     /* 802.11 header */
0189     __le16 frame_ctl;
0190     __le16 duration_id;
0191     u8 addr1[ETH_ALEN];
0192     u8 addr2[ETH_ALEN];
0193     u8 addr3[ETH_ALEN];
0194     __le16 seq_ctl;
0195     u8 addr4[ETH_ALEN];
0196 
0197     /* Data length */
0198     __le16 data_len;
0199 } __packed;
0200 
0201 struct orinoco_rx_data {
0202     struct hermes_rx_descriptor *desc;
0203     struct sk_buff *skb;
0204     struct list_head list;
0205 };
0206 
0207 struct orinoco_scan_data {
0208     void *buf;
0209     size_t len;
0210     int type;
0211     struct list_head list;
0212 };
0213 
0214 /********************************************************************/
0215 /* Function prototypes                                              */
0216 /********************************************************************/
0217 
0218 static int __orinoco_set_multicast_list(struct net_device *dev);
0219 static int __orinoco_up(struct orinoco_private *priv);
0220 static int __orinoco_down(struct orinoco_private *priv);
0221 static int __orinoco_commit(struct orinoco_private *priv);
0222 
0223 /********************************************************************/
0224 /* Internal helper functions                                        */
0225 /********************************************************************/
0226 
0227 void set_port_type(struct orinoco_private *priv)
0228 {
0229     switch (priv->iw_mode) {
0230     case NL80211_IFTYPE_STATION:
0231         priv->port_type = 1;
0232         priv->createibss = 0;
0233         break;
0234     case NL80211_IFTYPE_ADHOC:
0235         if (priv->prefer_port3) {
0236             priv->port_type = 3;
0237             priv->createibss = 0;
0238         } else {
0239             priv->port_type = priv->ibss_port;
0240             priv->createibss = 1;
0241         }
0242         break;
0243     case NL80211_IFTYPE_MONITOR:
0244         priv->port_type = 3;
0245         priv->createibss = 0;
0246         break;
0247     default:
0248         printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
0249                priv->ndev->name);
0250     }
0251 }
0252 
0253 /********************************************************************/
0254 /* Device methods                                                   */
0255 /********************************************************************/
0256 
0257 int orinoco_open(struct net_device *dev)
0258 {
0259     struct orinoco_private *priv = ndev_priv(dev);
0260     unsigned long flags;
0261     int err;
0262 
0263     if (orinoco_lock(priv, &flags) != 0)
0264         return -EBUSY;
0265 
0266     err = __orinoco_up(priv);
0267 
0268     if (!err)
0269         priv->open = 1;
0270 
0271     orinoco_unlock(priv, &flags);
0272 
0273     return err;
0274 }
0275 EXPORT_SYMBOL(orinoco_open);
0276 
0277 int orinoco_stop(struct net_device *dev)
0278 {
0279     struct orinoco_private *priv = ndev_priv(dev);
0280     int err = 0;
0281 
0282     /* We mustn't use orinoco_lock() here, because we need to be
0283        able to close the interface even if hw_unavailable is set
0284        (e.g. as we're released after a PC Card removal) */
0285     orinoco_lock_irq(priv);
0286 
0287     priv->open = 0;
0288 
0289     err = __orinoco_down(priv);
0290 
0291     orinoco_unlock_irq(priv);
0292 
0293     return err;
0294 }
0295 EXPORT_SYMBOL(orinoco_stop);
0296 
0297 void orinoco_set_multicast_list(struct net_device *dev)
0298 {
0299     struct orinoco_private *priv = ndev_priv(dev);
0300     unsigned long flags;
0301 
0302     if (orinoco_lock(priv, &flags) != 0) {
0303         printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
0304                "called when hw_unavailable\n", dev->name);
0305         return;
0306     }
0307 
0308     __orinoco_set_multicast_list(dev);
0309     orinoco_unlock(priv, &flags);
0310 }
0311 EXPORT_SYMBOL(orinoco_set_multicast_list);
0312 
0313 int orinoco_change_mtu(struct net_device *dev, int new_mtu)
0314 {
0315     struct orinoco_private *priv = ndev_priv(dev);
0316 
0317     /* MTU + encapsulation + header length */
0318     if ((new_mtu + ENCAPS_OVERHEAD + sizeof(struct ieee80211_hdr)) >
0319          (priv->nicbuf_size - ETH_HLEN))
0320         return -EINVAL;
0321 
0322     dev->mtu = new_mtu;
0323 
0324     return 0;
0325 }
0326 EXPORT_SYMBOL(orinoco_change_mtu);
0327 
0328 /********************************************************************/
0329 /* Tx path                                                          */
0330 /********************************************************************/
0331 
0332 /* Add encapsulation and MIC to the existing SKB.
0333  * The main xmit routine will then send the whole lot to the card.
0334  * Need 8 bytes headroom
0335  * Need 8 bytes tailroom
0336  *
0337  *                          With encapsulated ethernet II frame
0338  *                          --------
0339  *                          803.3 header (14 bytes)
0340  *                           dst[6]
0341  * --------                  src[6]
0342  * 803.3 header (14 bytes)   len[2]
0343  *  dst[6]                  803.2 header (8 bytes)
0344  *  src[6]                   encaps[6]
0345  *  len[2] <- leave alone -> len[2]
0346  * --------                 -------- <-- 0
0347  * Payload                  Payload
0348  * ...                      ...
0349  *
0350  * --------                 --------
0351  *                          MIC (8 bytes)
0352  *                          --------
0353  *
0354  * returns 0 on success, -ENOMEM on error.
0355  */
0356 int orinoco_process_xmit_skb(struct sk_buff *skb,
0357                  struct net_device *dev,
0358                  struct orinoco_private *priv,
0359                  int *tx_control,
0360                  u8 *mic_buf)
0361 {
0362     struct orinoco_tkip_key *key;
0363     struct ethhdr *eh;
0364     int do_mic;
0365 
0366     key = (struct orinoco_tkip_key *) priv->keys[priv->tx_key].key;
0367 
0368     do_mic = ((priv->encode_alg == ORINOCO_ALG_TKIP) &&
0369           (key != NULL));
0370 
0371     if (do_mic)
0372         *tx_control |= (priv->tx_key << HERMES_MIC_KEY_ID_SHIFT) |
0373             HERMES_TXCTRL_MIC;
0374 
0375     eh = (struct ethhdr *)skb->data;
0376 
0377     /* Encapsulate Ethernet-II frames */
0378     if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
0379         struct header_struct {
0380             struct ethhdr eth;  /* 802.3 header */
0381             u8 encap[6];        /* 802.2 header */
0382         } __packed hdr;
0383         int len = skb->len + sizeof(encaps_hdr) - (2 * ETH_ALEN);
0384 
0385         if (skb_headroom(skb) < ENCAPS_OVERHEAD) {
0386             if (net_ratelimit())
0387                 printk(KERN_ERR
0388                        "%s: Not enough headroom for 802.2 headers %d\n",
0389                        dev->name, skb_headroom(skb));
0390             return -ENOMEM;
0391         }
0392 
0393         /* Fill in new header */
0394         memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
0395         hdr.eth.h_proto = htons(len);
0396         memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
0397 
0398         /* Make room for the new header, and copy it in */
0399         eh = skb_push(skb, ENCAPS_OVERHEAD);
0400         memcpy(eh, &hdr, sizeof(hdr));
0401     }
0402 
0403     /* Calculate Michael MIC */
0404     if (do_mic) {
0405         size_t len = skb->len - ETH_HLEN;
0406         u8 *mic = &mic_buf[0];
0407 
0408         /* Have to write to an even address, so copy the spare
0409          * byte across */
0410         if (skb->len % 2) {
0411             *mic = skb->data[skb->len - 1];
0412             mic++;
0413         }
0414 
0415         orinoco_mic(priv->tx_tfm_mic, key->tx_mic,
0416                 eh->h_dest, eh->h_source, 0 /* priority */,
0417                 skb->data + ETH_HLEN,
0418                 len, mic);
0419     }
0420 
0421     return 0;
0422 }
0423 EXPORT_SYMBOL(orinoco_process_xmit_skb);
0424 
0425 static netdev_tx_t orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
0426 {
0427     struct orinoco_private *priv = ndev_priv(dev);
0428     struct net_device_stats *stats = &dev->stats;
0429     struct hermes *hw = &priv->hw;
0430     int err = 0;
0431     u16 txfid = priv->txfid;
0432     int tx_control;
0433     unsigned long flags;
0434     u8 mic_buf[MICHAEL_MIC_LEN + 1];
0435 
0436     if (!netif_running(dev)) {
0437         printk(KERN_ERR "%s: Tx on stopped device!\n",
0438                dev->name);
0439         return NETDEV_TX_BUSY;
0440     }
0441 
0442     if (netif_queue_stopped(dev)) {
0443         printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
0444                dev->name);
0445         return NETDEV_TX_BUSY;
0446     }
0447 
0448     if (orinoco_lock(priv, &flags) != 0) {
0449         printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
0450                dev->name);
0451         return NETDEV_TX_BUSY;
0452     }
0453 
0454     if (!netif_carrier_ok(dev) ||
0455         (priv->iw_mode == NL80211_IFTYPE_MONITOR)) {
0456         /* Oops, the firmware hasn't established a connection,
0457            silently drop the packet (this seems to be the
0458            safest approach). */
0459         goto drop;
0460     }
0461 
0462     /* Check packet length */
0463     if (skb->len < ETH_HLEN)
0464         goto drop;
0465 
0466     tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
0467 
0468     err = orinoco_process_xmit_skb(skb, dev, priv, &tx_control,
0469                        &mic_buf[0]);
0470     if (err)
0471         goto drop;
0472 
0473     if (priv->has_alt_txcntl) {
0474         /* WPA enabled firmwares have tx_cntl at the end of
0475          * the 802.11 header.  So write zeroed descriptor and
0476          * 802.11 header at the same time
0477          */
0478         char desc[HERMES_802_3_OFFSET];
0479         __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
0480 
0481         memset(&desc, 0, sizeof(desc));
0482 
0483         *txcntl = cpu_to_le16(tx_control);
0484         err = hw->ops->bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
0485                       txfid, 0);
0486         if (err) {
0487             if (net_ratelimit())
0488                 printk(KERN_ERR "%s: Error %d writing Tx "
0489                        "descriptor to BAP\n", dev->name, err);
0490             goto busy;
0491         }
0492     } else {
0493         struct hermes_tx_descriptor desc;
0494 
0495         memset(&desc, 0, sizeof(desc));
0496 
0497         desc.tx_control = cpu_to_le16(tx_control);
0498         err = hw->ops->bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
0499                       txfid, 0);
0500         if (err) {
0501             if (net_ratelimit())
0502                 printk(KERN_ERR "%s: Error %d writing Tx "
0503                        "descriptor to BAP\n", dev->name, err);
0504             goto busy;
0505         }
0506 
0507         /* Clear the 802.11 header and data length fields - some
0508          * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
0509          * if this isn't done. */
0510         hermes_clear_words(hw, HERMES_DATA0,
0511                    HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
0512     }
0513 
0514     err = hw->ops->bap_pwrite(hw, USER_BAP, skb->data, skb->len,
0515                   txfid, HERMES_802_3_OFFSET);
0516     if (err) {
0517         printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
0518                dev->name, err);
0519         goto busy;
0520     }
0521 
0522     if (tx_control & HERMES_TXCTRL_MIC) {
0523         size_t offset = HERMES_802_3_OFFSET + skb->len;
0524         size_t len = MICHAEL_MIC_LEN;
0525 
0526         if (offset % 2) {
0527             offset--;
0528             len++;
0529         }
0530         err = hw->ops->bap_pwrite(hw, USER_BAP, &mic_buf[0], len,
0531                       txfid, offset);
0532         if (err) {
0533             printk(KERN_ERR "%s: Error %d writing MIC to BAP\n",
0534                    dev->name, err);
0535             goto busy;
0536         }
0537     }
0538 
0539     /* Finally, we actually initiate the send */
0540     netif_stop_queue(dev);
0541 
0542     err = hw->ops->cmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
0543                 txfid, NULL);
0544     if (err) {
0545         netif_start_queue(dev);
0546         if (net_ratelimit())
0547             printk(KERN_ERR "%s: Error %d transmitting packet\n",
0548                 dev->name, err);
0549         goto busy;
0550     }
0551 
0552     stats->tx_bytes += HERMES_802_3_OFFSET + skb->len;
0553     goto ok;
0554 
0555  drop:
0556     stats->tx_errors++;
0557     stats->tx_dropped++;
0558 
0559  ok:
0560     orinoco_unlock(priv, &flags);
0561     dev_kfree_skb(skb);
0562     return NETDEV_TX_OK;
0563 
0564  busy:
0565     if (err == -EIO)
0566         schedule_work(&priv->reset_work);
0567     orinoco_unlock(priv, &flags);
0568     return NETDEV_TX_BUSY;
0569 }
0570 
0571 static void __orinoco_ev_alloc(struct net_device *dev, struct hermes *hw)
0572 {
0573     struct orinoco_private *priv = ndev_priv(dev);
0574     u16 fid = hermes_read_regn(hw, ALLOCFID);
0575 
0576     if (fid != priv->txfid) {
0577         if (fid != DUMMY_FID)
0578             printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
0579                    dev->name, fid);
0580         return;
0581     }
0582 
0583     hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
0584 }
0585 
0586 static void __orinoco_ev_tx(struct net_device *dev, struct hermes *hw)
0587 {
0588     dev->stats.tx_packets++;
0589 
0590     netif_wake_queue(dev);
0591 
0592     hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
0593 }
0594 
0595 static void __orinoco_ev_txexc(struct net_device *dev, struct hermes *hw)
0596 {
0597     struct net_device_stats *stats = &dev->stats;
0598     u16 fid = hermes_read_regn(hw, TXCOMPLFID);
0599     u16 status;
0600     struct hermes_txexc_data hdr;
0601     int err = 0;
0602 
0603     if (fid == DUMMY_FID)
0604         return; /* Nothing's really happened */
0605 
0606     /* Read part of the frame header - we need status and addr1 */
0607     err = hw->ops->bap_pread(hw, IRQ_BAP, &hdr,
0608                  sizeof(struct hermes_txexc_data),
0609                  fid, 0);
0610 
0611     hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
0612     stats->tx_errors++;
0613 
0614     if (err) {
0615         printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
0616                "(FID=%04X error %d)\n",
0617                dev->name, fid, err);
0618         return;
0619     }
0620 
0621     DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
0622           err, fid);
0623 
0624     /* We produce a TXDROP event only for retry or lifetime
0625      * exceeded, because that's the only status that really mean
0626      * that this particular node went away.
0627      * Other errors means that *we* screwed up. - Jean II */
0628     status = le16_to_cpu(hdr.desc.status);
0629     if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
0630         union iwreq_data    wrqu;
0631 
0632         /* Copy 802.11 dest address.
0633          * We use the 802.11 header because the frame may
0634          * not be 802.3 or may be mangled...
0635          * In Ad-Hoc mode, it will be the node address.
0636          * In managed mode, it will be most likely the AP addr
0637          * User space will figure out how to convert it to
0638          * whatever it needs (IP address or else).
0639          * - Jean II */
0640         memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
0641         wrqu.addr.sa_family = ARPHRD_ETHER;
0642 
0643         /* Send event to user space */
0644         wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
0645     }
0646 
0647     netif_wake_queue(dev);
0648 }
0649 
0650 void orinoco_tx_timeout(struct net_device *dev, unsigned int txqueue)
0651 {
0652     struct orinoco_private *priv = ndev_priv(dev);
0653     struct net_device_stats *stats = &dev->stats;
0654     struct hermes *hw = &priv->hw;
0655 
0656     printk(KERN_WARNING "%s: Tx timeout! "
0657            "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
0658            dev->name, hermes_read_regn(hw, ALLOCFID),
0659            hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
0660 
0661     stats->tx_errors++;
0662 
0663     schedule_work(&priv->reset_work);
0664 }
0665 EXPORT_SYMBOL(orinoco_tx_timeout);
0666 
0667 /********************************************************************/
0668 /* Rx path (data frames)                                            */
0669 /********************************************************************/
0670 
0671 /* Does the frame have a SNAP header indicating it should be
0672  * de-encapsulated to Ethernet-II? */
0673 static inline int is_ethersnap(void *_hdr)
0674 {
0675     u8 *hdr = _hdr;
0676 
0677     /* We de-encapsulate all packets which, a) have SNAP headers
0678      * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
0679      * and where b) the OUI of the SNAP header is 00:00:00 or
0680      * 00:00:f8 - we need both because different APs appear to use
0681      * different OUIs for some reason */
0682     return (memcmp(hdr, &encaps_hdr, 5) == 0)
0683         && ((hdr[5] == 0x00) || (hdr[5] == 0xf8));
0684 }
0685 
0686 static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
0687                       int level, int noise)
0688 {
0689     struct iw_quality wstats;
0690     wstats.level = level - 0x95;
0691     wstats.noise = noise - 0x95;
0692     wstats.qual = (level > noise) ? (level - noise) : 0;
0693     wstats.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
0694     /* Update spy records */
0695     wireless_spy_update(dev, mac, &wstats);
0696 }
0697 
0698 static void orinoco_stat_gather(struct net_device *dev,
0699                 struct sk_buff *skb,
0700                 struct hermes_rx_descriptor *desc)
0701 {
0702     struct orinoco_private *priv = ndev_priv(dev);
0703 
0704     /* Using spy support with lots of Rx packets, like in an
0705      * infrastructure (AP), will really slow down everything, because
0706      * the MAC address must be compared to each entry of the spy list.
0707      * If the user really asks for it (set some address in the
0708      * spy list), we do it, but he will pay the price.
0709      * Note that to get here, you need both WIRELESS_SPY
0710      * compiled in AND some addresses in the list !!!
0711      */
0712     /* Note : gcc will optimise the whole section away if
0713      * WIRELESS_SPY is not defined... - Jean II */
0714     if (SPY_NUMBER(priv)) {
0715         orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
0716                    desc->signal, desc->silence);
0717     }
0718 }
0719 
0720 /*
0721  * orinoco_rx_monitor - handle received monitor frames.
0722  *
0723  * Arguments:
0724  *  dev     network device
0725  *  rxfid       received FID
0726  *  desc        rx descriptor of the frame
0727  *
0728  * Call context: interrupt
0729  */
0730 static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
0731                    struct hermes_rx_descriptor *desc)
0732 {
0733     u32 hdrlen = 30;    /* return full header by default */
0734     u32 datalen = 0;
0735     u16 fc;
0736     int err;
0737     int len;
0738     struct sk_buff *skb;
0739     struct orinoco_private *priv = ndev_priv(dev);
0740     struct net_device_stats *stats = &dev->stats;
0741     struct hermes *hw = &priv->hw;
0742 
0743     len = le16_to_cpu(desc->data_len);
0744 
0745     /* Determine the size of the header and the data */
0746     fc = le16_to_cpu(desc->frame_ctl);
0747     switch (fc & IEEE80211_FCTL_FTYPE) {
0748     case IEEE80211_FTYPE_DATA:
0749         if ((fc & IEEE80211_FCTL_TODS)
0750             && (fc & IEEE80211_FCTL_FROMDS))
0751             hdrlen = 30;
0752         else
0753             hdrlen = 24;
0754         datalen = len;
0755         break;
0756     case IEEE80211_FTYPE_MGMT:
0757         hdrlen = 24;
0758         datalen = len;
0759         break;
0760     case IEEE80211_FTYPE_CTL:
0761         switch (fc & IEEE80211_FCTL_STYPE) {
0762         case IEEE80211_STYPE_PSPOLL:
0763         case IEEE80211_STYPE_RTS:
0764         case IEEE80211_STYPE_CFEND:
0765         case IEEE80211_STYPE_CFENDACK:
0766             hdrlen = 16;
0767             break;
0768         case IEEE80211_STYPE_CTS:
0769         case IEEE80211_STYPE_ACK:
0770             hdrlen = 10;
0771             break;
0772         }
0773         break;
0774     default:
0775         /* Unknown frame type */
0776         break;
0777     }
0778 
0779     /* sanity check the length */
0780     if (datalen > IEEE80211_MAX_DATA_LEN + 12) {
0781         printk(KERN_DEBUG "%s: oversized monitor frame, "
0782                "data length = %d\n", dev->name, datalen);
0783         stats->rx_length_errors++;
0784         goto update_stats;
0785     }
0786 
0787     skb = dev_alloc_skb(hdrlen + datalen);
0788     if (!skb) {
0789         printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
0790                dev->name);
0791         goto update_stats;
0792     }
0793 
0794     /* Copy the 802.11 header to the skb */
0795     skb_put_data(skb, &(desc->frame_ctl), hdrlen);
0796     skb_reset_mac_header(skb);
0797 
0798     /* If any, copy the data from the card to the skb */
0799     if (datalen > 0) {
0800         err = hw->ops->bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
0801                      ALIGN(datalen, 2), rxfid,
0802                      HERMES_802_2_OFFSET);
0803         if (err) {
0804             printk(KERN_ERR "%s: error %d reading monitor frame\n",
0805                    dev->name, err);
0806             goto drop;
0807         }
0808     }
0809 
0810     skb->dev = dev;
0811     skb->ip_summed = CHECKSUM_NONE;
0812     skb->pkt_type = PACKET_OTHERHOST;
0813     skb->protocol = cpu_to_be16(ETH_P_802_2);
0814 
0815     stats->rx_packets++;
0816     stats->rx_bytes += skb->len;
0817 
0818     netif_rx(skb);
0819     return;
0820 
0821  drop:
0822     dev_kfree_skb_irq(skb);
0823  update_stats:
0824     stats->rx_errors++;
0825     stats->rx_dropped++;
0826 }
0827 
0828 void __orinoco_ev_rx(struct net_device *dev, struct hermes *hw)
0829 {
0830     struct orinoco_private *priv = ndev_priv(dev);
0831     struct net_device_stats *stats = &dev->stats;
0832     struct iw_statistics *wstats = &priv->wstats;
0833     struct sk_buff *skb = NULL;
0834     u16 rxfid, status;
0835     int length;
0836     struct hermes_rx_descriptor *desc;
0837     struct orinoco_rx_data *rx_data;
0838     int err;
0839 
0840     desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
0841     if (!desc)
0842         goto update_stats;
0843 
0844     rxfid = hermes_read_regn(hw, RXFID);
0845 
0846     err = hw->ops->bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
0847                  rxfid, 0);
0848     if (err) {
0849         printk(KERN_ERR "%s: error %d reading Rx descriptor. "
0850                "Frame dropped.\n", dev->name, err);
0851         goto update_stats;
0852     }
0853 
0854     status = le16_to_cpu(desc->status);
0855 
0856     if (status & HERMES_RXSTAT_BADCRC) {
0857         DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
0858               dev->name);
0859         stats->rx_crc_errors++;
0860         goto update_stats;
0861     }
0862 
0863     /* Handle frames in monitor mode */
0864     if (priv->iw_mode == NL80211_IFTYPE_MONITOR) {
0865         orinoco_rx_monitor(dev, rxfid, desc);
0866         goto out;
0867     }
0868 
0869     if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
0870         DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
0871               dev->name);
0872         wstats->discard.code++;
0873         goto update_stats;
0874     }
0875 
0876     length = le16_to_cpu(desc->data_len);
0877 
0878     /* Sanity checks */
0879     if (length < 3) { /* No for even an 802.2 LLC header */
0880         /* At least on Symbol firmware with PCF we get quite a
0881            lot of these legitimately - Poll frames with no
0882            data. */
0883         goto out;
0884     }
0885     if (length > IEEE80211_MAX_DATA_LEN) {
0886         printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
0887                dev->name, length);
0888         stats->rx_length_errors++;
0889         goto update_stats;
0890     }
0891 
0892     /* Payload size does not include Michael MIC. Increase payload
0893      * size to read it together with the data. */
0894     if (status & HERMES_RXSTAT_MIC)
0895         length += MICHAEL_MIC_LEN;
0896 
0897     /* We need space for the packet data itself, plus an ethernet
0898        header, plus 2 bytes so we can align the IP header on a
0899        32bit boundary, plus 1 byte so we can read in odd length
0900        packets from the card, which has an IO granularity of 16
0901        bits */
0902     skb = dev_alloc_skb(length + ETH_HLEN + 2 + 1);
0903     if (!skb) {
0904         printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
0905                dev->name);
0906         goto update_stats;
0907     }
0908 
0909     /* We'll prepend the header, so reserve space for it.  The worst
0910        case is no decapsulation, when 802.3 header is prepended and
0911        nothing is removed.  2 is for aligning the IP header.  */
0912     skb_reserve(skb, ETH_HLEN + 2);
0913 
0914     err = hw->ops->bap_pread(hw, IRQ_BAP, skb_put(skb, length),
0915                  ALIGN(length, 2), rxfid,
0916                  HERMES_802_2_OFFSET);
0917     if (err) {
0918         printk(KERN_ERR "%s: error %d reading frame. "
0919                "Frame dropped.\n", dev->name, err);
0920         goto drop;
0921     }
0922 
0923     /* Add desc and skb to rx queue */
0924     rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
0925     if (!rx_data)
0926         goto drop;
0927 
0928     rx_data->desc = desc;
0929     rx_data->skb = skb;
0930     list_add_tail(&rx_data->list, &priv->rx_list);
0931     tasklet_schedule(&priv->rx_tasklet);
0932 
0933     return;
0934 
0935 drop:
0936     dev_kfree_skb_irq(skb);
0937 update_stats:
0938     stats->rx_errors++;
0939     stats->rx_dropped++;
0940 out:
0941     kfree(desc);
0942 }
0943 EXPORT_SYMBOL(__orinoco_ev_rx);
0944 
0945 static void orinoco_rx(struct net_device *dev,
0946                struct hermes_rx_descriptor *desc,
0947                struct sk_buff *skb)
0948 {
0949     struct orinoco_private *priv = ndev_priv(dev);
0950     struct net_device_stats *stats = &dev->stats;
0951     u16 status, fc;
0952     int length;
0953     struct ethhdr *hdr;
0954 
0955     status = le16_to_cpu(desc->status);
0956     length = le16_to_cpu(desc->data_len);
0957     fc = le16_to_cpu(desc->frame_ctl);
0958 
0959     /* Calculate and check MIC */
0960     if (status & HERMES_RXSTAT_MIC) {
0961         struct orinoco_tkip_key *key;
0962         int key_id = ((status & HERMES_RXSTAT_MIC_KEY_ID) >>
0963                   HERMES_MIC_KEY_ID_SHIFT);
0964         u8 mic[MICHAEL_MIC_LEN];
0965         u8 *rxmic;
0966         u8 *src = (fc & IEEE80211_FCTL_FROMDS) ?
0967             desc->addr3 : desc->addr2;
0968 
0969         /* Extract Michael MIC from payload */
0970         rxmic = skb->data + skb->len - MICHAEL_MIC_LEN;
0971 
0972         skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
0973         length -= MICHAEL_MIC_LEN;
0974 
0975         key = (struct orinoco_tkip_key *) priv->keys[key_id].key;
0976 
0977         if (!key) {
0978             printk(KERN_WARNING "%s: Received encrypted frame from "
0979                    "%pM using key %i, but key is not installed\n",
0980                    dev->name, src, key_id);
0981             goto drop;
0982         }
0983 
0984         orinoco_mic(priv->rx_tfm_mic, key->rx_mic, desc->addr1, src,
0985                 0, /* priority or QoS? */
0986                 skb->data, skb->len, &mic[0]);
0987 
0988         if (memcmp(mic, rxmic,
0989                MICHAEL_MIC_LEN)) {
0990             union iwreq_data wrqu;
0991             struct iw_michaelmicfailure wxmic;
0992 
0993             printk(KERN_WARNING "%s: "
0994                    "Invalid Michael MIC in data frame from %pM, "
0995                    "using key %i\n",
0996                    dev->name, src, key_id);
0997 
0998             /* TODO: update stats */
0999 
1000             /* Notify userspace */
1001             memset(&wxmic, 0, sizeof(wxmic));
1002             wxmic.flags = key_id & IW_MICFAILURE_KEY_ID;
1003             wxmic.flags |= (desc->addr1[0] & 1) ?
1004                 IW_MICFAILURE_GROUP : IW_MICFAILURE_PAIRWISE;
1005             wxmic.src_addr.sa_family = ARPHRD_ETHER;
1006             memcpy(wxmic.src_addr.sa_data, src, ETH_ALEN);
1007 
1008             (void) orinoco_hw_get_tkip_iv(priv, key_id,
1009                               &wxmic.tsc[0]);
1010 
1011             memset(&wrqu, 0, sizeof(wrqu));
1012             wrqu.data.length = sizeof(wxmic);
1013             wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu,
1014                         (char *) &wxmic);
1015 
1016             goto drop;
1017         }
1018     }
1019 
1020     /* Handle decapsulation
1021      * In most cases, the firmware tell us about SNAP frames.
1022      * For some reason, the SNAP frames sent by LinkSys APs
1023      * are not properly recognised by most firmwares.
1024      * So, check ourselves */
1025     if (length >= ENCAPS_OVERHEAD &&
1026         (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
1027          ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
1028          is_ethersnap(skb->data))) {
1029         /* These indicate a SNAP within 802.2 LLC within
1030            802.11 frame which we'll need to de-encapsulate to
1031            the original EthernetII frame. */
1032         hdr = skb_push(skb, ETH_HLEN - ENCAPS_OVERHEAD);
1033     } else {
1034         /* 802.3 frame - prepend 802.3 header as is */
1035         hdr = skb_push(skb, ETH_HLEN);
1036         hdr->h_proto = htons(length);
1037     }
1038     memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
1039     if (fc & IEEE80211_FCTL_FROMDS)
1040         memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
1041     else
1042         memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
1043 
1044     skb->protocol = eth_type_trans(skb, dev);
1045     skb->ip_summed = CHECKSUM_NONE;
1046     if (fc & IEEE80211_FCTL_TODS)
1047         skb->pkt_type = PACKET_OTHERHOST;
1048 
1049     /* Process the wireless stats if needed */
1050     orinoco_stat_gather(dev, skb, desc);
1051 
1052     /* Pass the packet to the networking stack */
1053     netif_rx(skb);
1054     stats->rx_packets++;
1055     stats->rx_bytes += length;
1056 
1057     return;
1058 
1059  drop:
1060     dev_kfree_skb(skb);
1061     stats->rx_errors++;
1062     stats->rx_dropped++;
1063 }
1064 
1065 static void orinoco_rx_isr_tasklet(struct tasklet_struct *t)
1066 {
1067     struct orinoco_private *priv = from_tasklet(priv, t, rx_tasklet);
1068     struct net_device *dev = priv->ndev;
1069     struct orinoco_rx_data *rx_data, *temp;
1070     struct hermes_rx_descriptor *desc;
1071     struct sk_buff *skb;
1072     unsigned long flags;
1073 
1074     /* orinoco_rx requires the driver lock, and we also need to
1075      * protect priv->rx_list, so just hold the lock over the
1076      * lot.
1077      *
1078      * If orinoco_lock fails, we've unplugged the card. In this
1079      * case just abort. */
1080     if (orinoco_lock(priv, &flags) != 0)
1081         return;
1082 
1083     /* extract desc and skb from queue */
1084     list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
1085         desc = rx_data->desc;
1086         skb = rx_data->skb;
1087         list_del(&rx_data->list);
1088         kfree(rx_data);
1089 
1090         orinoco_rx(dev, desc, skb);
1091 
1092         kfree(desc);
1093     }
1094 
1095     orinoco_unlock(priv, &flags);
1096 }
1097 
1098 /********************************************************************/
1099 /* Rx path (info frames)                                            */
1100 /********************************************************************/
1101 
1102 static void print_linkstatus(struct net_device *dev, u16 status)
1103 {
1104     char *s;
1105 
1106     if (suppress_linkstatus)
1107         return;
1108 
1109     switch (status) {
1110     case HERMES_LINKSTATUS_NOT_CONNECTED:
1111         s = "Not Connected";
1112         break;
1113     case HERMES_LINKSTATUS_CONNECTED:
1114         s = "Connected";
1115         break;
1116     case HERMES_LINKSTATUS_DISCONNECTED:
1117         s = "Disconnected";
1118         break;
1119     case HERMES_LINKSTATUS_AP_CHANGE:
1120         s = "AP Changed";
1121         break;
1122     case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1123         s = "AP Out of Range";
1124         break;
1125     case HERMES_LINKSTATUS_AP_IN_RANGE:
1126         s = "AP In Range";
1127         break;
1128     case HERMES_LINKSTATUS_ASSOC_FAILED:
1129         s = "Association Failed";
1130         break;
1131     default:
1132         s = "UNKNOWN";
1133     }
1134 
1135     printk(KERN_DEBUG "%s: New link status: %s (%04x)\n",
1136            dev->name, s, status);
1137 }
1138 
1139 /* Search scan results for requested BSSID, join it if found */
1140 static void orinoco_join_ap(struct work_struct *work)
1141 {
1142     struct orinoco_private *priv =
1143         container_of(work, struct orinoco_private, join_work);
1144     struct net_device *dev = priv->ndev;
1145     struct hermes *hw = &priv->hw;
1146     int err;
1147     unsigned long flags;
1148     struct join_req {
1149         u8 bssid[ETH_ALEN];
1150         __le16 channel;
1151     } __packed req;
1152     const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
1153     struct prism2_scan_apinfo *atom = NULL;
1154     int offset = 4;
1155     int found = 0;
1156     u8 *buf;
1157     u16 len;
1158 
1159     /* Allocate buffer for scan results */
1160     buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
1161     if (!buf)
1162         return;
1163 
1164     if (orinoco_lock(priv, &flags) != 0)
1165         goto fail_lock;
1166 
1167     /* Sanity checks in case user changed something in the meantime */
1168     if (!priv->bssid_fixed)
1169         goto out;
1170 
1171     if (strlen(priv->desired_essid) == 0)
1172         goto out;
1173 
1174     /* Read scan results from the firmware */
1175     err = hw->ops->read_ltv(hw, USER_BAP,
1176                 HERMES_RID_SCANRESULTSTABLE,
1177                 MAX_SCAN_LEN, &len, buf);
1178     if (err) {
1179         printk(KERN_ERR "%s: Cannot read scan results\n",
1180                dev->name);
1181         goto out;
1182     }
1183 
1184     len = HERMES_RECLEN_TO_BYTES(len);
1185 
1186     /* Go through the scan results looking for the channel of the AP
1187      * we were requested to join */
1188     for (; offset + atom_len <= len; offset += atom_len) {
1189         atom = (struct prism2_scan_apinfo *) (buf + offset);
1190         if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1191             found = 1;
1192             break;
1193         }
1194     }
1195 
1196     if (!found) {
1197         DEBUG(1, "%s: Requested AP not found in scan results\n",
1198               dev->name);
1199         goto out;
1200     }
1201 
1202     memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1203     req.channel = atom->channel;    /* both are little-endian */
1204     err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1205                   &req);
1206     if (err)
1207         printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1208 
1209  out:
1210     orinoco_unlock(priv, &flags);
1211 
1212  fail_lock:
1213     kfree(buf);
1214 }
1215 
1216 /* Send new BSSID to userspace */
1217 static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
1218 {
1219     struct net_device *dev = priv->ndev;
1220     struct hermes *hw = &priv->hw;
1221     union iwreq_data wrqu;
1222     int err;
1223 
1224     err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
1225                 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1226     if (err != 0)
1227         return;
1228 
1229     wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1230 
1231     /* Send event to user space */
1232     wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
1233 }
1234 
1235 static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
1236 {
1237     struct net_device *dev = priv->ndev;
1238     struct hermes *hw = &priv->hw;
1239     union iwreq_data wrqu;
1240     int err;
1241     u8 buf[88];
1242     u8 *ie;
1243 
1244     if (!priv->has_wpa)
1245         return;
1246 
1247     err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
1248                 sizeof(buf), NULL, &buf);
1249     if (err != 0)
1250         return;
1251 
1252     ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1253     if (ie) {
1254         int rem = sizeof(buf) - (ie - &buf[0]);
1255         wrqu.data.length = ie[1] + 2;
1256         if (wrqu.data.length > rem)
1257             wrqu.data.length = rem;
1258 
1259         if (wrqu.data.length)
1260             /* Send event to user space */
1261             wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
1262     }
1263 }
1264 
1265 static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
1266 {
1267     struct net_device *dev = priv->ndev;
1268     struct hermes *hw = &priv->hw;
1269     union iwreq_data wrqu;
1270     int err;
1271     u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
1272     u8 *ie;
1273 
1274     if (!priv->has_wpa)
1275         return;
1276 
1277     err = hw->ops->read_ltv(hw, USER_BAP,
1278                 HERMES_RID_CURRENT_ASSOC_RESP_INFO,
1279                 sizeof(buf), NULL, &buf);
1280     if (err != 0)
1281         return;
1282 
1283     ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1284     if (ie) {
1285         int rem = sizeof(buf) - (ie - &buf[0]);
1286         wrqu.data.length = ie[1] + 2;
1287         if (wrqu.data.length > rem)
1288             wrqu.data.length = rem;
1289 
1290         if (wrqu.data.length)
1291             /* Send event to user space */
1292             wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
1293     }
1294 }
1295 
1296 static void orinoco_send_wevents(struct work_struct *work)
1297 {
1298     struct orinoco_private *priv =
1299         container_of(work, struct orinoco_private, wevent_work);
1300     unsigned long flags;
1301 
1302     if (orinoco_lock(priv, &flags) != 0)
1303         return;
1304 
1305     orinoco_send_assocreqie_wevent(priv);
1306     orinoco_send_assocrespie_wevent(priv);
1307     orinoco_send_bssid_wevent(priv);
1308 
1309     orinoco_unlock(priv, &flags);
1310 }
1311 
1312 static void qbuf_scan(struct orinoco_private *priv, void *buf,
1313               int len, int type)
1314 {
1315     struct orinoco_scan_data *sd;
1316     unsigned long flags;
1317 
1318     sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
1319     if (!sd)
1320         return;
1321 
1322     sd->buf = buf;
1323     sd->len = len;
1324     sd->type = type;
1325 
1326     spin_lock_irqsave(&priv->scan_lock, flags);
1327     list_add_tail(&sd->list, &priv->scan_list);
1328     spin_unlock_irqrestore(&priv->scan_lock, flags);
1329 
1330     schedule_work(&priv->process_scan);
1331 }
1332 
1333 static void qabort_scan(struct orinoco_private *priv)
1334 {
1335     struct orinoco_scan_data *sd;
1336     unsigned long flags;
1337 
1338     sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
1339     if (!sd)
1340         return;
1341 
1342     sd->len = -1; /* Abort */
1343 
1344     spin_lock_irqsave(&priv->scan_lock, flags);
1345     list_add_tail(&sd->list, &priv->scan_list);
1346     spin_unlock_irqrestore(&priv->scan_lock, flags);
1347 
1348     schedule_work(&priv->process_scan);
1349 }
1350 
1351 static void orinoco_process_scan_results(struct work_struct *work)
1352 {
1353     struct orinoco_private *priv =
1354         container_of(work, struct orinoco_private, process_scan);
1355     struct orinoco_scan_data *sd, *temp;
1356     unsigned long flags;
1357     void *buf;
1358     int len;
1359     int type;
1360 
1361     spin_lock_irqsave(&priv->scan_lock, flags);
1362     list_for_each_entry_safe(sd, temp, &priv->scan_list, list) {
1363 
1364         buf = sd->buf;
1365         len = sd->len;
1366         type = sd->type;
1367 
1368         list_del(&sd->list);
1369         spin_unlock_irqrestore(&priv->scan_lock, flags);
1370         kfree(sd);
1371 
1372         if (len > 0) {
1373             if (type == HERMES_INQ_CHANNELINFO)
1374                 orinoco_add_extscan_result(priv, buf, len);
1375             else
1376                 orinoco_add_hostscan_results(priv, buf, len);
1377 
1378             kfree(buf);
1379         } else {
1380             /* Either abort or complete the scan */
1381             orinoco_scan_done(priv, (len < 0));
1382         }
1383 
1384         spin_lock_irqsave(&priv->scan_lock, flags);
1385     }
1386     spin_unlock_irqrestore(&priv->scan_lock, flags);
1387 }
1388 
1389 void __orinoco_ev_info(struct net_device *dev, struct hermes *hw)
1390 {
1391     struct orinoco_private *priv = ndev_priv(dev);
1392     u16 infofid;
1393     struct {
1394         __le16 len;
1395         __le16 type;
1396     } __packed info;
1397     int len, type;
1398     int err;
1399 
1400     /* This is an answer to an INQUIRE command that we did earlier,
1401      * or an information "event" generated by the card
1402      * The controller return to us a pseudo frame containing
1403      * the information in question - Jean II */
1404     infofid = hermes_read_regn(hw, INFOFID);
1405 
1406     /* Read the info frame header - don't try too hard */
1407     err = hw->ops->bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1408                  infofid, 0);
1409     if (err) {
1410         printk(KERN_ERR "%s: error %d reading info frame. "
1411                "Frame dropped.\n", dev->name, err);
1412         return;
1413     }
1414 
1415     len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1416     type = le16_to_cpu(info.type);
1417 
1418     switch (type) {
1419     case HERMES_INQ_TALLIES: {
1420         struct hermes_tallies_frame tallies;
1421         struct iw_statistics *wstats = &priv->wstats;
1422 
1423         if (len > sizeof(tallies)) {
1424             printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1425                    dev->name, len);
1426             len = sizeof(tallies);
1427         }
1428 
1429         err = hw->ops->bap_pread(hw, IRQ_BAP, &tallies, len,
1430                      infofid, sizeof(info));
1431         if (err)
1432             break;
1433 
1434         /* Increment our various counters */
1435         /* wstats->discard.nwid - no wrong BSSID stuff */
1436         wstats->discard.code +=
1437             le16_to_cpu(tallies.RxWEPUndecryptable);
1438         if (len == sizeof(tallies))
1439             wstats->discard.code +=
1440                 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1441                 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1442         wstats->discard.misc +=
1443             le16_to_cpu(tallies.TxDiscardsWrongSA);
1444         wstats->discard.fragment +=
1445             le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1446         wstats->discard.retries +=
1447             le16_to_cpu(tallies.TxRetryLimitExceeded);
1448         /* wstats->miss.beacon - no match */
1449     }
1450     break;
1451     case HERMES_INQ_LINKSTATUS: {
1452         struct hermes_linkstatus linkstatus;
1453         u16 newstatus;
1454         int connected;
1455 
1456         if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
1457             break;
1458 
1459         if (len != sizeof(linkstatus)) {
1460             printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1461                    dev->name, len);
1462             break;
1463         }
1464 
1465         err = hw->ops->bap_pread(hw, IRQ_BAP, &linkstatus, len,
1466                      infofid, sizeof(info));
1467         if (err)
1468             break;
1469         newstatus = le16_to_cpu(linkstatus.linkstatus);
1470 
1471         /* Symbol firmware uses "out of range" to signal that
1472          * the hostscan frame can be requested.  */
1473         if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
1474             priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
1475             priv->has_hostscan && priv->scan_request) {
1476             hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
1477             break;
1478         }
1479 
1480         connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
1481             || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1482             || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
1483 
1484         if (connected)
1485             netif_carrier_on(dev);
1486         else if (!ignore_disconnect)
1487             netif_carrier_off(dev);
1488 
1489         if (newstatus != priv->last_linkstatus) {
1490             priv->last_linkstatus = newstatus;
1491             print_linkstatus(dev, newstatus);
1492             /* The info frame contains only one word which is the
1493              * status (see hermes.h). The status is pretty boring
1494              * in itself, that's why we export the new BSSID...
1495              * Jean II */
1496             schedule_work(&priv->wevent_work);
1497         }
1498     }
1499     break;
1500     case HERMES_INQ_SCAN:
1501         if (!priv->scan_request && priv->bssid_fixed &&
1502             priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
1503             schedule_work(&priv->join_work);
1504             break;
1505         }
1506         fallthrough;
1507     case HERMES_INQ_HOSTSCAN:
1508     case HERMES_INQ_HOSTSCAN_SYMBOL: {
1509         /* Result of a scanning. Contains information about
1510          * cells in the vicinity - Jean II */
1511         unsigned char *buf;
1512 
1513         /* Sanity check */
1514         if (len > 4096) {
1515             printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
1516                    dev->name, len);
1517             qabort_scan(priv);
1518             break;
1519         }
1520 
1521         /* Allocate buffer for results */
1522         buf = kmalloc(len, GFP_ATOMIC);
1523         if (buf == NULL) {
1524             /* No memory, so can't printk()... */
1525             qabort_scan(priv);
1526             break;
1527         }
1528 
1529         /* Read scan data */
1530         err = hw->ops->bap_pread(hw, IRQ_BAP, (void *) buf, len,
1531                      infofid, sizeof(info));
1532         if (err) {
1533             kfree(buf);
1534             qabort_scan(priv);
1535             break;
1536         }
1537 
1538 #ifdef ORINOCO_DEBUG
1539         {
1540             int i;
1541             printk(KERN_DEBUG "Scan result [%02X", buf[0]);
1542             for (i = 1; i < (len * 2); i++)
1543                 printk(":%02X", buf[i]);
1544             printk("]\n");
1545         }
1546 #endif  /* ORINOCO_DEBUG */
1547 
1548         qbuf_scan(priv, buf, len, type);
1549     }
1550     break;
1551     case HERMES_INQ_CHANNELINFO:
1552     {
1553         struct agere_ext_scan_info *bss;
1554 
1555         if (!priv->scan_request) {
1556             printk(KERN_DEBUG "%s: Got chaninfo without scan, "
1557                    "len=%d\n", dev->name, len);
1558             break;
1559         }
1560 
1561         /* An empty result indicates that the scan is complete */
1562         if (len == 0) {
1563             qbuf_scan(priv, NULL, len, type);
1564             break;
1565         }
1566 
1567         /* Sanity check */
1568         else if (len < (offsetof(struct agere_ext_scan_info,
1569                        data) + 2)) {
1570             /* Drop this result now so we don't have to
1571              * keep checking later */
1572             printk(KERN_WARNING
1573                    "%s: Ext scan results too short (%d bytes)\n",
1574                    dev->name, len);
1575             break;
1576         }
1577 
1578         bss = kmalloc(len, GFP_ATOMIC);
1579         if (bss == NULL)
1580             break;
1581 
1582         /* Read scan data */
1583         err = hw->ops->bap_pread(hw, IRQ_BAP, (void *) bss, len,
1584                      infofid, sizeof(info));
1585         if (err)
1586             kfree(bss);
1587         else
1588             qbuf_scan(priv, bss, len, type);
1589 
1590         break;
1591     }
1592     case HERMES_INQ_SEC_STAT_AGERE:
1593         /* Security status (Agere specific) */
1594         /* Ignore this frame for now */
1595         if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
1596             break;
1597         fallthrough;
1598     default:
1599         printk(KERN_DEBUG "%s: Unknown information frame received: "
1600                "type 0x%04x, length %d\n", dev->name, type, len);
1601         /* We don't actually do anything about it */
1602         break;
1603     }
1604 }
1605 EXPORT_SYMBOL(__orinoco_ev_info);
1606 
1607 static void __orinoco_ev_infdrop(struct net_device *dev, struct hermes *hw)
1608 {
1609     if (net_ratelimit())
1610         printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
1611 }
1612 
1613 /********************************************************************/
1614 /* Internal hardware control routines                               */
1615 /********************************************************************/
1616 
1617 static int __orinoco_up(struct orinoco_private *priv)
1618 {
1619     struct net_device *dev = priv->ndev;
1620     struct hermes *hw = &priv->hw;
1621     int err;
1622 
1623     netif_carrier_off(dev); /* just to make sure */
1624 
1625     err = __orinoco_commit(priv);
1626     if (err) {
1627         printk(KERN_ERR "%s: Error %d configuring card\n",
1628                dev->name, err);
1629         return err;
1630     }
1631 
1632     /* Fire things up again */
1633     hermes_set_irqmask(hw, ORINOCO_INTEN);
1634     err = hermes_enable_port(hw, 0);
1635     if (err) {
1636         printk(KERN_ERR "%s: Error %d enabling MAC port\n",
1637                dev->name, err);
1638         return err;
1639     }
1640 
1641     netif_start_queue(dev);
1642 
1643     return 0;
1644 }
1645 
1646 static int __orinoco_down(struct orinoco_private *priv)
1647 {
1648     struct net_device *dev = priv->ndev;
1649     struct hermes *hw = &priv->hw;
1650     int err;
1651 
1652     netif_stop_queue(dev);
1653 
1654     if (!priv->hw_unavailable) {
1655         if (!priv->broken_disableport) {
1656             err = hermes_disable_port(hw, 0);
1657             if (err) {
1658                 /* Some firmwares (e.g. Intersil 1.3.x) seem
1659                  * to have problems disabling the port, oh
1660                  * well, too bad. */
1661                 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
1662                        dev->name, err);
1663                 priv->broken_disableport = 1;
1664             }
1665         }
1666         hermes_set_irqmask(hw, 0);
1667         hermes_write_regn(hw, EVACK, 0xffff);
1668     }
1669 
1670     orinoco_scan_done(priv, true);
1671 
1672     /* firmware will have to reassociate */
1673     netif_carrier_off(dev);
1674     priv->last_linkstatus = 0xffff;
1675 
1676     return 0;
1677 }
1678 
1679 static int orinoco_reinit_firmware(struct orinoco_private *priv)
1680 {
1681     struct hermes *hw = &priv->hw;
1682     int err;
1683 
1684     err = hw->ops->init(hw);
1685     if (priv->do_fw_download && !err) {
1686         err = orinoco_download(priv);
1687         if (err)
1688             priv->do_fw_download = 0;
1689     }
1690     if (!err)
1691         err = orinoco_hw_allocate_fid(priv);
1692 
1693     return err;
1694 }
1695 
1696 static int
1697 __orinoco_set_multicast_list(struct net_device *dev)
1698 {
1699     struct orinoco_private *priv = ndev_priv(dev);
1700     int err = 0;
1701     int promisc, mc_count;
1702 
1703     /* The Hermes doesn't seem to have an allmulti mode, so we go
1704      * into promiscuous mode and let the upper levels deal. */
1705     if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
1706         (netdev_mc_count(dev) > MAX_MULTICAST(priv))) {
1707         promisc = 1;
1708         mc_count = 0;
1709     } else {
1710         promisc = 0;
1711         mc_count = netdev_mc_count(dev);
1712     }
1713 
1714     err = __orinoco_hw_set_multicast_list(priv, dev, mc_count, promisc);
1715 
1716     return err;
1717 }
1718 
1719 /* This must be called from user context, without locks held - use
1720  * schedule_work() */
1721 void orinoco_reset(struct work_struct *work)
1722 {
1723     struct orinoco_private *priv =
1724         container_of(work, struct orinoco_private, reset_work);
1725     struct net_device *dev = priv->ndev;
1726     struct hermes *hw = &priv->hw;
1727     int err;
1728     unsigned long flags;
1729 
1730     if (orinoco_lock(priv, &flags) != 0)
1731         /* When the hardware becomes available again, whatever
1732          * detects that is responsible for re-initializing
1733          * it. So no need for anything further */
1734         return;
1735 
1736     netif_stop_queue(dev);
1737 
1738     /* Shut off interrupts.  Depending on what state the hardware
1739      * is in, this might not work, but we'll try anyway */
1740     hermes_set_irqmask(hw, 0);
1741     hermes_write_regn(hw, EVACK, 0xffff);
1742 
1743     priv->hw_unavailable++;
1744     priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
1745     netif_carrier_off(dev);
1746 
1747     orinoco_unlock(priv, &flags);
1748 
1749     /* Scanning support: Notify scan cancellation */
1750     orinoco_scan_done(priv, true);
1751 
1752     if (priv->hard_reset) {
1753         err = (*priv->hard_reset)(priv);
1754         if (err) {
1755             printk(KERN_ERR "%s: orinoco_reset: Error %d "
1756                    "performing hard reset\n", dev->name, err);
1757             goto disable;
1758         }
1759     }
1760 
1761     err = orinoco_reinit_firmware(priv);
1762     if (err) {
1763         printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
1764                dev->name, err);
1765         goto disable;
1766     }
1767 
1768     /* This has to be called from user context */
1769     orinoco_lock_irq(priv);
1770 
1771     priv->hw_unavailable--;
1772 
1773     /* priv->open or priv->hw_unavailable might have changed while
1774      * we dropped the lock */
1775     if (priv->open && (!priv->hw_unavailable)) {
1776         err = __orinoco_up(priv);
1777         if (err) {
1778             printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
1779                    dev->name, err);
1780         } else
1781             netif_trans_update(dev);
1782     }
1783 
1784     orinoco_unlock_irq(priv);
1785 
1786     return;
1787  disable:
1788     hermes_set_irqmask(hw, 0);
1789     netif_device_detach(dev);
1790     printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
1791 }
1792 
1793 static int __orinoco_commit(struct orinoco_private *priv)
1794 {
1795     struct net_device *dev = priv->ndev;
1796     int err = 0;
1797 
1798     /* If we've called commit, we are reconfiguring or bringing the
1799      * interface up. Maintaining countermeasures across this would
1800      * be confusing, so note that we've disabled them. The port will
1801      * be enabled later in orinoco_commit or __orinoco_up. */
1802     priv->tkip_cm_active = 0;
1803 
1804     err = orinoco_hw_program_rids(priv);
1805 
1806     /* FIXME: what about netif_tx_lock */
1807     (void) __orinoco_set_multicast_list(dev);
1808 
1809     return err;
1810 }
1811 
1812 /* Ensures configuration changes are applied. May result in a reset.
1813  * The caller should hold priv->lock
1814  */
1815 int orinoco_commit(struct orinoco_private *priv)
1816 {
1817     struct net_device *dev = priv->ndev;
1818     struct hermes *hw = &priv->hw;
1819     int err;
1820 
1821     if (priv->broken_disableport) {
1822         schedule_work(&priv->reset_work);
1823         return 0;
1824     }
1825 
1826     err = hermes_disable_port(hw, 0);
1827     if (err) {
1828         printk(KERN_WARNING "%s: Unable to disable port "
1829                "while reconfiguring card\n", dev->name);
1830         priv->broken_disableport = 1;
1831         goto out;
1832     }
1833 
1834     err = __orinoco_commit(priv);
1835     if (err) {
1836         printk(KERN_WARNING "%s: Unable to reconfigure card\n",
1837                dev->name);
1838         goto out;
1839     }
1840 
1841     err = hermes_enable_port(hw, 0);
1842     if (err) {
1843         printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
1844                dev->name);
1845         goto out;
1846     }
1847 
1848  out:
1849     if (err) {
1850         printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
1851         schedule_work(&priv->reset_work);
1852         err = 0;
1853     }
1854     return err;
1855 }
1856 
1857 /********************************************************************/
1858 /* Interrupt handler                                                */
1859 /********************************************************************/
1860 
1861 static void __orinoco_ev_tick(struct net_device *dev, struct hermes *hw)
1862 {
1863     printk(KERN_DEBUG "%s: TICK\n", dev->name);
1864 }
1865 
1866 static void __orinoco_ev_wterr(struct net_device *dev, struct hermes *hw)
1867 {
1868     /* This seems to happen a fair bit under load, but ignoring it
1869        seems to work fine...*/
1870     printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
1871            dev->name);
1872 }
1873 
1874 irqreturn_t orinoco_interrupt(int irq, void *dev_id)
1875 {
1876     struct orinoco_private *priv = dev_id;
1877     struct net_device *dev = priv->ndev;
1878     struct hermes *hw = &priv->hw;
1879     int count = MAX_IRQLOOPS_PER_IRQ;
1880     u16 evstat, events;
1881     /* These are used to detect a runaway interrupt situation.
1882      *
1883      * If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
1884      * we panic and shut down the hardware
1885      */
1886     /* jiffies value the last time we were called */
1887     static int last_irq_jiffy; /* = 0 */
1888     static int loops_this_jiffy; /* = 0 */
1889     unsigned long flags;
1890 
1891     if (orinoco_lock(priv, &flags) != 0) {
1892         /* If hw is unavailable - we don't know if the irq was
1893          * for us or not */
1894         return IRQ_HANDLED;
1895     }
1896 
1897     evstat = hermes_read_regn(hw, EVSTAT);
1898     events = evstat & hw->inten;
1899     if (!events) {
1900         orinoco_unlock(priv, &flags);
1901         return IRQ_NONE;
1902     }
1903 
1904     if (jiffies != last_irq_jiffy)
1905         loops_this_jiffy = 0;
1906     last_irq_jiffy = jiffies;
1907 
1908     while (events && count--) {
1909         if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
1910             printk(KERN_WARNING "%s: IRQ handler is looping too "
1911                    "much! Resetting.\n", dev->name);
1912             /* Disable interrupts for now */
1913             hermes_set_irqmask(hw, 0);
1914             schedule_work(&priv->reset_work);
1915             break;
1916         }
1917 
1918         /* Check the card hasn't been removed */
1919         if (!hermes_present(hw)) {
1920             DEBUG(0, "orinoco_interrupt(): card removed\n");
1921             break;
1922         }
1923 
1924         if (events & HERMES_EV_TICK)
1925             __orinoco_ev_tick(dev, hw);
1926         if (events & HERMES_EV_WTERR)
1927             __orinoco_ev_wterr(dev, hw);
1928         if (events & HERMES_EV_INFDROP)
1929             __orinoco_ev_infdrop(dev, hw);
1930         if (events & HERMES_EV_INFO)
1931             __orinoco_ev_info(dev, hw);
1932         if (events & HERMES_EV_RX)
1933             __orinoco_ev_rx(dev, hw);
1934         if (events & HERMES_EV_TXEXC)
1935             __orinoco_ev_txexc(dev, hw);
1936         if (events & HERMES_EV_TX)
1937             __orinoco_ev_tx(dev, hw);
1938         if (events & HERMES_EV_ALLOC)
1939             __orinoco_ev_alloc(dev, hw);
1940 
1941         hermes_write_regn(hw, EVACK, evstat);
1942 
1943         evstat = hermes_read_regn(hw, EVSTAT);
1944         events = evstat & hw->inten;
1945     }
1946 
1947     orinoco_unlock(priv, &flags);
1948     return IRQ_HANDLED;
1949 }
1950 EXPORT_SYMBOL(orinoco_interrupt);
1951 
1952 /********************************************************************/
1953 /* Power management                                                 */
1954 /********************************************************************/
1955 #if defined(CONFIG_PM_SLEEP) && !defined(CONFIG_HERMES_CACHE_FW_ON_INIT)
1956 static int orinoco_pm_notifier(struct notifier_block *notifier,
1957                    unsigned long pm_event,
1958                    void *unused)
1959 {
1960     struct orinoco_private *priv = container_of(notifier,
1961                             struct orinoco_private,
1962                             pm_notifier);
1963 
1964     /* All we need to do is cache the firmware before suspend, and
1965      * release it when we come out.
1966      *
1967      * Only need to do this if we're downloading firmware. */
1968     if (!priv->do_fw_download)
1969         return NOTIFY_DONE;
1970 
1971     switch (pm_event) {
1972     case PM_HIBERNATION_PREPARE:
1973     case PM_SUSPEND_PREPARE:
1974         orinoco_cache_fw(priv, 0);
1975         break;
1976 
1977     case PM_POST_RESTORE:
1978         /* Restore from hibernation failed. We need to clean
1979          * up in exactly the same way, so fall through. */
1980     case PM_POST_HIBERNATION:
1981     case PM_POST_SUSPEND:
1982         orinoco_uncache_fw(priv);
1983         break;
1984 
1985     case PM_RESTORE_PREPARE:
1986     default:
1987         break;
1988     }
1989 
1990     return NOTIFY_DONE;
1991 }
1992 
1993 static void orinoco_register_pm_notifier(struct orinoco_private *priv)
1994 {
1995     priv->pm_notifier.notifier_call = orinoco_pm_notifier;
1996     register_pm_notifier(&priv->pm_notifier);
1997 }
1998 
1999 static void orinoco_unregister_pm_notifier(struct orinoco_private *priv)
2000 {
2001     unregister_pm_notifier(&priv->pm_notifier);
2002 }
2003 #else /* !PM_SLEEP || HERMES_CACHE_FW_ON_INIT */
2004 #define orinoco_register_pm_notifier(priv) do { } while (0)
2005 #define orinoco_unregister_pm_notifier(priv) do { } while (0)
2006 #endif
2007 
2008 /********************************************************************/
2009 /* Initialization                                                   */
2010 /********************************************************************/
2011 
2012 int orinoco_init(struct orinoco_private *priv)
2013 {
2014     struct device *dev = priv->dev;
2015     struct wiphy *wiphy = priv_to_wiphy(priv);
2016     struct hermes *hw = &priv->hw;
2017     int err = 0;
2018 
2019     /* No need to lock, the hw_unavailable flag is already set in
2020      * alloc_orinocodev() */
2021     priv->nicbuf_size = IEEE80211_MAX_FRAME_LEN + ETH_HLEN;
2022 
2023     /* Initialize the firmware */
2024     err = hw->ops->init(hw);
2025     if (err != 0) {
2026         dev_err(dev, "Failed to initialize firmware (err = %d)\n",
2027             err);
2028         goto out;
2029     }
2030 
2031     err = determine_fw_capabilities(priv, wiphy->fw_version,
2032                     sizeof(wiphy->fw_version),
2033                     &wiphy->hw_version);
2034     if (err != 0) {
2035         dev_err(dev, "Incompatible firmware, aborting\n");
2036         goto out;
2037     }
2038 
2039     if (priv->do_fw_download) {
2040 #ifdef CONFIG_HERMES_CACHE_FW_ON_INIT
2041         orinoco_cache_fw(priv, 0);
2042 #endif
2043 
2044         err = orinoco_download(priv);
2045         if (err)
2046             priv->do_fw_download = 0;
2047 
2048         /* Check firmware version again */
2049         err = determine_fw_capabilities(priv, wiphy->fw_version,
2050                         sizeof(wiphy->fw_version),
2051                         &wiphy->hw_version);
2052         if (err != 0) {
2053             dev_err(dev, "Incompatible firmware, aborting\n");
2054             goto out;
2055         }
2056     }
2057 
2058     if (priv->has_port3)
2059         dev_info(dev, "Ad-hoc demo mode supported\n");
2060     if (priv->has_ibss)
2061         dev_info(dev, "IEEE standard IBSS ad-hoc mode supported\n");
2062     if (priv->has_wep)
2063         dev_info(dev, "WEP supported, %s-bit key\n",
2064              priv->has_big_wep ? "104" : "40");
2065     if (priv->has_wpa) {
2066         dev_info(dev, "WPA-PSK supported\n");
2067         if (orinoco_mic_init(priv)) {
2068             dev_err(dev, "Failed to setup MIC crypto algorithm. "
2069                 "Disabling WPA support\n");
2070             priv->has_wpa = 0;
2071         }
2072     }
2073 
2074     err = orinoco_hw_read_card_settings(priv, wiphy->perm_addr);
2075     if (err)
2076         goto out;
2077 
2078     err = orinoco_hw_allocate_fid(priv);
2079     if (err) {
2080         dev_err(dev, "Failed to allocate NIC buffer!\n");
2081         goto out;
2082     }
2083 
2084     /* Set up the default configuration */
2085     priv->iw_mode = NL80211_IFTYPE_STATION;
2086     /* By default use IEEE/IBSS ad-hoc mode if we have it */
2087     priv->prefer_port3 = priv->has_port3 && (!priv->has_ibss);
2088     set_port_type(priv);
2089     priv->channel = 0; /* use firmware default */
2090 
2091     priv->promiscuous = 0;
2092     priv->encode_alg = ORINOCO_ALG_NONE;
2093     priv->tx_key = 0;
2094     priv->wpa_enabled = 0;
2095     priv->tkip_cm_active = 0;
2096     priv->key_mgmt = 0;
2097     priv->wpa_ie_len = 0;
2098     priv->wpa_ie = NULL;
2099 
2100     if (orinoco_wiphy_register(wiphy)) {
2101         err = -ENODEV;
2102         goto out;
2103     }
2104 
2105     /* Make the hardware available, as long as it hasn't been
2106      * removed elsewhere (e.g. by PCMCIA hot unplug) */
2107     orinoco_lock_irq(priv);
2108     priv->hw_unavailable--;
2109     orinoco_unlock_irq(priv);
2110 
2111     dev_dbg(dev, "Ready\n");
2112 
2113  out:
2114     return err;
2115 }
2116 EXPORT_SYMBOL(orinoco_init);
2117 
2118 static const struct net_device_ops orinoco_netdev_ops = {
2119     .ndo_open       = orinoco_open,
2120     .ndo_stop       = orinoco_stop,
2121     .ndo_start_xmit     = orinoco_xmit,
2122     .ndo_set_rx_mode    = orinoco_set_multicast_list,
2123     .ndo_change_mtu     = orinoco_change_mtu,
2124     .ndo_set_mac_address    = eth_mac_addr,
2125     .ndo_validate_addr  = eth_validate_addr,
2126     .ndo_tx_timeout     = orinoco_tx_timeout,
2127 };
2128 
2129 /* Allocate private data.
2130  *
2131  * This driver has a number of structures associated with it
2132  *  netdev - Net device structure for each network interface
2133  *  wiphy - structure associated with wireless phy
2134  *  wireless_dev (wdev) - structure for each wireless interface
2135  *  hw - structure for hermes chip info
2136  *  card - card specific structure for use by the card driver
2137  *         (airport, orinoco_cs)
2138  *  priv - orinoco private data
2139  *  device - generic linux device structure
2140  *
2141  *  +---------+    +---------+
2142  *  |  wiphy  |    | netdev  |
2143  *  | +-------+    | +-------+
2144  *  | | priv  |    | | wdev  |
2145  *  | | +-----+    +-+-------+
2146  *  | | | hw  |
2147  *  | +-+-----+
2148  *  | | card  |
2149  *  +-+-------+
2150  *
2151  * priv has a link to netdev and device
2152  * wdev has a link to wiphy
2153  */
2154 struct orinoco_private
2155 *alloc_orinocodev(int sizeof_card,
2156           struct device *device,
2157           int (*hard_reset)(struct orinoco_private *),
2158           int (*stop_fw)(struct orinoco_private *, int))
2159 {
2160     struct orinoco_private *priv;
2161     struct wiphy *wiphy;
2162 
2163     /* allocate wiphy
2164      * NOTE: We only support a single virtual interface
2165      *       but this may change when monitor mode is added
2166      */
2167     wiphy = wiphy_new(&orinoco_cfg_ops,
2168               sizeof(struct orinoco_private) + sizeof_card);
2169     if (!wiphy)
2170         return NULL;
2171 
2172     priv = wiphy_priv(wiphy);
2173     priv->dev = device;
2174 
2175     if (sizeof_card)
2176         priv->card = (void *)((unsigned long)priv
2177                       + sizeof(struct orinoco_private));
2178     else
2179         priv->card = NULL;
2180 
2181     orinoco_wiphy_init(wiphy);
2182 
2183 #ifdef WIRELESS_SPY
2184     priv->wireless_data.spy_data = &priv->spy_data;
2185 #endif
2186 
2187     /* Set up default callbacks */
2188     priv->hard_reset = hard_reset;
2189     priv->stop_fw = stop_fw;
2190 
2191     spin_lock_init(&priv->lock);
2192     priv->open = 0;
2193     priv->hw_unavailable = 1; /* orinoco_init() must clear this
2194                    * before anything else touches the
2195                    * hardware */
2196     INIT_WORK(&priv->reset_work, orinoco_reset);
2197     INIT_WORK(&priv->join_work, orinoco_join_ap);
2198     INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
2199 
2200     INIT_LIST_HEAD(&priv->rx_list);
2201     tasklet_setup(&priv->rx_tasklet, orinoco_rx_isr_tasklet);
2202 
2203     spin_lock_init(&priv->scan_lock);
2204     INIT_LIST_HEAD(&priv->scan_list);
2205     INIT_WORK(&priv->process_scan, orinoco_process_scan_results);
2206 
2207     priv->last_linkstatus = 0xffff;
2208 
2209 #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
2210     priv->cached_pri_fw = NULL;
2211     priv->cached_fw = NULL;
2212 #endif
2213 
2214     /* Register PM notifiers */
2215     orinoco_register_pm_notifier(priv);
2216 
2217     return priv;
2218 }
2219 EXPORT_SYMBOL(alloc_orinocodev);
2220 
2221 /* We can only support a single interface. We provide a separate
2222  * function to set it up to distinguish between hardware
2223  * initialisation and interface setup.
2224  *
2225  * The base_addr and irq parameters are passed on to netdev for use
2226  * with SIOCGIFMAP.
2227  */
2228 int orinoco_if_add(struct orinoco_private *priv,
2229            unsigned long base_addr,
2230            unsigned int irq,
2231            const struct net_device_ops *ops)
2232 {
2233     struct wiphy *wiphy = priv_to_wiphy(priv);
2234     struct wireless_dev *wdev;
2235     struct net_device *dev;
2236     int ret;
2237 
2238     dev = alloc_etherdev(sizeof(struct wireless_dev));
2239 
2240     if (!dev)
2241         return -ENOMEM;
2242 
2243     /* Initialise wireless_dev */
2244     wdev = netdev_priv(dev);
2245     wdev->wiphy = wiphy;
2246     wdev->iftype = NL80211_IFTYPE_STATION;
2247 
2248     /* Setup / override net_device fields */
2249     dev->ieee80211_ptr = wdev;
2250     dev->watchdog_timeo = HZ; /* 1 second timeout */
2251     dev->wireless_handlers = &orinoco_handler_def;
2252 #ifdef WIRELESS_SPY
2253     dev->wireless_data = &priv->wireless_data;
2254 #endif
2255     /* Default to standard ops if not set */
2256     if (ops)
2257         dev->netdev_ops = ops;
2258     else
2259         dev->netdev_ops = &orinoco_netdev_ops;
2260 
2261     /* we use the default eth_mac_addr for setting the MAC addr */
2262 
2263     /* Reserve space in skb for the SNAP header */
2264     dev->needed_headroom = ENCAPS_OVERHEAD;
2265 
2266     netif_carrier_off(dev);
2267 
2268     eth_hw_addr_set(dev, wiphy->perm_addr);
2269 
2270     dev->base_addr = base_addr;
2271     dev->irq = irq;
2272 
2273     dev->min_mtu = ORINOCO_MIN_MTU;
2274     dev->max_mtu = ORINOCO_MAX_MTU;
2275 
2276     SET_NETDEV_DEV(dev, priv->dev);
2277     ret = register_netdev(dev);
2278     if (ret)
2279         goto fail;
2280 
2281     priv->ndev = dev;
2282 
2283     /* Report what we've done */
2284     dev_dbg(priv->dev, "Registered interface %s.\n", dev->name);
2285 
2286     return 0;
2287 
2288  fail:
2289     free_netdev(dev);
2290     return ret;
2291 }
2292 EXPORT_SYMBOL(orinoco_if_add);
2293 
2294 void orinoco_if_del(struct orinoco_private *priv)
2295 {
2296     struct net_device *dev = priv->ndev;
2297 
2298     unregister_netdev(dev);
2299     free_netdev(dev);
2300 }
2301 EXPORT_SYMBOL(orinoco_if_del);
2302 
2303 void free_orinocodev(struct orinoco_private *priv)
2304 {
2305     struct wiphy *wiphy = priv_to_wiphy(priv);
2306     struct orinoco_rx_data *rx_data, *temp;
2307     struct orinoco_scan_data *sd, *sdtemp;
2308 
2309     /* If the tasklet is scheduled when we call tasklet_kill it
2310      * will run one final time. However the tasklet will only
2311      * drain priv->rx_list if the hw is still available. */
2312     tasklet_kill(&priv->rx_tasklet);
2313 
2314     /* Explicitly drain priv->rx_list */
2315     list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
2316         list_del(&rx_data->list);
2317 
2318         dev_kfree_skb(rx_data->skb);
2319         kfree(rx_data->desc);
2320         kfree(rx_data);
2321     }
2322 
2323     cancel_work_sync(&priv->process_scan);
2324     /* Explicitly drain priv->scan_list */
2325     list_for_each_entry_safe(sd, sdtemp, &priv->scan_list, list) {
2326         list_del(&sd->list);
2327 
2328         if (sd->len > 0)
2329             kfree(sd->buf);
2330         kfree(sd);
2331     }
2332 
2333     orinoco_unregister_pm_notifier(priv);
2334     orinoco_uncache_fw(priv);
2335 
2336     priv->wpa_ie_len = 0;
2337     kfree(priv->wpa_ie);
2338     orinoco_mic_free(priv);
2339     wiphy_free(wiphy);
2340 }
2341 EXPORT_SYMBOL(free_orinocodev);
2342 
2343 int orinoco_up(struct orinoco_private *priv)
2344 {
2345     struct net_device *dev = priv->ndev;
2346     unsigned long flags;
2347     int err;
2348 
2349     priv->hw.ops->lock_irqsave(&priv->lock, &flags);
2350 
2351     err = orinoco_reinit_firmware(priv);
2352     if (err) {
2353         printk(KERN_ERR "%s: Error %d re-initializing firmware\n",
2354                dev->name, err);
2355         goto exit;
2356     }
2357 
2358     netif_device_attach(dev);
2359     priv->hw_unavailable--;
2360 
2361     if (priv->open && !priv->hw_unavailable) {
2362         err = __orinoco_up(priv);
2363         if (err)
2364             printk(KERN_ERR "%s: Error %d restarting card\n",
2365                    dev->name, err);
2366     }
2367 
2368 exit:
2369     priv->hw.ops->unlock_irqrestore(&priv->lock, &flags);
2370 
2371     return 0;
2372 }
2373 EXPORT_SYMBOL(orinoco_up);
2374 
2375 void orinoco_down(struct orinoco_private *priv)
2376 {
2377     struct net_device *dev = priv->ndev;
2378     unsigned long flags;
2379     int err;
2380 
2381     priv->hw.ops->lock_irqsave(&priv->lock, &flags);
2382     err = __orinoco_down(priv);
2383     if (err)
2384         printk(KERN_WARNING "%s: Error %d downing interface\n",
2385                dev->name, err);
2386 
2387     netif_device_detach(dev);
2388     priv->hw_unavailable++;
2389     priv->hw.ops->unlock_irqrestore(&priv->lock, &flags);
2390 }
2391 EXPORT_SYMBOL(orinoco_down);
2392 
2393 /********************************************************************/
2394 /* Module initialization                                            */
2395 /********************************************************************/
2396 
2397 /* Can't be declared "const" or the whole __initdata section will
2398  * become const */
2399 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
2400     " (David Gibson <hermes@gibson.dropbear.id.au>, "
2401     "Pavel Roskin <proski@gnu.org>, et al)";
2402 
2403 static int __init init_orinoco(void)
2404 {
2405     printk(KERN_DEBUG "%s\n", version);
2406     return 0;
2407 }
2408 
2409 static void __exit exit_orinoco(void)
2410 {
2411 }
2412 
2413 module_init(init_orinoco);
2414 module_exit(exit_orinoco);