1 /* main.c - (formerly known as dldwd_cs.c, orinoco_cs.c and orinoco.c)
3 * A driver for Hermes or Prism 2 chipset based PCMCIA wireless
4 * adaptors, with Lucent/Agere, Intersil or Symbol firmware.
6 * Current maintainers (as of 29 September 2003) are:
7 * Pavel Roskin <proski AT gnu.org>
8 * and David Gibson <hermes AT gibson.dropbear.id.au>
10 * (C) Copyright David Gibson, IBM Corporation 2001-2003.
11 * Copyright (C) 2000 David Gibson, Linuxcare Australia.
12 * With some help from :
13 * Copyright (C) 2001 Jean Tourrilhes, HP Labs
14 * Copyright (C) 2001 Benjamin Herrenschmidt
16 * Based on dummy_cs.c 1.27 2000/06/12 21:27:25
18 * Portions based on wvlan_cs.c 1.0.6, Copyright Andreas Neuhaus <andy
19 * AT fasta.fh-dortmund.de>
20 * http://www.stud.fh-dortmund.de/~andy/wvlan/
22 * The contents of this file are subject to the Mozilla Public License
23 * Version 1.1 (the "License"); you may not use this file except in
24 * compliance with the License. You may obtain a copy of the License
25 * at http://www.mozilla.org/MPL/
27 * Software distributed under the License is distributed on an "AS IS"
28 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
29 * the License for the specific language governing rights and
30 * limitations under the License.
32 * The initial developer of the original code is David A. Hinds
33 * <dahinds AT users.sourceforge.net>. Portions created by David
34 * A. Hinds are Copyright (C) 1999 David A. Hinds. All Rights
37 * Alternatively, the contents of this file may be used under the
38 * terms of the GNU General Public License version 2 (the "GPL"), in
39 * which case the provisions of the GPL are applicable instead of the
40 * above. If you wish to allow the use of your version of this file
41 * only under the terms of the GPL and not to allow others to use your
42 * version of this file under the MPL, indicate your decision by
43 * deleting the provisions above and replace them with the notice and
44 * other provisions required by the GPL. If you do not delete the
45 * provisions above, a recipient may use your version of this file
46 * under either the MPL or the GPL. */
50 * o Handle de-encapsulation within network layer, provide 802.11
51 * headers (patch from Thomas 'Dent' Mirlacher)
52 * o Fix possible races in SPY handling.
53 * o Disconnect wireless extensions from fundamental configuration.
54 * o (maybe) Software WEP support (patch from Stano Meduna).
55 * o (maybe) Use multiple Tx buffers - driver handling queue
56 * rather than firmware.
59 /* Locking and synchronization:
61 * The basic principle is that everything is serialized through a
62 * single spinlock, priv->lock. The lock is used in user, bh and irq
63 * context, so when taken outside hardirq context it should always be
64 * taken with interrupts disabled. The lock protects both the
65 * hardware and the struct orinoco_private.
67 * Another flag, priv->hw_unavailable indicates that the hardware is
68 * unavailable for an extended period of time (e.g. suspended, or in
69 * the middle of a hard reset). This flag is protected by the
70 * spinlock. All code which touches the hardware should check the
71 * flag after taking the lock, and if it is set, give up on whatever
72 * they are doing and drop the lock again. The orinoco_lock()
73 * function handles this (it unlocks and returns -EBUSY if
74 * hw_unavailable is non-zero).
77 #define DRIVER_NAME "orinoco"
79 #include <linux/module.h>
80 #include <linux/kernel.h>
81 #include <linux/init.h>
82 #include <linux/delay.h>
83 #include <linux/device.h>
84 #include <linux/netdevice.h>
85 #include <linux/etherdevice.h>
86 #include <linux/suspend.h>
87 #include <linux/if_arp.h>
88 #include <linux/wireless.h>
89 #include <linux/ieee80211.h>
90 #include <net/iw_handler.h>
91 #include <net/cfg80211.h>
93 #include "hermes_rid.h"
94 #include "hermes_dld.h"
105 /********************************************************************/
106 /* Module information */
107 /********************************************************************/
109 MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & "
110 "David Gibson <hermes@gibson.dropbear.id.au>");
111 MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based "
112 "and similar wireless cards");
113 MODULE_LICENSE("Dual MPL/GPL");
115 /* Level of debugging. Used in the macros in orinoco.h */
117 int orinoco_debug = ORINOCO_DEBUG;
118 EXPORT_SYMBOL(orinoco_debug);
119 module_param(orinoco_debug, int, 0644);
120 MODULE_PARM_DESC(orinoco_debug, "Debug level");
123 static int suppress_linkstatus; /* = 0 */
124 module_param(suppress_linkstatus, bool, 0644);
125 MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
127 static int ignore_disconnect; /* = 0 */
128 module_param(ignore_disconnect, int, 0644);
129 MODULE_PARM_DESC(ignore_disconnect,
130 "Don't report lost link to the network layer");
132 int force_monitor; /* = 0 */
133 module_param(force_monitor, int, 0644);
134 MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
136 /********************************************************************/
137 /* Internal constants */
138 /********************************************************************/
140 /* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
141 static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
142 #define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
144 #define ORINOCO_MIN_MTU 256
145 #define ORINOCO_MAX_MTU (IEEE80211_MAX_DATA_LEN - ENCAPS_OVERHEAD)
147 #define MAX_IRQLOOPS_PER_IRQ 10
148 #define MAX_IRQLOOPS_PER_JIFFY (20000/HZ) /* Based on a guestimate of
149 * how many events the
151 * legitimately generate */
153 #define DUMMY_FID 0xFFFF
155 /*#define MAX_MULTICAST(priv) (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
156 HERMES_MAX_MULTICAST : 0)*/
157 #define MAX_MULTICAST(priv) (HERMES_MAX_MULTICAST)
159 #define ORINOCO_INTEN (HERMES_EV_RX | HERMES_EV_ALLOC \
160 | HERMES_EV_TX | HERMES_EV_TXEXC \
161 | HERMES_EV_WTERR | HERMES_EV_INFO \
164 /********************************************************************/
166 /********************************************************************/
168 /* Beginning of the Tx descriptor, used in TxExc handling */
169 struct hermes_txexc_data {
170 struct hermes_tx_descriptor desc;
174 } __attribute__ ((packed));
176 /* Rx frame header except compatibility 802.3 header */
177 struct hermes_rx_descriptor {
198 } __attribute__ ((packed));
200 struct orinoco_rx_data {
201 struct hermes_rx_descriptor *desc;
203 struct list_head list;
206 struct orinoco_scan_data {
210 struct list_head list;
213 /********************************************************************/
214 /* Function prototypes */
215 /********************************************************************/
217 static int __orinoco_set_multicast_list(struct net_device *dev);
218 static int __orinoco_up(struct orinoco_private *priv);
219 static int __orinoco_down(struct orinoco_private *priv);
220 static int __orinoco_commit(struct orinoco_private *priv);
222 /********************************************************************/
223 /* Internal helper functions */
224 /********************************************************************/
226 void set_port_type(struct orinoco_private *priv)
228 switch (priv->iw_mode) {
229 case NL80211_IFTYPE_STATION:
231 priv->createibss = 0;
233 case NL80211_IFTYPE_ADHOC:
234 if (priv->prefer_port3) {
236 priv->createibss = 0;
238 priv->port_type = priv->ibss_port;
239 priv->createibss = 1;
242 case NL80211_IFTYPE_MONITOR:
244 priv->createibss = 0;
247 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
252 /********************************************************************/
254 /********************************************************************/
256 static int orinoco_open(struct net_device *dev)
258 struct orinoco_private *priv = ndev_priv(dev);
262 if (orinoco_lock(priv, &flags) != 0)
265 err = __orinoco_up(priv);
270 orinoco_unlock(priv, &flags);
275 static int orinoco_stop(struct net_device *dev)
277 struct orinoco_private *priv = ndev_priv(dev);
280 /* We mustn't use orinoco_lock() here, because we need to be
281 able to close the interface even if hw_unavailable is set
282 (e.g. as we're released after a PC Card removal) */
283 spin_lock_irq(&priv->lock);
287 err = __orinoco_down(priv);
289 spin_unlock_irq(&priv->lock);
294 static struct net_device_stats *orinoco_get_stats(struct net_device *dev)
296 struct orinoco_private *priv = ndev_priv(dev);
301 static void orinoco_set_multicast_list(struct net_device *dev)
303 struct orinoco_private *priv = ndev_priv(dev);
306 if (orinoco_lock(priv, &flags) != 0) {
307 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
308 "called when hw_unavailable\n", dev->name);
312 __orinoco_set_multicast_list(dev);
313 orinoco_unlock(priv, &flags);
316 static int orinoco_change_mtu(struct net_device *dev, int new_mtu)
318 struct orinoco_private *priv = ndev_priv(dev);
320 if ((new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU))
323 /* MTU + encapsulation + header length */
324 if ((new_mtu + ENCAPS_OVERHEAD + sizeof(struct ieee80211_hdr)) >
325 (priv->nicbuf_size - ETH_HLEN))
333 /********************************************************************/
335 /********************************************************************/
337 static netdev_tx_t orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
339 struct orinoco_private *priv = ndev_priv(dev);
340 struct net_device_stats *stats = &priv->stats;
341 struct orinoco_tkip_key *key;
342 hermes_t *hw = &priv->hw;
344 u16 txfid = priv->txfid;
350 if (!netif_running(dev)) {
351 printk(KERN_ERR "%s: Tx on stopped device!\n",
353 return NETDEV_TX_BUSY;
356 if (netif_queue_stopped(dev)) {
357 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
359 return NETDEV_TX_BUSY;
362 if (orinoco_lock(priv, &flags) != 0) {
363 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
365 return NETDEV_TX_BUSY;
368 if (!netif_carrier_ok(dev) ||
369 (priv->iw_mode == NL80211_IFTYPE_MONITOR)) {
370 /* Oops, the firmware hasn't established a connection,
371 silently drop the packet (this seems to be the
376 /* Check packet length */
377 if (skb->len < ETH_HLEN)
380 key = (struct orinoco_tkip_key *) priv->keys[priv->tx_key].key;
382 do_mic = ((priv->encode_alg == ORINOCO_ALG_TKIP) &&
385 tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
388 tx_control |= (priv->tx_key << HERMES_MIC_KEY_ID_SHIFT) |
391 if (priv->has_alt_txcntl) {
392 /* WPA enabled firmwares have tx_cntl at the end of
393 * the 802.11 header. So write zeroed descriptor and
394 * 802.11 header at the same time
396 char desc[HERMES_802_3_OFFSET];
397 __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
399 memset(&desc, 0, sizeof(desc));
401 *txcntl = cpu_to_le16(tx_control);
402 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
406 printk(KERN_ERR "%s: Error %d writing Tx "
407 "descriptor to BAP\n", dev->name, err);
411 struct hermes_tx_descriptor desc;
413 memset(&desc, 0, sizeof(desc));
415 desc.tx_control = cpu_to_le16(tx_control);
416 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
420 printk(KERN_ERR "%s: Error %d writing Tx "
421 "descriptor to BAP\n", dev->name, err);
425 /* Clear the 802.11 header and data length fields - some
426 * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
427 * if this isn't done. */
428 hermes_clear_words(hw, HERMES_DATA0,
429 HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
432 eh = (struct ethhdr *)skb->data;
434 /* Encapsulate Ethernet-II frames */
435 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
436 struct header_struct {
437 struct ethhdr eth; /* 802.3 header */
438 u8 encap[6]; /* 802.2 header */
439 } __attribute__ ((packed)) hdr;
441 /* Strip destination and source from the data */
442 skb_pull(skb, 2 * ETH_ALEN);
444 /* And move them to a separate header */
445 memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
446 hdr.eth.h_proto = htons(sizeof(encaps_hdr) + skb->len);
447 memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
449 /* Insert the SNAP header */
450 if (skb_headroom(skb) < sizeof(hdr)) {
452 "%s: Not enough headroom for 802.2 headers %d\n",
453 dev->name, skb_headroom(skb));
456 eh = (struct ethhdr *) skb_push(skb, sizeof(hdr));
457 memcpy(eh, &hdr, sizeof(hdr));
460 err = hermes_bap_pwrite(hw, USER_BAP, skb->data, skb->len,
461 txfid, HERMES_802_3_OFFSET);
463 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
468 /* Calculate Michael MIC */
470 u8 mic_buf[MICHAEL_MIC_LEN + 1];
476 /* MIC start is on an odd boundary */
477 mic_buf[0] = skb->data[skb->len - 1];
479 offset = skb->len - 1;
480 len = MICHAEL_MIC_LEN + 1;
484 len = MICHAEL_MIC_LEN;
487 orinoco_mic(priv->tx_tfm_mic, key->tx_mic,
488 eh->h_dest, eh->h_source, 0 /* priority */,
489 skb->data + ETH_HLEN, skb->len - ETH_HLEN, mic);
492 err = hermes_bap_pwrite(hw, USER_BAP, &mic_buf[0], len,
493 txfid, HERMES_802_3_OFFSET + offset);
495 printk(KERN_ERR "%s: Error %d writing MIC to BAP\n",
501 /* Finally, we actually initiate the send */
502 netif_stop_queue(dev);
504 err = hermes_docmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
507 netif_start_queue(dev);
509 printk(KERN_ERR "%s: Error %d transmitting packet\n",
514 dev->trans_start = jiffies;
515 stats->tx_bytes += HERMES_802_3_OFFSET + skb->len;
523 orinoco_unlock(priv, &flags);
529 schedule_work(&priv->reset_work);
530 orinoco_unlock(priv, &flags);
531 return NETDEV_TX_BUSY;
534 static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
536 struct orinoco_private *priv = ndev_priv(dev);
537 u16 fid = hermes_read_regn(hw, ALLOCFID);
539 if (fid != priv->txfid) {
540 if (fid != DUMMY_FID)
541 printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
546 hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
549 static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
551 struct orinoco_private *priv = ndev_priv(dev);
552 struct net_device_stats *stats = &priv->stats;
556 netif_wake_queue(dev);
558 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
561 static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
563 struct orinoco_private *priv = ndev_priv(dev);
564 struct net_device_stats *stats = &priv->stats;
565 u16 fid = hermes_read_regn(hw, TXCOMPLFID);
567 struct hermes_txexc_data hdr;
570 if (fid == DUMMY_FID)
571 return; /* Nothing's really happened */
573 /* Read part of the frame header - we need status and addr1 */
574 err = hermes_bap_pread(hw, IRQ_BAP, &hdr,
575 sizeof(struct hermes_txexc_data),
578 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
582 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
583 "(FID=%04X error %d)\n",
584 dev->name, fid, err);
588 DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
591 /* We produce a TXDROP event only for retry or lifetime
592 * exceeded, because that's the only status that really mean
593 * that this particular node went away.
594 * Other errors means that *we* screwed up. - Jean II */
595 status = le16_to_cpu(hdr.desc.status);
596 if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
597 union iwreq_data wrqu;
599 /* Copy 802.11 dest address.
600 * We use the 802.11 header because the frame may
601 * not be 802.3 or may be mangled...
602 * In Ad-Hoc mode, it will be the node address.
603 * In managed mode, it will be most likely the AP addr
604 * User space will figure out how to convert it to
605 * whatever it needs (IP address or else).
607 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
608 wrqu.addr.sa_family = ARPHRD_ETHER;
610 /* Send event to user space */
611 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
614 netif_wake_queue(dev);
617 static void orinoco_tx_timeout(struct net_device *dev)
619 struct orinoco_private *priv = ndev_priv(dev);
620 struct net_device_stats *stats = &priv->stats;
621 struct hermes *hw = &priv->hw;
623 printk(KERN_WARNING "%s: Tx timeout! "
624 "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
625 dev->name, hermes_read_regn(hw, ALLOCFID),
626 hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
630 schedule_work(&priv->reset_work);
633 /********************************************************************/
634 /* Rx path (data frames) */
635 /********************************************************************/
637 /* Does the frame have a SNAP header indicating it should be
638 * de-encapsulated to Ethernet-II? */
639 static inline int is_ethersnap(void *_hdr)
643 /* We de-encapsulate all packets which, a) have SNAP headers
644 * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
645 * and where b) the OUI of the SNAP header is 00:00:00 or
646 * 00:00:f8 - we need both because different APs appear to use
647 * different OUIs for some reason */
648 return (memcmp(hdr, &encaps_hdr, 5) == 0)
649 && ((hdr[5] == 0x00) || (hdr[5] == 0xf8));
652 static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
653 int level, int noise)
655 struct iw_quality wstats;
656 wstats.level = level - 0x95;
657 wstats.noise = noise - 0x95;
658 wstats.qual = (level > noise) ? (level - noise) : 0;
659 wstats.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
660 /* Update spy records */
661 wireless_spy_update(dev, mac, &wstats);
664 static void orinoco_stat_gather(struct net_device *dev,
666 struct hermes_rx_descriptor *desc)
668 struct orinoco_private *priv = ndev_priv(dev);
670 /* Using spy support with lots of Rx packets, like in an
671 * infrastructure (AP), will really slow down everything, because
672 * the MAC address must be compared to each entry of the spy list.
673 * If the user really asks for it (set some address in the
674 * spy list), we do it, but he will pay the price.
675 * Note that to get here, you need both WIRELESS_SPY
676 * compiled in AND some addresses in the list !!!
678 /* Note : gcc will optimise the whole section away if
679 * WIRELESS_SPY is not defined... - Jean II */
680 if (SPY_NUMBER(priv)) {
681 orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
682 desc->signal, desc->silence);
687 * orinoco_rx_monitor - handle received monitor frames.
692 * desc rx descriptor of the frame
694 * Call context: interrupt
696 static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
697 struct hermes_rx_descriptor *desc)
699 u32 hdrlen = 30; /* return full header by default */
705 struct orinoco_private *priv = ndev_priv(dev);
706 struct net_device_stats *stats = &priv->stats;
707 hermes_t *hw = &priv->hw;
709 len = le16_to_cpu(desc->data_len);
711 /* Determine the size of the header and the data */
712 fc = le16_to_cpu(desc->frame_ctl);
713 switch (fc & IEEE80211_FCTL_FTYPE) {
714 case IEEE80211_FTYPE_DATA:
715 if ((fc & IEEE80211_FCTL_TODS)
716 && (fc & IEEE80211_FCTL_FROMDS))
722 case IEEE80211_FTYPE_MGMT:
726 case IEEE80211_FTYPE_CTL:
727 switch (fc & IEEE80211_FCTL_STYPE) {
728 case IEEE80211_STYPE_PSPOLL:
729 case IEEE80211_STYPE_RTS:
730 case IEEE80211_STYPE_CFEND:
731 case IEEE80211_STYPE_CFENDACK:
734 case IEEE80211_STYPE_CTS:
735 case IEEE80211_STYPE_ACK:
741 /* Unknown frame type */
745 /* sanity check the length */
746 if (datalen > IEEE80211_MAX_DATA_LEN + 12) {
747 printk(KERN_DEBUG "%s: oversized monitor frame, "
748 "data length = %d\n", dev->name, datalen);
749 stats->rx_length_errors++;
753 skb = dev_alloc_skb(hdrlen + datalen);
755 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
760 /* Copy the 802.11 header to the skb */
761 memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
762 skb_reset_mac_header(skb);
764 /* If any, copy the data from the card to the skb */
766 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
767 ALIGN(datalen, 2), rxfid,
768 HERMES_802_2_OFFSET);
770 printk(KERN_ERR "%s: error %d reading monitor frame\n",
777 skb->ip_summed = CHECKSUM_NONE;
778 skb->pkt_type = PACKET_OTHERHOST;
779 skb->protocol = cpu_to_be16(ETH_P_802_2);
782 stats->rx_bytes += skb->len;
788 dev_kfree_skb_irq(skb);
794 static void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
796 struct orinoco_private *priv = ndev_priv(dev);
797 struct net_device_stats *stats = &priv->stats;
798 struct iw_statistics *wstats = &priv->wstats;
799 struct sk_buff *skb = NULL;
802 struct hermes_rx_descriptor *desc;
803 struct orinoco_rx_data *rx_data;
806 desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
809 "%s: Can't allocate space for RX descriptor\n",
814 rxfid = hermes_read_regn(hw, RXFID);
816 err = hermes_bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
819 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
820 "Frame dropped.\n", dev->name, err);
824 status = le16_to_cpu(desc->status);
826 if (status & HERMES_RXSTAT_BADCRC) {
827 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
829 stats->rx_crc_errors++;
833 /* Handle frames in monitor mode */
834 if (priv->iw_mode == NL80211_IFTYPE_MONITOR) {
835 orinoco_rx_monitor(dev, rxfid, desc);
839 if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
840 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
842 wstats->discard.code++;
846 length = le16_to_cpu(desc->data_len);
849 if (length < 3) { /* No for even an 802.2 LLC header */
850 /* At least on Symbol firmware with PCF we get quite a
851 lot of these legitimately - Poll frames with no
855 if (length > IEEE80211_MAX_DATA_LEN) {
856 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
858 stats->rx_length_errors++;
862 /* Payload size does not include Michael MIC. Increase payload
863 * size to read it together with the data. */
864 if (status & HERMES_RXSTAT_MIC)
865 length += MICHAEL_MIC_LEN;
867 /* We need space for the packet data itself, plus an ethernet
868 header, plus 2 bytes so we can align the IP header on a
869 32bit boundary, plus 1 byte so we can read in odd length
870 packets from the card, which has an IO granularity of 16
872 skb = dev_alloc_skb(length+ETH_HLEN+2+1);
874 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
879 /* We'll prepend the header, so reserve space for it. The worst
880 case is no decapsulation, when 802.3 header is prepended and
881 nothing is removed. 2 is for aligning the IP header. */
882 skb_reserve(skb, ETH_HLEN + 2);
884 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, length),
885 ALIGN(length, 2), rxfid,
886 HERMES_802_2_OFFSET);
888 printk(KERN_ERR "%s: error %d reading frame. "
889 "Frame dropped.\n", dev->name, err);
893 /* Add desc and skb to rx queue */
894 rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
896 printk(KERN_WARNING "%s: Can't allocate RX packet\n",
900 rx_data->desc = desc;
902 list_add_tail(&rx_data->list, &priv->rx_list);
903 tasklet_schedule(&priv->rx_tasklet);
908 dev_kfree_skb_irq(skb);
916 static void orinoco_rx(struct net_device *dev,
917 struct hermes_rx_descriptor *desc,
920 struct orinoco_private *priv = ndev_priv(dev);
921 struct net_device_stats *stats = &priv->stats;
926 status = le16_to_cpu(desc->status);
927 length = le16_to_cpu(desc->data_len);
928 fc = le16_to_cpu(desc->frame_ctl);
930 /* Calculate and check MIC */
931 if (status & HERMES_RXSTAT_MIC) {
932 struct orinoco_tkip_key *key;
933 int key_id = ((status & HERMES_RXSTAT_MIC_KEY_ID) >>
934 HERMES_MIC_KEY_ID_SHIFT);
935 u8 mic[MICHAEL_MIC_LEN];
937 u8 *src = (fc & IEEE80211_FCTL_FROMDS) ?
938 desc->addr3 : desc->addr2;
940 /* Extract Michael MIC from payload */
941 rxmic = skb->data + skb->len - MICHAEL_MIC_LEN;
943 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
944 length -= MICHAEL_MIC_LEN;
946 key = (struct orinoco_tkip_key *) priv->keys[key_id].key;
949 printk(KERN_WARNING "%s: Received encrypted frame from "
950 "%pM using key %i, but key is not installed\n",
951 dev->name, src, key_id);
955 orinoco_mic(priv->rx_tfm_mic, key->rx_mic, desc->addr1, src,
956 0, /* priority or QoS? */
957 skb->data, skb->len, &mic[0]);
959 if (memcmp(mic, rxmic,
961 union iwreq_data wrqu;
962 struct iw_michaelmicfailure wxmic;
964 printk(KERN_WARNING "%s: "
965 "Invalid Michael MIC in data frame from %pM, "
967 dev->name, src, key_id);
969 /* TODO: update stats */
971 /* Notify userspace */
972 memset(&wxmic, 0, sizeof(wxmic));
973 wxmic.flags = key_id & IW_MICFAILURE_KEY_ID;
974 wxmic.flags |= (desc->addr1[0] & 1) ?
975 IW_MICFAILURE_GROUP : IW_MICFAILURE_PAIRWISE;
976 wxmic.src_addr.sa_family = ARPHRD_ETHER;
977 memcpy(wxmic.src_addr.sa_data, src, ETH_ALEN);
979 (void) orinoco_hw_get_tkip_iv(priv, key_id,
982 memset(&wrqu, 0, sizeof(wrqu));
983 wrqu.data.length = sizeof(wxmic);
984 wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu,
991 /* Handle decapsulation
992 * In most cases, the firmware tell us about SNAP frames.
993 * For some reason, the SNAP frames sent by LinkSys APs
994 * are not properly recognised by most firmwares.
995 * So, check ourselves */
996 if (length >= ENCAPS_OVERHEAD &&
997 (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
998 ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
999 is_ethersnap(skb->data))) {
1000 /* These indicate a SNAP within 802.2 LLC within
1001 802.11 frame which we'll need to de-encapsulate to
1002 the original EthernetII frame. */
1003 hdr = (struct ethhdr *)skb_push(skb,
1004 ETH_HLEN - ENCAPS_OVERHEAD);
1006 /* 802.3 frame - prepend 802.3 header as is */
1007 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
1008 hdr->h_proto = htons(length);
1010 memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
1011 if (fc & IEEE80211_FCTL_FROMDS)
1012 memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
1014 memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
1016 skb->protocol = eth_type_trans(skb, dev);
1017 skb->ip_summed = CHECKSUM_NONE;
1018 if (fc & IEEE80211_FCTL_TODS)
1019 skb->pkt_type = PACKET_OTHERHOST;
1021 /* Process the wireless stats if needed */
1022 orinoco_stat_gather(dev, skb, desc);
1024 /* Pass the packet to the networking stack */
1026 stats->rx_packets++;
1027 stats->rx_bytes += length;
1034 stats->rx_dropped++;
1037 static void orinoco_rx_isr_tasklet(unsigned long data)
1039 struct orinoco_private *priv = (struct orinoco_private *) data;
1040 struct net_device *dev = priv->ndev;
1041 struct orinoco_rx_data *rx_data, *temp;
1042 struct hermes_rx_descriptor *desc;
1043 struct sk_buff *skb;
1044 unsigned long flags;
1046 /* orinoco_rx requires the driver lock, and we also need to
1047 * protect priv->rx_list, so just hold the lock over the
1050 * If orinoco_lock fails, we've unplugged the card. In this
1051 * case just abort. */
1052 if (orinoco_lock(priv, &flags) != 0)
1055 /* extract desc and skb from queue */
1056 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
1057 desc = rx_data->desc;
1059 list_del(&rx_data->list);
1062 orinoco_rx(dev, desc, skb);
1067 orinoco_unlock(priv, &flags);
1070 /********************************************************************/
1071 /* Rx path (info frames) */
1072 /********************************************************************/
1074 static void print_linkstatus(struct net_device *dev, u16 status)
1078 if (suppress_linkstatus)
1082 case HERMES_LINKSTATUS_NOT_CONNECTED:
1083 s = "Not Connected";
1085 case HERMES_LINKSTATUS_CONNECTED:
1088 case HERMES_LINKSTATUS_DISCONNECTED:
1091 case HERMES_LINKSTATUS_AP_CHANGE:
1094 case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1095 s = "AP Out of Range";
1097 case HERMES_LINKSTATUS_AP_IN_RANGE:
1100 case HERMES_LINKSTATUS_ASSOC_FAILED:
1101 s = "Association Failed";
1107 printk(KERN_DEBUG "%s: New link status: %s (%04x)\n",
1108 dev->name, s, status);
1111 /* Search scan results for requested BSSID, join it if found */
1112 static void orinoco_join_ap(struct work_struct *work)
1114 struct orinoco_private *priv =
1115 container_of(work, struct orinoco_private, join_work);
1116 struct net_device *dev = priv->ndev;
1117 struct hermes *hw = &priv->hw;
1119 unsigned long flags;
1123 } __attribute__ ((packed)) req;
1124 const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
1125 struct prism2_scan_apinfo *atom = NULL;
1131 /* Allocate buffer for scan results */
1132 buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
1136 if (orinoco_lock(priv, &flags) != 0)
1139 /* Sanity checks in case user changed something in the meantime */
1140 if (!priv->bssid_fixed)
1143 if (strlen(priv->desired_essid) == 0)
1146 /* Read scan results from the firmware */
1147 err = hermes_read_ltv(hw, USER_BAP,
1148 HERMES_RID_SCANRESULTSTABLE,
1149 MAX_SCAN_LEN, &len, buf);
1151 printk(KERN_ERR "%s: Cannot read scan results\n",
1156 len = HERMES_RECLEN_TO_BYTES(len);
1158 /* Go through the scan results looking for the channel of the AP
1159 * we were requested to join */
1160 for (; offset + atom_len <= len; offset += atom_len) {
1161 atom = (struct prism2_scan_apinfo *) (buf + offset);
1162 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1169 DEBUG(1, "%s: Requested AP not found in scan results\n",
1174 memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1175 req.channel = atom->channel; /* both are little-endian */
1176 err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1179 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1182 orinoco_unlock(priv, &flags);
1188 /* Send new BSSID to userspace */
1189 static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
1191 struct net_device *dev = priv->ndev;
1192 struct hermes *hw = &priv->hw;
1193 union iwreq_data wrqu;
1196 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
1197 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1201 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1203 /* Send event to user space */
1204 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
1207 static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
1209 struct net_device *dev = priv->ndev;
1210 struct hermes *hw = &priv->hw;
1211 union iwreq_data wrqu;
1219 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
1220 sizeof(buf), NULL, &buf);
1224 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1226 int rem = sizeof(buf) - (ie - &buf[0]);
1227 wrqu.data.length = ie[1] + 2;
1228 if (wrqu.data.length > rem)
1229 wrqu.data.length = rem;
1231 if (wrqu.data.length)
1232 /* Send event to user space */
1233 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
1237 static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
1239 struct net_device *dev = priv->ndev;
1240 struct hermes *hw = &priv->hw;
1241 union iwreq_data wrqu;
1243 u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
1249 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_RESP_INFO,
1250 sizeof(buf), NULL, &buf);
1254 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1256 int rem = sizeof(buf) - (ie - &buf[0]);
1257 wrqu.data.length = ie[1] + 2;
1258 if (wrqu.data.length > rem)
1259 wrqu.data.length = rem;
1261 if (wrqu.data.length)
1262 /* Send event to user space */
1263 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
1267 static void orinoco_send_wevents(struct work_struct *work)
1269 struct orinoco_private *priv =
1270 container_of(work, struct orinoco_private, wevent_work);
1271 unsigned long flags;
1273 if (orinoco_lock(priv, &flags) != 0)
1276 orinoco_send_assocreqie_wevent(priv);
1277 orinoco_send_assocrespie_wevent(priv);
1278 orinoco_send_bssid_wevent(priv);
1280 orinoco_unlock(priv, &flags);
1283 static void qbuf_scan(struct orinoco_private *priv, void *buf,
1286 struct orinoco_scan_data *sd;
1287 unsigned long flags;
1289 sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
1294 spin_lock_irqsave(&priv->scan_lock, flags);
1295 list_add_tail(&sd->list, &priv->scan_list);
1296 spin_unlock_irqrestore(&priv->scan_lock, flags);
1298 schedule_work(&priv->process_scan);
1301 static void qabort_scan(struct orinoco_private *priv)
1303 struct orinoco_scan_data *sd;
1304 unsigned long flags;
1306 sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
1307 sd->len = -1; /* Abort */
1309 spin_lock_irqsave(&priv->scan_lock, flags);
1310 list_add_tail(&sd->list, &priv->scan_list);
1311 spin_unlock_irqrestore(&priv->scan_lock, flags);
1313 schedule_work(&priv->process_scan);
1316 static void orinoco_process_scan_results(struct work_struct *work)
1318 struct orinoco_private *priv =
1319 container_of(work, struct orinoco_private, process_scan);
1320 struct orinoco_scan_data *sd, *temp;
1321 unsigned long flags;
1326 spin_lock_irqsave(&priv->scan_lock, flags);
1327 list_for_each_entry_safe(sd, temp, &priv->scan_list, list) {
1328 spin_unlock_irqrestore(&priv->scan_lock, flags);
1334 list_del(&sd->list);
1338 if (type == HERMES_INQ_CHANNELINFO)
1339 orinoco_add_extscan_result(priv, buf, len);
1341 orinoco_add_hostscan_results(priv, buf, len);
1344 } else if (priv->scan_request) {
1345 /* Either abort or complete the scan */
1346 cfg80211_scan_done(priv->scan_request, (len < 0));
1347 priv->scan_request = NULL;
1350 spin_lock_irqsave(&priv->scan_lock, flags);
1352 spin_unlock_irqrestore(&priv->scan_lock, flags);
1355 static void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
1357 struct orinoco_private *priv = ndev_priv(dev);
1362 } __attribute__ ((packed)) info;
1366 /* This is an answer to an INQUIRE command that we did earlier,
1367 * or an information "event" generated by the card
1368 * The controller return to us a pseudo frame containing
1369 * the information in question - Jean II */
1370 infofid = hermes_read_regn(hw, INFOFID);
1372 /* Read the info frame header - don't try too hard */
1373 err = hermes_bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1376 printk(KERN_ERR "%s: error %d reading info frame. "
1377 "Frame dropped.\n", dev->name, err);
1381 len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1382 type = le16_to_cpu(info.type);
1385 case HERMES_INQ_TALLIES: {
1386 struct hermes_tallies_frame tallies;
1387 struct iw_statistics *wstats = &priv->wstats;
1389 if (len > sizeof(tallies)) {
1390 printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1392 len = sizeof(tallies);
1395 err = hermes_bap_pread(hw, IRQ_BAP, &tallies, len,
1396 infofid, sizeof(info));
1400 /* Increment our various counters */
1401 /* wstats->discard.nwid - no wrong BSSID stuff */
1402 wstats->discard.code +=
1403 le16_to_cpu(tallies.RxWEPUndecryptable);
1404 if (len == sizeof(tallies))
1405 wstats->discard.code +=
1406 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1407 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1408 wstats->discard.misc +=
1409 le16_to_cpu(tallies.TxDiscardsWrongSA);
1410 wstats->discard.fragment +=
1411 le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1412 wstats->discard.retries +=
1413 le16_to_cpu(tallies.TxRetryLimitExceeded);
1414 /* wstats->miss.beacon - no match */
1417 case HERMES_INQ_LINKSTATUS: {
1418 struct hermes_linkstatus linkstatus;
1422 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
1425 if (len != sizeof(linkstatus)) {
1426 printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1431 err = hermes_bap_pread(hw, IRQ_BAP, &linkstatus, len,
1432 infofid, sizeof(info));
1435 newstatus = le16_to_cpu(linkstatus.linkstatus);
1437 /* Symbol firmware uses "out of range" to signal that
1438 * the hostscan frame can be requested. */
1439 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
1440 priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
1441 priv->has_hostscan && priv->scan_request) {
1442 hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
1446 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
1447 || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1448 || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
1451 netif_carrier_on(dev);
1452 else if (!ignore_disconnect)
1453 netif_carrier_off(dev);
1455 if (newstatus != priv->last_linkstatus) {
1456 priv->last_linkstatus = newstatus;
1457 print_linkstatus(dev, newstatus);
1458 /* The info frame contains only one word which is the
1459 * status (see hermes.h). The status is pretty boring
1460 * in itself, that's why we export the new BSSID...
1462 schedule_work(&priv->wevent_work);
1466 case HERMES_INQ_SCAN:
1467 if (!priv->scan_request && priv->bssid_fixed &&
1468 priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
1469 schedule_work(&priv->join_work);
1473 case HERMES_INQ_HOSTSCAN:
1474 case HERMES_INQ_HOSTSCAN_SYMBOL: {
1475 /* Result of a scanning. Contains information about
1476 * cells in the vicinity - Jean II */
1481 printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
1487 /* Allocate buffer for results */
1488 buf = kmalloc(len, GFP_ATOMIC);
1490 /* No memory, so can't printk()... */
1495 /* Read scan data */
1496 err = hermes_bap_pread(hw, IRQ_BAP, (void *) buf, len,
1497 infofid, sizeof(info));
1504 #ifdef ORINOCO_DEBUG
1507 printk(KERN_DEBUG "Scan result [%02X", buf[0]);
1508 for (i = 1; i < (len * 2); i++)
1509 printk(":%02X", buf[i]);
1512 #endif /* ORINOCO_DEBUG */
1514 qbuf_scan(priv, buf, len, type);
1517 case HERMES_INQ_CHANNELINFO:
1519 struct agere_ext_scan_info *bss;
1521 if (!priv->scan_request) {
1522 printk(KERN_DEBUG "%s: Got chaninfo without scan, "
1523 "len=%d\n", dev->name, len);
1527 /* An empty result indicates that the scan is complete */
1529 qbuf_scan(priv, NULL, len, type);
1534 else if (len < (offsetof(struct agere_ext_scan_info,
1536 /* Drop this result now so we don't have to
1537 * keep checking later */
1539 "%s: Ext scan results too short (%d bytes)\n",
1544 bss = kmalloc(len, GFP_ATOMIC);
1548 /* Read scan data */
1549 err = hermes_bap_pread(hw, IRQ_BAP, (void *) bss, len,
1550 infofid, sizeof(info));
1554 qbuf_scan(priv, bss, len, type);
1558 case HERMES_INQ_SEC_STAT_AGERE:
1559 /* Security status (Agere specific) */
1560 /* Ignore this frame for now */
1561 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
1565 printk(KERN_DEBUG "%s: Unknown information frame received: "
1566 "type 0x%04x, length %d\n", dev->name, type, len);
1567 /* We don't actually do anything about it */
1574 static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
1576 if (net_ratelimit())
1577 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
1580 /********************************************************************/
1581 /* Internal hardware control routines */
1582 /********************************************************************/
1584 static int __orinoco_up(struct orinoco_private *priv)
1586 struct net_device *dev = priv->ndev;
1587 struct hermes *hw = &priv->hw;
1590 netif_carrier_off(dev); /* just to make sure */
1592 err = __orinoco_commit(priv);
1594 printk(KERN_ERR "%s: Error %d configuring card\n",
1599 /* Fire things up again */
1600 hermes_set_irqmask(hw, ORINOCO_INTEN);
1601 err = hermes_enable_port(hw, 0);
1603 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
1608 netif_start_queue(dev);
1613 static int __orinoco_down(struct orinoco_private *priv)
1615 struct net_device *dev = priv->ndev;
1616 struct hermes *hw = &priv->hw;
1619 netif_stop_queue(dev);
1621 if (!priv->hw_unavailable) {
1622 if (!priv->broken_disableport) {
1623 err = hermes_disable_port(hw, 0);
1625 /* Some firmwares (e.g. Intersil 1.3.x) seem
1626 * to have problems disabling the port, oh
1628 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
1630 priv->broken_disableport = 1;
1633 hermes_set_irqmask(hw, 0);
1634 hermes_write_regn(hw, EVACK, 0xffff);
1637 /* firmware will have to reassociate */
1638 netif_carrier_off(dev);
1639 priv->last_linkstatus = 0xffff;
1644 static int orinoco_reinit_firmware(struct orinoco_private *priv)
1646 struct hermes *hw = &priv->hw;
1649 err = hermes_init(hw);
1650 if (priv->do_fw_download && !err) {
1651 err = orinoco_download(priv);
1653 priv->do_fw_download = 0;
1656 err = orinoco_hw_allocate_fid(priv);
1662 __orinoco_set_multicast_list(struct net_device *dev)
1664 struct orinoco_private *priv = ndev_priv(dev);
1666 int promisc, mc_count;
1668 /* The Hermes doesn't seem to have an allmulti mode, so we go
1669 * into promiscuous mode and let the upper levels deal. */
1670 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
1671 (netdev_mc_count(dev) > MAX_MULTICAST(priv))) {
1676 mc_count = netdev_mc_count(dev);
1679 err = __orinoco_hw_set_multicast_list(priv, dev, mc_count, promisc);
1684 /* This must be called from user context, without locks held - use
1685 * schedule_work() */
1686 void orinoco_reset(struct work_struct *work)
1688 struct orinoco_private *priv =
1689 container_of(work, struct orinoco_private, reset_work);
1690 struct net_device *dev = priv->ndev;
1691 struct hermes *hw = &priv->hw;
1693 unsigned long flags;
1695 if (orinoco_lock(priv, &flags) != 0)
1696 /* When the hardware becomes available again, whatever
1697 * detects that is responsible for re-initializing
1698 * it. So no need for anything further */
1701 netif_stop_queue(dev);
1703 /* Shut off interrupts. Depending on what state the hardware
1704 * is in, this might not work, but we'll try anyway */
1705 hermes_set_irqmask(hw, 0);
1706 hermes_write_regn(hw, EVACK, 0xffff);
1708 priv->hw_unavailable++;
1709 priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
1710 netif_carrier_off(dev);
1712 orinoco_unlock(priv, &flags);
1714 /* Scanning support: Notify scan cancellation */
1715 if (priv->scan_request) {
1716 cfg80211_scan_done(priv->scan_request, 1);
1717 priv->scan_request = NULL;
1720 if (priv->hard_reset) {
1721 err = (*priv->hard_reset)(priv);
1723 printk(KERN_ERR "%s: orinoco_reset: Error %d "
1724 "performing hard reset\n", dev->name, err);
1729 err = orinoco_reinit_firmware(priv);
1731 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
1736 /* This has to be called from user context */
1737 spin_lock_irq(&priv->lock);
1739 priv->hw_unavailable--;
1741 /* priv->open or priv->hw_unavailable might have changed while
1742 * we dropped the lock */
1743 if (priv->open && (!priv->hw_unavailable)) {
1744 err = __orinoco_up(priv);
1746 printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
1749 dev->trans_start = jiffies;
1752 spin_unlock_irq(&priv->lock);
1756 hermes_set_irqmask(hw, 0);
1757 netif_device_detach(dev);
1758 printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
1761 static int __orinoco_commit(struct orinoco_private *priv)
1763 struct net_device *dev = priv->ndev;
1766 err = orinoco_hw_program_rids(priv);
1768 /* FIXME: what about netif_tx_lock */
1769 (void) __orinoco_set_multicast_list(dev);
1774 /* Ensures configuration changes are applied. May result in a reset.
1775 * The caller should hold priv->lock
1777 int orinoco_commit(struct orinoco_private *priv)
1779 struct net_device *dev = priv->ndev;
1780 hermes_t *hw = &priv->hw;
1783 if (priv->broken_disableport) {
1784 schedule_work(&priv->reset_work);
1788 err = hermes_disable_port(hw, 0);
1790 printk(KERN_WARNING "%s: Unable to disable port "
1791 "while reconfiguring card\n", dev->name);
1792 priv->broken_disableport = 1;
1796 err = __orinoco_commit(priv);
1798 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
1803 err = hermes_enable_port(hw, 0);
1805 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
1812 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
1813 schedule_work(&priv->reset_work);
1819 /********************************************************************/
1820 /* Interrupt handler */
1821 /********************************************************************/
1823 static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
1825 printk(KERN_DEBUG "%s: TICK\n", dev->name);
1828 static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
1830 /* This seems to happen a fair bit under load, but ignoring it
1831 seems to work fine...*/
1832 printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
1836 irqreturn_t orinoco_interrupt(int irq, void *dev_id)
1838 struct orinoco_private *priv = dev_id;
1839 struct net_device *dev = priv->ndev;
1840 hermes_t *hw = &priv->hw;
1841 int count = MAX_IRQLOOPS_PER_IRQ;
1843 /* These are used to detect a runaway interrupt situation.
1845 * If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
1846 * we panic and shut down the hardware
1848 /* jiffies value the last time we were called */
1849 static int last_irq_jiffy; /* = 0 */
1850 static int loops_this_jiffy; /* = 0 */
1851 unsigned long flags;
1853 if (orinoco_lock(priv, &flags) != 0) {
1854 /* If hw is unavailable - we don't know if the irq was
1859 evstat = hermes_read_regn(hw, EVSTAT);
1860 events = evstat & hw->inten;
1862 orinoco_unlock(priv, &flags);
1866 if (jiffies != last_irq_jiffy)
1867 loops_this_jiffy = 0;
1868 last_irq_jiffy = jiffies;
1870 while (events && count--) {
1871 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
1872 printk(KERN_WARNING "%s: IRQ handler is looping too "
1873 "much! Resetting.\n", dev->name);
1874 /* Disable interrupts for now */
1875 hermes_set_irqmask(hw, 0);
1876 schedule_work(&priv->reset_work);
1880 /* Check the card hasn't been removed */
1881 if (!hermes_present(hw)) {
1882 DEBUG(0, "orinoco_interrupt(): card removed\n");
1886 if (events & HERMES_EV_TICK)
1887 __orinoco_ev_tick(dev, hw);
1888 if (events & HERMES_EV_WTERR)
1889 __orinoco_ev_wterr(dev, hw);
1890 if (events & HERMES_EV_INFDROP)
1891 __orinoco_ev_infdrop(dev, hw);
1892 if (events & HERMES_EV_INFO)
1893 __orinoco_ev_info(dev, hw);
1894 if (events & HERMES_EV_RX)
1895 __orinoco_ev_rx(dev, hw);
1896 if (events & HERMES_EV_TXEXC)
1897 __orinoco_ev_txexc(dev, hw);
1898 if (events & HERMES_EV_TX)
1899 __orinoco_ev_tx(dev, hw);
1900 if (events & HERMES_EV_ALLOC)
1901 __orinoco_ev_alloc(dev, hw);
1903 hermes_write_regn(hw, EVACK, evstat);
1905 evstat = hermes_read_regn(hw, EVSTAT);
1906 events = evstat & hw->inten;
1909 orinoco_unlock(priv, &flags);
1912 EXPORT_SYMBOL(orinoco_interrupt);
1914 /********************************************************************/
1915 /* Power management */
1916 /********************************************************************/
1917 #if defined(CONFIG_PM_SLEEP) && !defined(CONFIG_HERMES_CACHE_FW_ON_INIT)
1918 static int orinoco_pm_notifier(struct notifier_block *notifier,
1919 unsigned long pm_event,
1922 struct orinoco_private *priv = container_of(notifier,
1923 struct orinoco_private,
1926 /* All we need to do is cache the firmware before suspend, and
1927 * release it when we come out.
1929 * Only need to do this if we're downloading firmware. */
1930 if (!priv->do_fw_download)
1934 case PM_HIBERNATION_PREPARE:
1935 case PM_SUSPEND_PREPARE:
1936 orinoco_cache_fw(priv, 0);
1939 case PM_POST_RESTORE:
1940 /* Restore from hibernation failed. We need to clean
1941 * up in exactly the same way, so fall through. */
1942 case PM_POST_HIBERNATION:
1943 case PM_POST_SUSPEND:
1944 orinoco_uncache_fw(priv);
1947 case PM_RESTORE_PREPARE:
1955 static void orinoco_register_pm_notifier(struct orinoco_private *priv)
1957 priv->pm_notifier.notifier_call = orinoco_pm_notifier;
1958 register_pm_notifier(&priv->pm_notifier);
1961 static void orinoco_unregister_pm_notifier(struct orinoco_private *priv)
1963 unregister_pm_notifier(&priv->pm_notifier);
1965 #else /* !PM_SLEEP || HERMES_CACHE_FW_ON_INIT */
1966 #define orinoco_register_pm_notifier(priv) do { } while(0)
1967 #define orinoco_unregister_pm_notifier(priv) do { } while(0)
1970 /********************************************************************/
1971 /* Initialization */
1972 /********************************************************************/
1974 int orinoco_init(struct orinoco_private *priv)
1976 struct device *dev = priv->dev;
1977 struct wiphy *wiphy = priv_to_wiphy(priv);
1978 hermes_t *hw = &priv->hw;
1981 /* No need to lock, the hw_unavailable flag is already set in
1982 * alloc_orinocodev() */
1983 priv->nicbuf_size = IEEE80211_MAX_FRAME_LEN + ETH_HLEN;
1985 /* Initialize the firmware */
1986 err = hermes_init(hw);
1988 dev_err(dev, "Failed to initialize firmware (err = %d)\n",
1993 err = determine_fw_capabilities(priv, wiphy->fw_version,
1994 sizeof(wiphy->fw_version),
1995 &wiphy->hw_version);
1997 dev_err(dev, "Incompatible firmware, aborting\n");
2001 if (priv->do_fw_download) {
2002 #ifdef CONFIG_HERMES_CACHE_FW_ON_INIT
2003 orinoco_cache_fw(priv, 0);
2006 err = orinoco_download(priv);
2008 priv->do_fw_download = 0;
2010 /* Check firmware version again */
2011 err = determine_fw_capabilities(priv, wiphy->fw_version,
2012 sizeof(wiphy->fw_version),
2013 &wiphy->hw_version);
2015 dev_err(dev, "Incompatible firmware, aborting\n");
2020 if (priv->has_port3)
2021 dev_info(dev, "Ad-hoc demo mode supported\n");
2023 dev_info(dev, "IEEE standard IBSS ad-hoc mode supported\n");
2025 dev_info(dev, "WEP supported, %s-bit key\n",
2026 priv->has_big_wep ? "104" : "40");
2027 if (priv->has_wpa) {
2028 dev_info(dev, "WPA-PSK supported\n");
2029 if (orinoco_mic_init(priv)) {
2030 dev_err(dev, "Failed to setup MIC crypto algorithm. "
2031 "Disabling WPA support\n");
2036 err = orinoco_hw_read_card_settings(priv, wiphy->perm_addr);
2040 err = orinoco_hw_allocate_fid(priv);
2042 dev_err(dev, "Failed to allocate NIC buffer!\n");
2046 /* Set up the default configuration */
2047 priv->iw_mode = NL80211_IFTYPE_STATION;
2048 /* By default use IEEE/IBSS ad-hoc mode if we have it */
2049 priv->prefer_port3 = priv->has_port3 && (!priv->has_ibss);
2050 set_port_type(priv);
2051 priv->channel = 0; /* use firmware default */
2053 priv->promiscuous = 0;
2054 priv->encode_alg = ORINOCO_ALG_NONE;
2056 priv->wpa_enabled = 0;
2057 priv->tkip_cm_active = 0;
2059 priv->wpa_ie_len = 0;
2060 priv->wpa_ie = NULL;
2062 if (orinoco_wiphy_register(wiphy)) {
2067 /* Make the hardware available, as long as it hasn't been
2068 * removed elsewhere (e.g. by PCMCIA hot unplug) */
2069 spin_lock_irq(&priv->lock);
2070 priv->hw_unavailable--;
2071 spin_unlock_irq(&priv->lock);
2073 dev_dbg(dev, "Ready\n");
2078 EXPORT_SYMBOL(orinoco_init);
2080 static const struct net_device_ops orinoco_netdev_ops = {
2081 .ndo_open = orinoco_open,
2082 .ndo_stop = orinoco_stop,
2083 .ndo_start_xmit = orinoco_xmit,
2084 .ndo_set_multicast_list = orinoco_set_multicast_list,
2085 .ndo_change_mtu = orinoco_change_mtu,
2086 .ndo_set_mac_address = eth_mac_addr,
2087 .ndo_validate_addr = eth_validate_addr,
2088 .ndo_tx_timeout = orinoco_tx_timeout,
2089 .ndo_get_stats = orinoco_get_stats,
2092 /* Allocate private data.
2094 * This driver has a number of structures associated with it
2095 * netdev - Net device structure for each network interface
2096 * wiphy - structure associated with wireless phy
2097 * wireless_dev (wdev) - structure for each wireless interface
2098 * hw - structure for hermes chip info
2099 * card - card specific structure for use by the card driver
2100 * (airport, orinoco_cs)
2101 * priv - orinoco private data
2102 * device - generic linux device structure
2104 * +---------+ +---------+
2105 * | wiphy | | netdev |
2106 * | +-------+ | +-------+
2107 * | | priv | | | wdev |
2108 * | | +-----+ +-+-------+
2114 * priv has a link to netdev and device
2115 * wdev has a link to wiphy
2117 struct orinoco_private
2118 *alloc_orinocodev(int sizeof_card,
2119 struct device *device,
2120 int (*hard_reset)(struct orinoco_private *),
2121 int (*stop_fw)(struct orinoco_private *, int))
2123 struct orinoco_private *priv;
2124 struct wiphy *wiphy;
2127 * NOTE: We only support a single virtual interface
2128 * but this may change when monitor mode is added
2130 wiphy = wiphy_new(&orinoco_cfg_ops,
2131 sizeof(struct orinoco_private) + sizeof_card);
2135 priv = wiphy_priv(wiphy);
2139 priv->card = (void *)((unsigned long)priv
2140 + sizeof(struct orinoco_private));
2144 orinoco_wiphy_init(wiphy);
2147 priv->wireless_data.spy_data = &priv->spy_data;
2150 /* Set up default callbacks */
2151 priv->hard_reset = hard_reset;
2152 priv->stop_fw = stop_fw;
2154 spin_lock_init(&priv->lock);
2156 priv->hw_unavailable = 1; /* orinoco_init() must clear this
2157 * before anything else touches the
2159 INIT_WORK(&priv->reset_work, orinoco_reset);
2160 INIT_WORK(&priv->join_work, orinoco_join_ap);
2161 INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
2163 INIT_LIST_HEAD(&priv->rx_list);
2164 tasklet_init(&priv->rx_tasklet, orinoco_rx_isr_tasklet,
2165 (unsigned long) priv);
2167 spin_lock_init(&priv->scan_lock);
2168 INIT_LIST_HEAD(&priv->scan_list);
2169 INIT_WORK(&priv->process_scan, orinoco_process_scan_results);
2171 priv->last_linkstatus = 0xffff;
2173 #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
2174 priv->cached_pri_fw = NULL;
2175 priv->cached_fw = NULL;
2178 /* Register PM notifiers */
2179 orinoco_register_pm_notifier(priv);
2183 EXPORT_SYMBOL(alloc_orinocodev);
2185 /* We can only support a single interface. We provide a separate
2186 * function to set it up to distinguish between hardware
2187 * initialisation and interface setup.
2189 * The base_addr and irq parameters are passed on to netdev for use
2192 int orinoco_if_add(struct orinoco_private *priv,
2193 unsigned long base_addr,
2196 struct wiphy *wiphy = priv_to_wiphy(priv);
2197 struct wireless_dev *wdev;
2198 struct net_device *dev;
2201 dev = alloc_etherdev(sizeof(struct wireless_dev));
2206 /* Initialise wireless_dev */
2207 wdev = netdev_priv(dev);
2208 wdev->wiphy = wiphy;
2209 wdev->iftype = NL80211_IFTYPE_STATION;
2211 /* Setup / override net_device fields */
2212 dev->ieee80211_ptr = wdev;
2213 dev->netdev_ops = &orinoco_netdev_ops;
2214 dev->watchdog_timeo = HZ; /* 1 second timeout */
2215 dev->wireless_handlers = &orinoco_handler_def;
2217 dev->wireless_data = &priv->wireless_data;
2219 /* we use the default eth_mac_addr for setting the MAC addr */
2221 /* Reserve space in skb for the SNAP header */
2222 dev->hard_header_len += ENCAPS_OVERHEAD;
2224 netif_carrier_off(dev);
2226 memcpy(dev->dev_addr, wiphy->perm_addr, ETH_ALEN);
2227 memcpy(dev->perm_addr, wiphy->perm_addr, ETH_ALEN);
2229 dev->base_addr = base_addr;
2232 SET_NETDEV_DEV(dev, priv->dev);
2233 ret = register_netdev(dev);
2239 /* Report what we've done */
2240 dev_dbg(priv->dev, "Registerred interface %s.\n", dev->name);
2248 EXPORT_SYMBOL(orinoco_if_add);
2250 void orinoco_if_del(struct orinoco_private *priv)
2252 struct net_device *dev = priv->ndev;
2254 unregister_netdev(dev);
2257 EXPORT_SYMBOL(orinoco_if_del);
2259 void free_orinocodev(struct orinoco_private *priv)
2261 struct wiphy *wiphy = priv_to_wiphy(priv);
2262 struct orinoco_rx_data *rx_data, *temp;
2263 struct orinoco_scan_data *sd, *sdtemp;
2265 wiphy_unregister(wiphy);
2267 /* If the tasklet is scheduled when we call tasklet_kill it
2268 * will run one final time. However the tasklet will only
2269 * drain priv->rx_list if the hw is still available. */
2270 tasklet_kill(&priv->rx_tasklet);
2272 /* Explicitly drain priv->rx_list */
2273 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
2274 list_del(&rx_data->list);
2276 dev_kfree_skb(rx_data->skb);
2277 kfree(rx_data->desc);
2281 cancel_work_sync(&priv->process_scan);
2282 /* Explicitly drain priv->scan_list */
2283 list_for_each_entry_safe(sd, sdtemp, &priv->scan_list, list) {
2284 list_del(&sd->list);
2286 if ((sd->len > 0) && sd->buf)
2291 orinoco_unregister_pm_notifier(priv);
2292 orinoco_uncache_fw(priv);
2294 priv->wpa_ie_len = 0;
2295 kfree(priv->wpa_ie);
2296 orinoco_mic_free(priv);
2299 EXPORT_SYMBOL(free_orinocodev);
2301 int orinoco_up(struct orinoco_private *priv)
2303 struct net_device *dev = priv->ndev;
2304 unsigned long flags;
2307 spin_lock_irqsave(&priv->lock, flags);
2309 err = orinoco_reinit_firmware(priv);
2311 printk(KERN_ERR "%s: Error %d re-initializing firmware\n",
2316 netif_device_attach(dev);
2317 priv->hw_unavailable--;
2319 if (priv->open && !priv->hw_unavailable) {
2320 err = __orinoco_up(priv);
2322 printk(KERN_ERR "%s: Error %d restarting card\n",
2327 spin_unlock_irqrestore(&priv->lock, flags);
2331 EXPORT_SYMBOL(orinoco_up);
2333 void orinoco_down(struct orinoco_private *priv)
2335 struct net_device *dev = priv->ndev;
2336 unsigned long flags;
2339 spin_lock_irqsave(&priv->lock, flags);
2340 err = __orinoco_down(priv);
2342 printk(KERN_WARNING "%s: Error %d downing interface\n",
2345 netif_device_detach(dev);
2346 priv->hw_unavailable++;
2347 spin_unlock_irqrestore(&priv->lock, flags);
2349 EXPORT_SYMBOL(orinoco_down);
2351 /********************************************************************/
2352 /* Module initialization */
2353 /********************************************************************/
2355 /* Can't be declared "const" or the whole __initdata section will
2357 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
2358 " (David Gibson <hermes@gibson.dropbear.id.au>, "
2359 "Pavel Roskin <proski@gnu.org>, et al)";
2361 static int __init init_orinoco(void)
2363 printk(KERN_DEBUG "%s\n", version);
2367 static void __exit exit_orinoco(void)
2371 module_init(init_orinoco);
2372 module_exit(exit_orinoco);