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/ethtool.h>
87 #include <linux/suspend.h>
88 #include <linux/if_arp.h>
89 #include <linux/wireless.h>
90 #include <linux/ieee80211.h>
91 #include <net/iw_handler.h>
92 #include <net/cfg80211.h>
94 #include "hermes_rid.h"
95 #include "hermes_dld.h"
106 /********************************************************************/
107 /* Module information */
108 /********************************************************************/
110 MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & "
111 "David Gibson <hermes@gibson.dropbear.id.au>");
112 MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based "
113 "and similar wireless cards");
114 MODULE_LICENSE("Dual MPL/GPL");
116 /* Level of debugging. Used in the macros in orinoco.h */
118 int orinoco_debug = ORINOCO_DEBUG;
119 EXPORT_SYMBOL(orinoco_debug);
120 module_param(orinoco_debug, int, 0644);
121 MODULE_PARM_DESC(orinoco_debug, "Debug level");
124 static int suppress_linkstatus; /* = 0 */
125 module_param(suppress_linkstatus, bool, 0644);
126 MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
128 static int ignore_disconnect; /* = 0 */
129 module_param(ignore_disconnect, int, 0644);
130 MODULE_PARM_DESC(ignore_disconnect,
131 "Don't report lost link to the network layer");
133 int force_monitor; /* = 0 */
134 module_param(force_monitor, int, 0644);
135 MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
137 /********************************************************************/
138 /* Internal constants */
139 /********************************************************************/
141 /* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
142 static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
143 #define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
145 #define ORINOCO_MIN_MTU 256
146 #define ORINOCO_MAX_MTU (IEEE80211_MAX_DATA_LEN - ENCAPS_OVERHEAD)
148 #define MAX_IRQLOOPS_PER_IRQ 10
149 #define MAX_IRQLOOPS_PER_JIFFY (20000/HZ) /* Based on a guestimate of
150 * how many events the
152 * legitimately generate */
154 #define DUMMY_FID 0xFFFF
156 /*#define MAX_MULTICAST(priv) (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
157 HERMES_MAX_MULTICAST : 0)*/
158 #define MAX_MULTICAST(priv) (HERMES_MAX_MULTICAST)
160 #define ORINOCO_INTEN (HERMES_EV_RX | HERMES_EV_ALLOC \
161 | HERMES_EV_TX | HERMES_EV_TXEXC \
162 | HERMES_EV_WTERR | HERMES_EV_INFO \
165 static const struct ethtool_ops orinoco_ethtool_ops;
167 /********************************************************************/
169 /********************************************************************/
171 /* Beginning of the Tx descriptor, used in TxExc handling */
172 struct hermes_txexc_data {
173 struct hermes_tx_descriptor desc;
177 } __attribute__ ((packed));
179 /* Rx frame header except compatibility 802.3 header */
180 struct hermes_rx_descriptor {
201 } __attribute__ ((packed));
203 struct orinoco_rx_data {
204 struct hermes_rx_descriptor *desc;
206 struct list_head list;
209 struct orinoco_scan_data {
213 struct list_head list;
216 /********************************************************************/
217 /* Function prototypes */
218 /********************************************************************/
220 static int __orinoco_set_multicast_list(struct net_device *dev);
221 static int __orinoco_up(struct orinoco_private *priv);
222 static int __orinoco_down(struct orinoco_private *priv);
223 static int __orinoco_commit(struct orinoco_private *priv);
225 /********************************************************************/
226 /* Internal helper functions */
227 /********************************************************************/
229 void set_port_type(struct orinoco_private *priv)
231 switch (priv->iw_mode) {
232 case NL80211_IFTYPE_STATION:
234 priv->createibss = 0;
236 case NL80211_IFTYPE_ADHOC:
237 if (priv->prefer_port3) {
239 priv->createibss = 0;
241 priv->port_type = priv->ibss_port;
242 priv->createibss = 1;
245 case NL80211_IFTYPE_MONITOR:
247 priv->createibss = 0;
250 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
255 /********************************************************************/
257 /********************************************************************/
259 static int orinoco_open(struct net_device *dev)
261 struct orinoco_private *priv = ndev_priv(dev);
265 if (orinoco_lock(priv, &flags) != 0)
268 err = __orinoco_up(priv);
273 orinoco_unlock(priv, &flags);
278 static int orinoco_stop(struct net_device *dev)
280 struct orinoco_private *priv = ndev_priv(dev);
283 /* We mustn't use orinoco_lock() here, because we need to be
284 able to close the interface even if hw_unavailable is set
285 (e.g. as we're released after a PC Card removal) */
286 spin_lock_irq(&priv->lock);
290 err = __orinoco_down(priv);
292 spin_unlock_irq(&priv->lock);
297 static struct net_device_stats *orinoco_get_stats(struct net_device *dev)
299 struct orinoco_private *priv = ndev_priv(dev);
304 static void orinoco_set_multicast_list(struct net_device *dev)
306 struct orinoco_private *priv = ndev_priv(dev);
309 if (orinoco_lock(priv, &flags) != 0) {
310 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
311 "called when hw_unavailable\n", dev->name);
315 __orinoco_set_multicast_list(dev);
316 orinoco_unlock(priv, &flags);
319 static int orinoco_change_mtu(struct net_device *dev, int new_mtu)
321 struct orinoco_private *priv = ndev_priv(dev);
323 if ((new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU))
326 /* MTU + encapsulation + header length */
327 if ((new_mtu + ENCAPS_OVERHEAD + sizeof(struct ieee80211_hdr)) >
328 (priv->nicbuf_size - ETH_HLEN))
336 /********************************************************************/
338 /********************************************************************/
340 static int orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
342 struct orinoco_private *priv = ndev_priv(dev);
343 struct net_device_stats *stats = &priv->stats;
344 hermes_t *hw = &priv->hw;
346 u16 txfid = priv->txfid;
351 if (!netif_running(dev)) {
352 printk(KERN_ERR "%s: Tx on stopped device!\n",
354 return NETDEV_TX_BUSY;
357 if (netif_queue_stopped(dev)) {
358 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
360 return NETDEV_TX_BUSY;
363 if (orinoco_lock(priv, &flags) != 0) {
364 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
366 return NETDEV_TX_BUSY;
369 if (!netif_carrier_ok(dev) ||
370 (priv->iw_mode == NL80211_IFTYPE_MONITOR)) {
371 /* Oops, the firmware hasn't established a connection,
372 silently drop the packet (this seems to be the
377 /* Check packet length */
378 if (skb->len < ETH_HLEN)
381 tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
383 if (priv->encode_alg == IW_ENCODE_ALG_TKIP)
384 tx_control |= (priv->tx_key << HERMES_MIC_KEY_ID_SHIFT) |
387 if (priv->has_alt_txcntl) {
388 /* WPA enabled firmwares have tx_cntl at the end of
389 * the 802.11 header. So write zeroed descriptor and
390 * 802.11 header at the same time
392 char desc[HERMES_802_3_OFFSET];
393 __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
395 memset(&desc, 0, sizeof(desc));
397 *txcntl = cpu_to_le16(tx_control);
398 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
402 printk(KERN_ERR "%s: Error %d writing Tx "
403 "descriptor to BAP\n", dev->name, err);
407 struct hermes_tx_descriptor desc;
409 memset(&desc, 0, sizeof(desc));
411 desc.tx_control = cpu_to_le16(tx_control);
412 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
416 printk(KERN_ERR "%s: Error %d writing Tx "
417 "descriptor to BAP\n", dev->name, err);
421 /* Clear the 802.11 header and data length fields - some
422 * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
423 * if this isn't done. */
424 hermes_clear_words(hw, HERMES_DATA0,
425 HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
428 eh = (struct ethhdr *)skb->data;
430 /* Encapsulate Ethernet-II frames */
431 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
432 struct header_struct {
433 struct ethhdr eth; /* 802.3 header */
434 u8 encap[6]; /* 802.2 header */
435 } __attribute__ ((packed)) hdr;
437 /* Strip destination and source from the data */
438 skb_pull(skb, 2 * ETH_ALEN);
440 /* And move them to a separate header */
441 memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
442 hdr.eth.h_proto = htons(sizeof(encaps_hdr) + skb->len);
443 memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
445 /* Insert the SNAP header */
446 if (skb_headroom(skb) < sizeof(hdr)) {
448 "%s: Not enough headroom for 802.2 headers %d\n",
449 dev->name, skb_headroom(skb));
452 eh = (struct ethhdr *) skb_push(skb, sizeof(hdr));
453 memcpy(eh, &hdr, sizeof(hdr));
456 err = hermes_bap_pwrite(hw, USER_BAP, skb->data, skb->len,
457 txfid, HERMES_802_3_OFFSET);
459 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
464 /* Calculate Michael MIC */
465 if (priv->encode_alg == IW_ENCODE_ALG_TKIP) {
466 u8 mic_buf[MICHAEL_MIC_LEN + 1];
472 /* MIC start is on an odd boundary */
473 mic_buf[0] = skb->data[skb->len - 1];
475 offset = skb->len - 1;
476 len = MICHAEL_MIC_LEN + 1;
480 len = MICHAEL_MIC_LEN;
483 orinoco_mic(priv->tx_tfm_mic,
484 priv->tkip_key[priv->tx_key].tx_mic,
485 eh->h_dest, eh->h_source, 0 /* priority */,
486 skb->data + ETH_HLEN, skb->len - ETH_HLEN, mic);
489 err = hermes_bap_pwrite(hw, USER_BAP, &mic_buf[0], len,
490 txfid, HERMES_802_3_OFFSET + offset);
492 printk(KERN_ERR "%s: Error %d writing MIC to BAP\n",
498 /* Finally, we actually initiate the send */
499 netif_stop_queue(dev);
501 err = hermes_docmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
504 netif_start_queue(dev);
506 printk(KERN_ERR "%s: Error %d transmitting packet\n",
511 dev->trans_start = jiffies;
512 stats->tx_bytes += HERMES_802_3_OFFSET + skb->len;
520 orinoco_unlock(priv, &flags);
526 schedule_work(&priv->reset_work);
527 orinoco_unlock(priv, &flags);
528 return NETDEV_TX_BUSY;
531 static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
533 struct orinoco_private *priv = ndev_priv(dev);
534 u16 fid = hermes_read_regn(hw, ALLOCFID);
536 if (fid != priv->txfid) {
537 if (fid != DUMMY_FID)
538 printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
543 hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
546 static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
548 struct orinoco_private *priv = ndev_priv(dev);
549 struct net_device_stats *stats = &priv->stats;
553 netif_wake_queue(dev);
555 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
558 static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
560 struct orinoco_private *priv = ndev_priv(dev);
561 struct net_device_stats *stats = &priv->stats;
562 u16 fid = hermes_read_regn(hw, TXCOMPLFID);
564 struct hermes_txexc_data hdr;
567 if (fid == DUMMY_FID)
568 return; /* Nothing's really happened */
570 /* Read part of the frame header - we need status and addr1 */
571 err = hermes_bap_pread(hw, IRQ_BAP, &hdr,
572 sizeof(struct hermes_txexc_data),
575 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
579 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
580 "(FID=%04X error %d)\n",
581 dev->name, fid, err);
585 DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
588 /* We produce a TXDROP event only for retry or lifetime
589 * exceeded, because that's the only status that really mean
590 * that this particular node went away.
591 * Other errors means that *we* screwed up. - Jean II */
592 status = le16_to_cpu(hdr.desc.status);
593 if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
594 union iwreq_data wrqu;
596 /* Copy 802.11 dest address.
597 * We use the 802.11 header because the frame may
598 * not be 802.3 or may be mangled...
599 * In Ad-Hoc mode, it will be the node address.
600 * In managed mode, it will be most likely the AP addr
601 * User space will figure out how to convert it to
602 * whatever it needs (IP address or else).
604 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
605 wrqu.addr.sa_family = ARPHRD_ETHER;
607 /* Send event to user space */
608 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
611 netif_wake_queue(dev);
614 static void orinoco_tx_timeout(struct net_device *dev)
616 struct orinoco_private *priv = ndev_priv(dev);
617 struct net_device_stats *stats = &priv->stats;
618 struct hermes *hw = &priv->hw;
620 printk(KERN_WARNING "%s: Tx timeout! "
621 "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
622 dev->name, hermes_read_regn(hw, ALLOCFID),
623 hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
627 schedule_work(&priv->reset_work);
630 /********************************************************************/
631 /* Rx path (data frames) */
632 /********************************************************************/
634 /* Does the frame have a SNAP header indicating it should be
635 * de-encapsulated to Ethernet-II? */
636 static inline int is_ethersnap(void *_hdr)
640 /* We de-encapsulate all packets which, a) have SNAP headers
641 * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
642 * and where b) the OUI of the SNAP header is 00:00:00 or
643 * 00:00:f8 - we need both because different APs appear to use
644 * different OUIs for some reason */
645 return (memcmp(hdr, &encaps_hdr, 5) == 0)
646 && ((hdr[5] == 0x00) || (hdr[5] == 0xf8));
649 static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
650 int level, int noise)
652 struct iw_quality wstats;
653 wstats.level = level - 0x95;
654 wstats.noise = noise - 0x95;
655 wstats.qual = (level > noise) ? (level - noise) : 0;
656 wstats.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
657 /* Update spy records */
658 wireless_spy_update(dev, mac, &wstats);
661 static void orinoco_stat_gather(struct net_device *dev,
663 struct hermes_rx_descriptor *desc)
665 struct orinoco_private *priv = ndev_priv(dev);
667 /* Using spy support with lots of Rx packets, like in an
668 * infrastructure (AP), will really slow down everything, because
669 * the MAC address must be compared to each entry of the spy list.
670 * If the user really asks for it (set some address in the
671 * spy list), we do it, but he will pay the price.
672 * Note that to get here, you need both WIRELESS_SPY
673 * compiled in AND some addresses in the list !!!
675 /* Note : gcc will optimise the whole section away if
676 * WIRELESS_SPY is not defined... - Jean II */
677 if (SPY_NUMBER(priv)) {
678 orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
679 desc->signal, desc->silence);
684 * orinoco_rx_monitor - handle received monitor frames.
689 * desc rx descriptor of the frame
691 * Call context: interrupt
693 static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
694 struct hermes_rx_descriptor *desc)
696 u32 hdrlen = 30; /* return full header by default */
702 struct orinoco_private *priv = ndev_priv(dev);
703 struct net_device_stats *stats = &priv->stats;
704 hermes_t *hw = &priv->hw;
706 len = le16_to_cpu(desc->data_len);
708 /* Determine the size of the header and the data */
709 fc = le16_to_cpu(desc->frame_ctl);
710 switch (fc & IEEE80211_FCTL_FTYPE) {
711 case IEEE80211_FTYPE_DATA:
712 if ((fc & IEEE80211_FCTL_TODS)
713 && (fc & IEEE80211_FCTL_FROMDS))
719 case IEEE80211_FTYPE_MGMT:
723 case IEEE80211_FTYPE_CTL:
724 switch (fc & IEEE80211_FCTL_STYPE) {
725 case IEEE80211_STYPE_PSPOLL:
726 case IEEE80211_STYPE_RTS:
727 case IEEE80211_STYPE_CFEND:
728 case IEEE80211_STYPE_CFENDACK:
731 case IEEE80211_STYPE_CTS:
732 case IEEE80211_STYPE_ACK:
738 /* Unknown frame type */
742 /* sanity check the length */
743 if (datalen > IEEE80211_MAX_DATA_LEN + 12) {
744 printk(KERN_DEBUG "%s: oversized monitor frame, "
745 "data length = %d\n", dev->name, datalen);
746 stats->rx_length_errors++;
750 skb = dev_alloc_skb(hdrlen + datalen);
752 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
757 /* Copy the 802.11 header to the skb */
758 memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
759 skb_reset_mac_header(skb);
761 /* If any, copy the data from the card to the skb */
763 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
764 ALIGN(datalen, 2), rxfid,
765 HERMES_802_2_OFFSET);
767 printk(KERN_ERR "%s: error %d reading monitor frame\n",
774 skb->ip_summed = CHECKSUM_NONE;
775 skb->pkt_type = PACKET_OTHERHOST;
776 skb->protocol = cpu_to_be16(ETH_P_802_2);
779 stats->rx_bytes += skb->len;
785 dev_kfree_skb_irq(skb);
791 static void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
793 struct orinoco_private *priv = ndev_priv(dev);
794 struct net_device_stats *stats = &priv->stats;
795 struct iw_statistics *wstats = &priv->wstats;
796 struct sk_buff *skb = NULL;
799 struct hermes_rx_descriptor *desc;
800 struct orinoco_rx_data *rx_data;
803 desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
806 "%s: Can't allocate space for RX descriptor\n",
811 rxfid = hermes_read_regn(hw, RXFID);
813 err = hermes_bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
816 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
817 "Frame dropped.\n", dev->name, err);
821 status = le16_to_cpu(desc->status);
823 if (status & HERMES_RXSTAT_BADCRC) {
824 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
826 stats->rx_crc_errors++;
830 /* Handle frames in monitor mode */
831 if (priv->iw_mode == NL80211_IFTYPE_MONITOR) {
832 orinoco_rx_monitor(dev, rxfid, desc);
836 if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
837 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
839 wstats->discard.code++;
843 length = le16_to_cpu(desc->data_len);
846 if (length < 3) { /* No for even an 802.2 LLC header */
847 /* At least on Symbol firmware with PCF we get quite a
848 lot of these legitimately - Poll frames with no
852 if (length > IEEE80211_MAX_DATA_LEN) {
853 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
855 stats->rx_length_errors++;
859 /* Payload size does not include Michael MIC. Increase payload
860 * size to read it together with the data. */
861 if (status & HERMES_RXSTAT_MIC)
862 length += MICHAEL_MIC_LEN;
864 /* We need space for the packet data itself, plus an ethernet
865 header, plus 2 bytes so we can align the IP header on a
866 32bit boundary, plus 1 byte so we can read in odd length
867 packets from the card, which has an IO granularity of 16
869 skb = dev_alloc_skb(length+ETH_HLEN+2+1);
871 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
876 /* We'll prepend the header, so reserve space for it. The worst
877 case is no decapsulation, when 802.3 header is prepended and
878 nothing is removed. 2 is for aligning the IP header. */
879 skb_reserve(skb, ETH_HLEN + 2);
881 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, length),
882 ALIGN(length, 2), rxfid,
883 HERMES_802_2_OFFSET);
885 printk(KERN_ERR "%s: error %d reading frame. "
886 "Frame dropped.\n", dev->name, err);
890 /* Add desc and skb to rx queue */
891 rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
893 printk(KERN_WARNING "%s: Can't allocate RX packet\n",
897 rx_data->desc = desc;
899 list_add_tail(&rx_data->list, &priv->rx_list);
900 tasklet_schedule(&priv->rx_tasklet);
905 dev_kfree_skb_irq(skb);
913 static void orinoco_rx(struct net_device *dev,
914 struct hermes_rx_descriptor *desc,
917 struct orinoco_private *priv = ndev_priv(dev);
918 struct net_device_stats *stats = &priv->stats;
923 status = le16_to_cpu(desc->status);
924 length = le16_to_cpu(desc->data_len);
925 fc = le16_to_cpu(desc->frame_ctl);
927 /* Calculate and check MIC */
928 if (status & HERMES_RXSTAT_MIC) {
929 int key_id = ((status & HERMES_RXSTAT_MIC_KEY_ID) >>
930 HERMES_MIC_KEY_ID_SHIFT);
931 u8 mic[MICHAEL_MIC_LEN];
933 u8 *src = (fc & IEEE80211_FCTL_FROMDS) ?
934 desc->addr3 : desc->addr2;
936 /* Extract Michael MIC from payload */
937 rxmic = skb->data + skb->len - MICHAEL_MIC_LEN;
939 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
940 length -= MICHAEL_MIC_LEN;
942 orinoco_mic(priv->rx_tfm_mic,
943 priv->tkip_key[key_id].rx_mic,
946 0, /* priority or QoS? */
951 if (memcmp(mic, rxmic,
953 union iwreq_data wrqu;
954 struct iw_michaelmicfailure wxmic;
956 printk(KERN_WARNING "%s: "
957 "Invalid Michael MIC in data frame from %pM, "
959 dev->name, src, key_id);
961 /* TODO: update stats */
963 /* Notify userspace */
964 memset(&wxmic, 0, sizeof(wxmic));
965 wxmic.flags = key_id & IW_MICFAILURE_KEY_ID;
966 wxmic.flags |= (desc->addr1[0] & 1) ?
967 IW_MICFAILURE_GROUP : IW_MICFAILURE_PAIRWISE;
968 wxmic.src_addr.sa_family = ARPHRD_ETHER;
969 memcpy(wxmic.src_addr.sa_data, src, ETH_ALEN);
971 (void) orinoco_hw_get_tkip_iv(priv, key_id,
974 memset(&wrqu, 0, sizeof(wrqu));
975 wrqu.data.length = sizeof(wxmic);
976 wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu,
983 /* Handle decapsulation
984 * In most cases, the firmware tell us about SNAP frames.
985 * For some reason, the SNAP frames sent by LinkSys APs
986 * are not properly recognised by most firmwares.
987 * So, check ourselves */
988 if (length >= ENCAPS_OVERHEAD &&
989 (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
990 ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
991 is_ethersnap(skb->data))) {
992 /* These indicate a SNAP within 802.2 LLC within
993 802.11 frame which we'll need to de-encapsulate to
994 the original EthernetII frame. */
995 hdr = (struct ethhdr *)skb_push(skb,
996 ETH_HLEN - ENCAPS_OVERHEAD);
998 /* 802.3 frame - prepend 802.3 header as is */
999 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
1000 hdr->h_proto = htons(length);
1002 memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
1003 if (fc & IEEE80211_FCTL_FROMDS)
1004 memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
1006 memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
1008 skb->protocol = eth_type_trans(skb, dev);
1009 skb->ip_summed = CHECKSUM_NONE;
1010 if (fc & IEEE80211_FCTL_TODS)
1011 skb->pkt_type = PACKET_OTHERHOST;
1013 /* Process the wireless stats if needed */
1014 orinoco_stat_gather(dev, skb, desc);
1016 /* Pass the packet to the networking stack */
1018 stats->rx_packets++;
1019 stats->rx_bytes += length;
1026 stats->rx_dropped++;
1029 static void orinoco_rx_isr_tasklet(unsigned long data)
1031 struct orinoco_private *priv = (struct orinoco_private *) data;
1032 struct net_device *dev = priv->ndev;
1033 struct orinoco_rx_data *rx_data, *temp;
1034 struct hermes_rx_descriptor *desc;
1035 struct sk_buff *skb;
1036 unsigned long flags;
1038 /* orinoco_rx requires the driver lock, and we also need to
1039 * protect priv->rx_list, so just hold the lock over the
1042 * If orinoco_lock fails, we've unplugged the card. In this
1043 * case just abort. */
1044 if (orinoco_lock(priv, &flags) != 0)
1047 /* extract desc and skb from queue */
1048 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
1049 desc = rx_data->desc;
1051 list_del(&rx_data->list);
1054 orinoco_rx(dev, desc, skb);
1059 orinoco_unlock(priv, &flags);
1062 /********************************************************************/
1063 /* Rx path (info frames) */
1064 /********************************************************************/
1066 static void print_linkstatus(struct net_device *dev, u16 status)
1070 if (suppress_linkstatus)
1074 case HERMES_LINKSTATUS_NOT_CONNECTED:
1075 s = "Not Connected";
1077 case HERMES_LINKSTATUS_CONNECTED:
1080 case HERMES_LINKSTATUS_DISCONNECTED:
1083 case HERMES_LINKSTATUS_AP_CHANGE:
1086 case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1087 s = "AP Out of Range";
1089 case HERMES_LINKSTATUS_AP_IN_RANGE:
1092 case HERMES_LINKSTATUS_ASSOC_FAILED:
1093 s = "Association Failed";
1099 printk(KERN_DEBUG "%s: New link status: %s (%04x)\n",
1100 dev->name, s, status);
1103 /* Search scan results for requested BSSID, join it if found */
1104 static void orinoco_join_ap(struct work_struct *work)
1106 struct orinoco_private *priv =
1107 container_of(work, struct orinoco_private, join_work);
1108 struct net_device *dev = priv->ndev;
1109 struct hermes *hw = &priv->hw;
1111 unsigned long flags;
1115 } __attribute__ ((packed)) req;
1116 const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
1117 struct prism2_scan_apinfo *atom = NULL;
1123 /* Allocate buffer for scan results */
1124 buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
1128 if (orinoco_lock(priv, &flags) != 0)
1131 /* Sanity checks in case user changed something in the meantime */
1132 if (!priv->bssid_fixed)
1135 if (strlen(priv->desired_essid) == 0)
1138 /* Read scan results from the firmware */
1139 err = hermes_read_ltv(hw, USER_BAP,
1140 HERMES_RID_SCANRESULTSTABLE,
1141 MAX_SCAN_LEN, &len, buf);
1143 printk(KERN_ERR "%s: Cannot read scan results\n",
1148 len = HERMES_RECLEN_TO_BYTES(len);
1150 /* Go through the scan results looking for the channel of the AP
1151 * we were requested to join */
1152 for (; offset + atom_len <= len; offset += atom_len) {
1153 atom = (struct prism2_scan_apinfo *) (buf + offset);
1154 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1161 DEBUG(1, "%s: Requested AP not found in scan results\n",
1166 memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1167 req.channel = atom->channel; /* both are little-endian */
1168 err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1171 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1174 orinoco_unlock(priv, &flags);
1180 /* Send new BSSID to userspace */
1181 static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
1183 struct net_device *dev = priv->ndev;
1184 struct hermes *hw = &priv->hw;
1185 union iwreq_data wrqu;
1188 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
1189 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1193 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1195 /* Send event to user space */
1196 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
1199 static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
1201 struct net_device *dev = priv->ndev;
1202 struct hermes *hw = &priv->hw;
1203 union iwreq_data wrqu;
1211 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
1212 sizeof(buf), NULL, &buf);
1216 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1218 int rem = sizeof(buf) - (ie - &buf[0]);
1219 wrqu.data.length = ie[1] + 2;
1220 if (wrqu.data.length > rem)
1221 wrqu.data.length = rem;
1223 if (wrqu.data.length)
1224 /* Send event to user space */
1225 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
1229 static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
1231 struct net_device *dev = priv->ndev;
1232 struct hermes *hw = &priv->hw;
1233 union iwreq_data wrqu;
1235 u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
1241 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_RESP_INFO,
1242 sizeof(buf), NULL, &buf);
1246 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1248 int rem = sizeof(buf) - (ie - &buf[0]);
1249 wrqu.data.length = ie[1] + 2;
1250 if (wrqu.data.length > rem)
1251 wrqu.data.length = rem;
1253 if (wrqu.data.length)
1254 /* Send event to user space */
1255 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
1259 static void orinoco_send_wevents(struct work_struct *work)
1261 struct orinoco_private *priv =
1262 container_of(work, struct orinoco_private, wevent_work);
1263 unsigned long flags;
1265 if (orinoco_lock(priv, &flags) != 0)
1268 orinoco_send_assocreqie_wevent(priv);
1269 orinoco_send_assocrespie_wevent(priv);
1270 orinoco_send_bssid_wevent(priv);
1272 orinoco_unlock(priv, &flags);
1275 static void qbuf_scan(struct orinoco_private *priv, void *buf,
1278 struct orinoco_scan_data *sd;
1279 unsigned long flags;
1281 sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
1286 spin_lock_irqsave(&priv->scan_lock, flags);
1287 list_add_tail(&sd->list, &priv->scan_list);
1288 spin_unlock_irqrestore(&priv->scan_lock, flags);
1290 schedule_work(&priv->process_scan);
1293 static void qabort_scan(struct orinoco_private *priv)
1295 struct orinoco_scan_data *sd;
1296 unsigned long flags;
1298 sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
1299 sd->len = -1; /* Abort */
1301 spin_lock_irqsave(&priv->scan_lock, flags);
1302 list_add_tail(&sd->list, &priv->scan_list);
1303 spin_unlock_irqrestore(&priv->scan_lock, flags);
1305 schedule_work(&priv->process_scan);
1308 static void orinoco_process_scan_results(struct work_struct *work)
1310 struct orinoco_private *priv =
1311 container_of(work, struct orinoco_private, process_scan);
1312 struct orinoco_scan_data *sd, *temp;
1313 unsigned long flags;
1318 spin_lock_irqsave(&priv->scan_lock, flags);
1319 list_for_each_entry_safe(sd, temp, &priv->scan_list, list) {
1320 spin_unlock_irqrestore(&priv->scan_lock, flags);
1326 list_del(&sd->list);
1330 if (type == HERMES_INQ_CHANNELINFO)
1331 orinoco_add_extscan_result(priv, buf, len);
1333 orinoco_add_hostscan_results(priv, buf, len);
1336 } else if (priv->scan_request) {
1337 /* Either abort or complete the scan */
1338 cfg80211_scan_done(priv->scan_request, (len < 0));
1339 priv->scan_request = NULL;
1342 spin_lock_irqsave(&priv->scan_lock, flags);
1344 spin_unlock_irqrestore(&priv->scan_lock, flags);
1347 static void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
1349 struct orinoco_private *priv = ndev_priv(dev);
1354 } __attribute__ ((packed)) info;
1358 /* This is an answer to an INQUIRE command that we did earlier,
1359 * or an information "event" generated by the card
1360 * The controller return to us a pseudo frame containing
1361 * the information in question - Jean II */
1362 infofid = hermes_read_regn(hw, INFOFID);
1364 /* Read the info frame header - don't try too hard */
1365 err = hermes_bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1368 printk(KERN_ERR "%s: error %d reading info frame. "
1369 "Frame dropped.\n", dev->name, err);
1373 len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1374 type = le16_to_cpu(info.type);
1377 case HERMES_INQ_TALLIES: {
1378 struct hermes_tallies_frame tallies;
1379 struct iw_statistics *wstats = &priv->wstats;
1381 if (len > sizeof(tallies)) {
1382 printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1384 len = sizeof(tallies);
1387 err = hermes_bap_pread(hw, IRQ_BAP, &tallies, len,
1388 infofid, sizeof(info));
1392 /* Increment our various counters */
1393 /* wstats->discard.nwid - no wrong BSSID stuff */
1394 wstats->discard.code +=
1395 le16_to_cpu(tallies.RxWEPUndecryptable);
1396 if (len == sizeof(tallies))
1397 wstats->discard.code +=
1398 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1399 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1400 wstats->discard.misc +=
1401 le16_to_cpu(tallies.TxDiscardsWrongSA);
1402 wstats->discard.fragment +=
1403 le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1404 wstats->discard.retries +=
1405 le16_to_cpu(tallies.TxRetryLimitExceeded);
1406 /* wstats->miss.beacon - no match */
1409 case HERMES_INQ_LINKSTATUS: {
1410 struct hermes_linkstatus linkstatus;
1414 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
1417 if (len != sizeof(linkstatus)) {
1418 printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1423 err = hermes_bap_pread(hw, IRQ_BAP, &linkstatus, len,
1424 infofid, sizeof(info));
1427 newstatus = le16_to_cpu(linkstatus.linkstatus);
1429 /* Symbol firmware uses "out of range" to signal that
1430 * the hostscan frame can be requested. */
1431 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
1432 priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
1433 priv->has_hostscan && priv->scan_request) {
1434 hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
1438 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
1439 || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1440 || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
1443 netif_carrier_on(dev);
1444 else if (!ignore_disconnect)
1445 netif_carrier_off(dev);
1447 if (newstatus != priv->last_linkstatus) {
1448 priv->last_linkstatus = newstatus;
1449 print_linkstatus(dev, newstatus);
1450 /* The info frame contains only one word which is the
1451 * status (see hermes.h). The status is pretty boring
1452 * in itself, that's why we export the new BSSID...
1454 schedule_work(&priv->wevent_work);
1458 case HERMES_INQ_SCAN:
1459 if (!priv->scan_request && priv->bssid_fixed &&
1460 priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
1461 schedule_work(&priv->join_work);
1465 case HERMES_INQ_HOSTSCAN:
1466 case HERMES_INQ_HOSTSCAN_SYMBOL: {
1467 /* Result of a scanning. Contains information about
1468 * cells in the vicinity - Jean II */
1473 printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
1479 /* Allocate buffer for results */
1480 buf = kmalloc(len, GFP_ATOMIC);
1482 /* No memory, so can't printk()... */
1487 /* Read scan data */
1488 err = hermes_bap_pread(hw, IRQ_BAP, (void *) buf, len,
1489 infofid, sizeof(info));
1496 #ifdef ORINOCO_DEBUG
1499 printk(KERN_DEBUG "Scan result [%02X", buf[0]);
1500 for (i = 1; i < (len * 2); i++)
1501 printk(":%02X", buf[i]);
1504 #endif /* ORINOCO_DEBUG */
1506 qbuf_scan(priv, buf, len, type);
1509 case HERMES_INQ_CHANNELINFO:
1511 struct agere_ext_scan_info *bss;
1513 if (!priv->scan_request) {
1514 printk(KERN_DEBUG "%s: Got chaninfo without scan, "
1515 "len=%d\n", dev->name, len);
1519 /* An empty result indicates that the scan is complete */
1521 qbuf_scan(priv, NULL, len, type);
1526 else if (len < (offsetof(struct agere_ext_scan_info,
1528 /* Drop this result now so we don't have to
1529 * keep checking later */
1531 "%s: Ext scan results too short (%d bytes)\n",
1536 bss = kmalloc(len, GFP_ATOMIC);
1540 /* Read scan data */
1541 err = hermes_bap_pread(hw, IRQ_BAP, (void *) bss, len,
1542 infofid, sizeof(info));
1546 qbuf_scan(priv, bss, len, type);
1550 case HERMES_INQ_SEC_STAT_AGERE:
1551 /* Security status (Agere specific) */
1552 /* Ignore this frame for now */
1553 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
1557 printk(KERN_DEBUG "%s: Unknown information frame received: "
1558 "type 0x%04x, length %d\n", dev->name, type, len);
1559 /* We don't actually do anything about it */
1566 static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
1568 if (net_ratelimit())
1569 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
1572 /********************************************************************/
1573 /* Internal hardware control routines */
1574 /********************************************************************/
1576 static int __orinoco_up(struct orinoco_private *priv)
1578 struct net_device *dev = priv->ndev;
1579 struct hermes *hw = &priv->hw;
1582 netif_carrier_off(dev); /* just to make sure */
1584 err = __orinoco_commit(priv);
1586 printk(KERN_ERR "%s: Error %d configuring card\n",
1591 /* Fire things up again */
1592 hermes_set_irqmask(hw, ORINOCO_INTEN);
1593 err = hermes_enable_port(hw, 0);
1595 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
1600 netif_start_queue(dev);
1605 static int __orinoco_down(struct orinoco_private *priv)
1607 struct net_device *dev = priv->ndev;
1608 struct hermes *hw = &priv->hw;
1611 netif_stop_queue(dev);
1613 if (!priv->hw_unavailable) {
1614 if (!priv->broken_disableport) {
1615 err = hermes_disable_port(hw, 0);
1617 /* Some firmwares (e.g. Intersil 1.3.x) seem
1618 * to have problems disabling the port, oh
1620 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
1622 priv->broken_disableport = 1;
1625 hermes_set_irqmask(hw, 0);
1626 hermes_write_regn(hw, EVACK, 0xffff);
1629 /* firmware will have to reassociate */
1630 netif_carrier_off(dev);
1631 priv->last_linkstatus = 0xffff;
1636 static int orinoco_reinit_firmware(struct orinoco_private *priv)
1638 struct hermes *hw = &priv->hw;
1641 err = hermes_init(hw);
1642 if (priv->do_fw_download && !err) {
1643 err = orinoco_download(priv);
1645 priv->do_fw_download = 0;
1648 err = orinoco_hw_allocate_fid(priv);
1654 __orinoco_set_multicast_list(struct net_device *dev)
1656 struct orinoco_private *priv = ndev_priv(dev);
1658 int promisc, mc_count;
1660 /* The Hermes doesn't seem to have an allmulti mode, so we go
1661 * into promiscuous mode and let the upper levels deal. */
1662 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
1663 (dev->mc_count > MAX_MULTICAST(priv))) {
1668 mc_count = dev->mc_count;
1671 err = __orinoco_hw_set_multicast_list(priv, dev->mc_list, mc_count,
1677 /* This must be called from user context, without locks held - use
1678 * schedule_work() */
1679 void orinoco_reset(struct work_struct *work)
1681 struct orinoco_private *priv =
1682 container_of(work, struct orinoco_private, reset_work);
1683 struct net_device *dev = priv->ndev;
1684 struct hermes *hw = &priv->hw;
1686 unsigned long flags;
1688 if (orinoco_lock(priv, &flags) != 0)
1689 /* When the hardware becomes available again, whatever
1690 * detects that is responsible for re-initializing
1691 * it. So no need for anything further */
1694 netif_stop_queue(dev);
1696 /* Shut off interrupts. Depending on what state the hardware
1697 * is in, this might not work, but we'll try anyway */
1698 hermes_set_irqmask(hw, 0);
1699 hermes_write_regn(hw, EVACK, 0xffff);
1701 priv->hw_unavailable++;
1702 priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
1703 netif_carrier_off(dev);
1705 orinoco_unlock(priv, &flags);
1707 /* Scanning support: Notify scan cancellation */
1708 if (priv->scan_request) {
1709 cfg80211_scan_done(priv->scan_request, 1);
1710 priv->scan_request = NULL;
1713 if (priv->hard_reset) {
1714 err = (*priv->hard_reset)(priv);
1716 printk(KERN_ERR "%s: orinoco_reset: Error %d "
1717 "performing hard reset\n", dev->name, err);
1722 err = orinoco_reinit_firmware(priv);
1724 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
1729 /* This has to be called from user context */
1730 spin_lock_irq(&priv->lock);
1732 priv->hw_unavailable--;
1734 /* priv->open or priv->hw_unavailable might have changed while
1735 * we dropped the lock */
1736 if (priv->open && (!priv->hw_unavailable)) {
1737 err = __orinoco_up(priv);
1739 printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
1742 dev->trans_start = jiffies;
1745 spin_unlock_irq(&priv->lock);
1749 hermes_set_irqmask(hw, 0);
1750 netif_device_detach(dev);
1751 printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
1754 static int __orinoco_commit(struct orinoco_private *priv)
1756 struct net_device *dev = priv->ndev;
1759 err = orinoco_hw_program_rids(priv);
1761 /* FIXME: what about netif_tx_lock */
1762 (void) __orinoco_set_multicast_list(dev);
1767 /* Ensures configuration changes are applied. May result in a reset.
1768 * The caller should hold priv->lock
1770 int orinoco_commit(struct orinoco_private *priv)
1772 struct net_device *dev = priv->ndev;
1773 hermes_t *hw = &priv->hw;
1776 if (priv->broken_disableport) {
1777 schedule_work(&priv->reset_work);
1781 err = hermes_disable_port(hw, 0);
1783 printk(KERN_WARNING "%s: Unable to disable port "
1784 "while reconfiguring card\n", dev->name);
1785 priv->broken_disableport = 1;
1789 err = __orinoco_commit(priv);
1791 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
1796 err = hermes_enable_port(hw, 0);
1798 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
1805 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
1806 schedule_work(&priv->reset_work);
1812 /********************************************************************/
1813 /* Interrupt handler */
1814 /********************************************************************/
1816 static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
1818 printk(KERN_DEBUG "%s: TICK\n", dev->name);
1821 static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
1823 /* This seems to happen a fair bit under load, but ignoring it
1824 seems to work fine...*/
1825 printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
1829 irqreturn_t orinoco_interrupt(int irq, void *dev_id)
1831 struct orinoco_private *priv = dev_id;
1832 struct net_device *dev = priv->ndev;
1833 hermes_t *hw = &priv->hw;
1834 int count = MAX_IRQLOOPS_PER_IRQ;
1836 /* These are used to detect a runaway interrupt situation.
1838 * If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
1839 * we panic and shut down the hardware
1841 /* jiffies value the last time we were called */
1842 static int last_irq_jiffy; /* = 0 */
1843 static int loops_this_jiffy; /* = 0 */
1844 unsigned long flags;
1846 if (orinoco_lock(priv, &flags) != 0) {
1847 /* If hw is unavailable - we don't know if the irq was
1852 evstat = hermes_read_regn(hw, EVSTAT);
1853 events = evstat & hw->inten;
1855 orinoco_unlock(priv, &flags);
1859 if (jiffies != last_irq_jiffy)
1860 loops_this_jiffy = 0;
1861 last_irq_jiffy = jiffies;
1863 while (events && count--) {
1864 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
1865 printk(KERN_WARNING "%s: IRQ handler is looping too "
1866 "much! Resetting.\n", dev->name);
1867 /* Disable interrupts for now */
1868 hermes_set_irqmask(hw, 0);
1869 schedule_work(&priv->reset_work);
1873 /* Check the card hasn't been removed */
1874 if (!hermes_present(hw)) {
1875 DEBUG(0, "orinoco_interrupt(): card removed\n");
1879 if (events & HERMES_EV_TICK)
1880 __orinoco_ev_tick(dev, hw);
1881 if (events & HERMES_EV_WTERR)
1882 __orinoco_ev_wterr(dev, hw);
1883 if (events & HERMES_EV_INFDROP)
1884 __orinoco_ev_infdrop(dev, hw);
1885 if (events & HERMES_EV_INFO)
1886 __orinoco_ev_info(dev, hw);
1887 if (events & HERMES_EV_RX)
1888 __orinoco_ev_rx(dev, hw);
1889 if (events & HERMES_EV_TXEXC)
1890 __orinoco_ev_txexc(dev, hw);
1891 if (events & HERMES_EV_TX)
1892 __orinoco_ev_tx(dev, hw);
1893 if (events & HERMES_EV_ALLOC)
1894 __orinoco_ev_alloc(dev, hw);
1896 hermes_write_regn(hw, EVACK, evstat);
1898 evstat = hermes_read_regn(hw, EVSTAT);
1899 events = evstat & hw->inten;
1902 orinoco_unlock(priv, &flags);
1905 EXPORT_SYMBOL(orinoco_interrupt);
1907 /********************************************************************/
1908 /* Power management */
1909 /********************************************************************/
1910 #if defined(CONFIG_PM_SLEEP) && !defined(CONFIG_HERMES_CACHE_FW_ON_INIT)
1911 static int orinoco_pm_notifier(struct notifier_block *notifier,
1912 unsigned long pm_event,
1915 struct orinoco_private *priv = container_of(notifier,
1916 struct orinoco_private,
1919 /* All we need to do is cache the firmware before suspend, and
1920 * release it when we come out.
1922 * Only need to do this if we're downloading firmware. */
1923 if (!priv->do_fw_download)
1927 case PM_HIBERNATION_PREPARE:
1928 case PM_SUSPEND_PREPARE:
1929 orinoco_cache_fw(priv, 0);
1932 case PM_POST_RESTORE:
1933 /* Restore from hibernation failed. We need to clean
1934 * up in exactly the same way, so fall through. */
1935 case PM_POST_HIBERNATION:
1936 case PM_POST_SUSPEND:
1937 orinoco_uncache_fw(priv);
1940 case PM_RESTORE_PREPARE:
1948 static void orinoco_register_pm_notifier(struct orinoco_private *priv)
1950 priv->pm_notifier.notifier_call = orinoco_pm_notifier;
1951 register_pm_notifier(&priv->pm_notifier);
1954 static void orinoco_unregister_pm_notifier(struct orinoco_private *priv)
1956 unregister_pm_notifier(&priv->pm_notifier);
1958 #else /* !PM_SLEEP || HERMES_CACHE_FW_ON_INIT */
1959 #define orinoco_register_pm_notifier(priv) do { } while(0)
1960 #define orinoco_unregister_pm_notifier(priv) do { } while(0)
1963 /********************************************************************/
1964 /* Initialization */
1965 /********************************************************************/
1967 int orinoco_init(struct orinoco_private *priv)
1969 struct device *dev = priv->dev;
1970 struct wiphy *wiphy = priv_to_wiphy(priv);
1971 hermes_t *hw = &priv->hw;
1974 /* No need to lock, the hw_unavailable flag is already set in
1975 * alloc_orinocodev() */
1976 priv->nicbuf_size = IEEE80211_MAX_FRAME_LEN + ETH_HLEN;
1978 /* Initialize the firmware */
1979 err = hermes_init(hw);
1981 dev_err(dev, "Failed to initialize firmware (err = %d)\n",
1986 err = determine_fw_capabilities(priv);
1988 dev_err(dev, "Incompatible firmware, aborting\n");
1992 if (priv->do_fw_download) {
1993 #ifdef CONFIG_HERMES_CACHE_FW_ON_INIT
1994 orinoco_cache_fw(priv, 0);
1997 err = orinoco_download(priv);
1999 priv->do_fw_download = 0;
2001 /* Check firmware version again */
2002 err = determine_fw_capabilities(priv);
2004 dev_err(dev, "Incompatible firmware, aborting\n");
2009 if (priv->has_port3)
2010 dev_info(dev, "Ad-hoc demo mode supported\n");
2012 dev_info(dev, "IEEE standard IBSS ad-hoc mode supported\n");
2014 dev_info(dev, "WEP supported, %s-bit key\n",
2015 priv->has_big_wep ? "104" : "40");
2016 if (priv->has_wpa) {
2017 dev_info(dev, "WPA-PSK supported\n");
2018 if (orinoco_mic_init(priv)) {
2019 dev_err(dev, "Failed to setup MIC crypto algorithm. "
2020 "Disabling WPA support\n");
2025 err = orinoco_hw_read_card_settings(priv, wiphy->perm_addr);
2029 err = orinoco_hw_allocate_fid(priv);
2031 dev_err(dev, "Failed to allocate NIC buffer!\n");
2035 /* Set up the default configuration */
2036 priv->iw_mode = NL80211_IFTYPE_STATION;
2037 /* By default use IEEE/IBSS ad-hoc mode if we have it */
2038 priv->prefer_port3 = priv->has_port3 && (!priv->has_ibss);
2039 set_port_type(priv);
2040 priv->channel = 0; /* use firmware default */
2042 priv->promiscuous = 0;
2043 priv->encode_alg = IW_ENCODE_ALG_NONE;
2045 priv->wpa_enabled = 0;
2046 priv->tkip_cm_active = 0;
2048 priv->wpa_ie_len = 0;
2049 priv->wpa_ie = NULL;
2051 if (orinoco_wiphy_register(wiphy)) {
2056 /* Make the hardware available, as long as it hasn't been
2057 * removed elsewhere (e.g. by PCMCIA hot unplug) */
2058 spin_lock_irq(&priv->lock);
2059 priv->hw_unavailable--;
2060 spin_unlock_irq(&priv->lock);
2062 dev_dbg(dev, "Ready\n");
2067 EXPORT_SYMBOL(orinoco_init);
2069 static const struct net_device_ops orinoco_netdev_ops = {
2070 .ndo_open = orinoco_open,
2071 .ndo_stop = orinoco_stop,
2072 .ndo_start_xmit = orinoco_xmit,
2073 .ndo_set_multicast_list = orinoco_set_multicast_list,
2074 .ndo_change_mtu = orinoco_change_mtu,
2075 .ndo_set_mac_address = eth_mac_addr,
2076 .ndo_validate_addr = eth_validate_addr,
2077 .ndo_tx_timeout = orinoco_tx_timeout,
2078 .ndo_get_stats = orinoco_get_stats,
2081 /* Allocate private data.
2083 * This driver has a number of structures associated with it
2084 * netdev - Net device structure for each network interface
2085 * wiphy - structure associated with wireless phy
2086 * wireless_dev (wdev) - structure for each wireless interface
2087 * hw - structure for hermes chip info
2088 * card - card specific structure for use by the card driver
2089 * (airport, orinoco_cs)
2090 * priv - orinoco private data
2091 * device - generic linux device structure
2093 * +---------+ +---------+
2094 * | wiphy | | netdev |
2095 * | +-------+ | +-------+
2096 * | | priv | | | wdev |
2097 * | | +-----+ +-+-------+
2103 * priv has a link to netdev and device
2104 * wdev has a link to wiphy
2106 struct orinoco_private
2107 *alloc_orinocodev(int sizeof_card,
2108 struct device *device,
2109 int (*hard_reset)(struct orinoco_private *),
2110 int (*stop_fw)(struct orinoco_private *, int))
2112 struct orinoco_private *priv;
2113 struct wiphy *wiphy;
2116 * NOTE: We only support a single virtual interface
2117 * but this may change when monitor mode is added
2119 wiphy = wiphy_new(&orinoco_cfg_ops,
2120 sizeof(struct orinoco_private) + sizeof_card);
2124 priv = wiphy_priv(wiphy);
2128 priv->card = (void *)((unsigned long)priv
2129 + sizeof(struct orinoco_private));
2133 orinoco_wiphy_init(wiphy);
2136 priv->wireless_data.spy_data = &priv->spy_data;
2139 /* Set up default callbacks */
2140 priv->hard_reset = hard_reset;
2141 priv->stop_fw = stop_fw;
2143 spin_lock_init(&priv->lock);
2145 priv->hw_unavailable = 1; /* orinoco_init() must clear this
2146 * before anything else touches the
2148 INIT_WORK(&priv->reset_work, orinoco_reset);
2149 INIT_WORK(&priv->join_work, orinoco_join_ap);
2150 INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
2152 INIT_LIST_HEAD(&priv->rx_list);
2153 tasklet_init(&priv->rx_tasklet, orinoco_rx_isr_tasklet,
2154 (unsigned long) priv);
2156 spin_lock_init(&priv->scan_lock);
2157 INIT_LIST_HEAD(&priv->scan_list);
2158 INIT_WORK(&priv->process_scan, orinoco_process_scan_results);
2160 priv->last_linkstatus = 0xffff;
2162 #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
2163 priv->cached_pri_fw = NULL;
2164 priv->cached_fw = NULL;
2167 /* Register PM notifiers */
2168 orinoco_register_pm_notifier(priv);
2172 EXPORT_SYMBOL(alloc_orinocodev);
2174 /* We can only support a single interface. We provide a separate
2175 * function to set it up to distinguish between hardware
2176 * initialisation and interface setup.
2178 * The base_addr and irq parameters are passed on to netdev for use
2181 int orinoco_if_add(struct orinoco_private *priv,
2182 unsigned long base_addr,
2185 struct wiphy *wiphy = priv_to_wiphy(priv);
2186 struct wireless_dev *wdev;
2187 struct net_device *dev;
2190 dev = alloc_etherdev(sizeof(struct wireless_dev));
2195 /* Initialise wireless_dev */
2196 wdev = netdev_priv(dev);
2197 wdev->wiphy = wiphy;
2198 wdev->iftype = NL80211_IFTYPE_STATION;
2200 /* Setup / override net_device fields */
2201 dev->ieee80211_ptr = wdev;
2202 dev->netdev_ops = &orinoco_netdev_ops;
2203 dev->watchdog_timeo = HZ; /* 1 second timeout */
2204 dev->ethtool_ops = &orinoco_ethtool_ops;
2205 dev->wireless_handlers = &orinoco_handler_def;
2207 dev->wireless_data = &priv->wireless_data;
2209 /* we use the default eth_mac_addr for setting the MAC addr */
2211 /* Reserve space in skb for the SNAP header */
2212 dev->hard_header_len += ENCAPS_OVERHEAD;
2214 netif_carrier_off(dev);
2216 memcpy(dev->dev_addr, wiphy->perm_addr, ETH_ALEN);
2218 dev->base_addr = base_addr;
2221 SET_NETDEV_DEV(dev, priv->dev);
2222 ret = register_netdev(dev);
2228 /* Report what we've done */
2229 dev_dbg(priv->dev, "Registerred interface %s.\n", dev->name);
2237 EXPORT_SYMBOL(orinoco_if_add);
2239 void orinoco_if_del(struct orinoco_private *priv)
2241 struct net_device *dev = priv->ndev;
2243 unregister_netdev(dev);
2246 EXPORT_SYMBOL(orinoco_if_del);
2248 void free_orinocodev(struct orinoco_private *priv)
2250 struct wiphy *wiphy = priv_to_wiphy(priv);
2251 struct orinoco_rx_data *rx_data, *temp;
2252 struct orinoco_scan_data *sd, *sdtemp;
2254 wiphy_unregister(wiphy);
2256 /* If the tasklet is scheduled when we call tasklet_kill it
2257 * will run one final time. However the tasklet will only
2258 * drain priv->rx_list if the hw is still available. */
2259 tasklet_kill(&priv->rx_tasklet);
2261 /* Explicitly drain priv->rx_list */
2262 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
2263 list_del(&rx_data->list);
2265 dev_kfree_skb(rx_data->skb);
2266 kfree(rx_data->desc);
2270 cancel_work_sync(&priv->process_scan);
2271 /* Explicitly drain priv->scan_list */
2272 list_for_each_entry_safe(sd, sdtemp, &priv->scan_list, list) {
2273 list_del(&sd->list);
2275 if ((sd->len > 0) && sd->buf)
2280 orinoco_unregister_pm_notifier(priv);
2281 orinoco_uncache_fw(priv);
2283 priv->wpa_ie_len = 0;
2284 kfree(priv->wpa_ie);
2285 orinoco_mic_free(priv);
2288 EXPORT_SYMBOL(free_orinocodev);
2290 int orinoco_up(struct orinoco_private *priv)
2292 struct net_device *dev = priv->ndev;
2293 unsigned long flags;
2296 spin_lock_irqsave(&priv->lock, flags);
2298 err = orinoco_reinit_firmware(priv);
2300 printk(KERN_ERR "%s: Error %d re-initializing firmware\n",
2305 netif_device_attach(dev);
2306 priv->hw_unavailable--;
2308 if (priv->open && !priv->hw_unavailable) {
2309 err = __orinoco_up(priv);
2311 printk(KERN_ERR "%s: Error %d restarting card\n",
2316 spin_unlock_irqrestore(&priv->lock, flags);
2320 EXPORT_SYMBOL(orinoco_up);
2322 void orinoco_down(struct orinoco_private *priv)
2324 struct net_device *dev = priv->ndev;
2325 unsigned long flags;
2328 spin_lock_irqsave(&priv->lock, flags);
2329 err = __orinoco_down(priv);
2331 printk(KERN_WARNING "%s: Error %d downing interface\n",
2334 netif_device_detach(dev);
2335 priv->hw_unavailable++;
2336 spin_unlock_irqrestore(&priv->lock, flags);
2338 EXPORT_SYMBOL(orinoco_down);
2340 static void orinoco_get_drvinfo(struct net_device *dev,
2341 struct ethtool_drvinfo *info)
2343 struct orinoco_private *priv = ndev_priv(dev);
2345 strncpy(info->driver, DRIVER_NAME, sizeof(info->driver) - 1);
2346 strncpy(info->version, DRIVER_VERSION, sizeof(info->version) - 1);
2347 strncpy(info->fw_version, priv->fw_name, sizeof(info->fw_version) - 1);
2348 if (dev->dev.parent)
2349 strncpy(info->bus_info, dev_name(dev->dev.parent),
2350 sizeof(info->bus_info) - 1);
2352 snprintf(info->bus_info, sizeof(info->bus_info) - 1,
2353 "PCMCIA %p", priv->hw.iobase);
2356 static const struct ethtool_ops orinoco_ethtool_ops = {
2357 .get_drvinfo = orinoco_get_drvinfo,
2358 .get_link = ethtool_op_get_link,
2361 /********************************************************************/
2362 /* Module initialization */
2363 /********************************************************************/
2365 /* Can't be declared "const" or the whole __initdata section will
2367 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
2368 " (David Gibson <hermes@gibson.dropbear.id.au>, "
2369 "Pavel Roskin <proski@gnu.org>, et al)";
2371 static int __init init_orinoco(void)
2373 printk(KERN_DEBUG "%s\n", version);
2377 static void __exit exit_orinoco(void)
2381 module_init(init_orinoco);
2382 module_exit(exit_orinoco);