orinoco: fix error reporting in rx_monitor
[orinoco_usb.git] / drivers / net / wireless / orinoco / main.c
blob7c0cdabc74becdbd533d42dddce7c5fa8124d15c
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
35 * Reserved.
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. */
49 * TODO
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/netdevice.h>
84 #include <linux/etherdevice.h>
85 #include <linux/ethtool.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>
92 #include "hermes_rid.h"
93 #include "hermes_dld.h"
94 #include "hw.h"
95 #include "scan.h"
96 #include "mic.h"
97 #include "fw.h"
98 #include "wext.h"
99 #include "main.h"
101 #include "orinoco.h"
103 /********************************************************************/
104 /* Module information */
105 /********************************************************************/
107 MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & "
108 "David Gibson <hermes@gibson.dropbear.id.au>");
109 MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based "
110 "and similar wireless cards");
111 MODULE_LICENSE("Dual MPL/GPL");
113 /* Level of debugging. Used in the macros in orinoco.h */
114 #ifdef ORINOCO_DEBUG
115 int orinoco_debug = ORINOCO_DEBUG;
116 EXPORT_SYMBOL(orinoco_debug);
117 module_param(orinoco_debug, int, 0644);
118 MODULE_PARM_DESC(orinoco_debug, "Debug level");
119 #endif
121 static int suppress_linkstatus; /* = 0 */
122 module_param(suppress_linkstatus, bool, 0644);
123 MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
125 static int ignore_disconnect; /* = 0 */
126 module_param(ignore_disconnect, int, 0644);
127 MODULE_PARM_DESC(ignore_disconnect,
128 "Don't report lost link to the network layer");
130 int force_monitor; /* = 0 */
131 module_param(force_monitor, int, 0644);
132 MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
134 /********************************************************************/
135 /* Internal constants */
136 /********************************************************************/
138 /* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
139 static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
140 #define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
142 #define ORINOCO_MIN_MTU 256
143 #define ORINOCO_MAX_MTU (IEEE80211_MAX_DATA_LEN - ENCAPS_OVERHEAD)
145 #define SYMBOL_MAX_VER_LEN (14)
146 #define MAX_IRQLOOPS_PER_IRQ 10
147 #define MAX_IRQLOOPS_PER_JIFFY (20000/HZ) /* Based on a guestimate of
148 * how many events the
149 * device could
150 * legitimately generate */
151 #define TX_NICBUF_SIZE_BUG 1585 /* Bug in Symbol firmware */
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 \
162 | HERMES_EV_INFDROP)
164 static const struct ethtool_ops orinoco_ethtool_ops;
166 /********************************************************************/
167 /* Data types */
168 /********************************************************************/
170 /* Beginning of the Tx descriptor, used in TxExc handling */
171 struct hermes_txexc_data {
172 struct hermes_tx_descriptor desc;
173 __le16 frame_ctl;
174 __le16 duration_id;
175 u8 addr1[ETH_ALEN];
176 } __attribute__ ((packed));
178 /* Rx frame header except compatibility 802.3 header */
179 struct hermes_rx_descriptor {
180 /* Control */
181 __le16 status;
182 __le32 time;
183 u8 silence;
184 u8 signal;
185 u8 rate;
186 u8 rxflow;
187 __le32 reserved;
189 /* 802.11 header */
190 __le16 frame_ctl;
191 __le16 duration_id;
192 u8 addr1[ETH_ALEN];
193 u8 addr2[ETH_ALEN];
194 u8 addr3[ETH_ALEN];
195 __le16 seq_ctl;
196 u8 addr4[ETH_ALEN];
198 /* Data length */
199 __le16 data_len;
200 } __attribute__ ((packed));
202 struct orinoco_rx_data {
203 struct hermes_rx_descriptor *desc;
204 struct sk_buff *skb;
205 struct list_head list;
208 /********************************************************************/
209 /* Function prototypes */
210 /********************************************************************/
212 static void __orinoco_set_multicast_list(struct net_device *dev);
214 /********************************************************************/
215 /* Internal helper functions */
216 /********************************************************************/
218 void set_port_type(struct orinoco_private *priv)
220 switch (priv->iw_mode) {
221 case IW_MODE_INFRA:
222 priv->port_type = 1;
223 priv->createibss = 0;
224 break;
225 case IW_MODE_ADHOC:
226 if (priv->prefer_port3) {
227 priv->port_type = 3;
228 priv->createibss = 0;
229 } else {
230 priv->port_type = priv->ibss_port;
231 priv->createibss = 1;
233 break;
234 case IW_MODE_MONITOR:
235 priv->port_type = 3;
236 priv->createibss = 0;
237 break;
238 default:
239 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
240 priv->ndev->name);
244 /********************************************************************/
245 /* Device methods */
246 /********************************************************************/
248 static int orinoco_open(struct net_device *dev)
250 struct orinoco_private *priv = netdev_priv(dev);
251 unsigned long flags;
252 int err;
254 if (orinoco_lock(priv, &flags) != 0)
255 return -EBUSY;
257 err = __orinoco_up(dev);
259 if (!err)
260 priv->open = 1;
262 orinoco_unlock(priv, &flags);
264 return err;
267 static int orinoco_stop(struct net_device *dev)
269 struct orinoco_private *priv = netdev_priv(dev);
270 int err = 0;
272 /* We mustn't use orinoco_lock() here, because we need to be
273 able to close the interface even if hw_unavailable is set
274 (e.g. as we're released after a PC Card removal) */
275 spin_lock_irq(&priv->lock);
277 priv->open = 0;
279 err = __orinoco_down(dev);
281 spin_unlock_irq(&priv->lock);
283 return err;
286 static struct net_device_stats *orinoco_get_stats(struct net_device *dev)
288 struct orinoco_private *priv = netdev_priv(dev);
290 return &priv->stats;
293 static void orinoco_set_multicast_list(struct net_device *dev)
295 struct orinoco_private *priv = netdev_priv(dev);
296 unsigned long flags;
298 if (orinoco_lock(priv, &flags) != 0) {
299 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
300 "called when hw_unavailable\n", dev->name);
301 return;
304 __orinoco_set_multicast_list(dev);
305 orinoco_unlock(priv, &flags);
308 static int orinoco_change_mtu(struct net_device *dev, int new_mtu)
310 struct orinoco_private *priv = netdev_priv(dev);
312 if ((new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU))
313 return -EINVAL;
315 /* MTU + encapsulation + header length */
316 if ((new_mtu + ENCAPS_OVERHEAD + sizeof(struct ieee80211_hdr)) >
317 (priv->nicbuf_size - ETH_HLEN))
318 return -EINVAL;
320 dev->mtu = new_mtu;
322 return 0;
325 /********************************************************************/
326 /* Tx path */
327 /********************************************************************/
329 static int orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
331 struct orinoco_private *priv = netdev_priv(dev);
332 struct net_device_stats *stats = &priv->stats;
333 hermes_t *hw = &priv->hw;
334 int err = 0;
335 u16 txfid = priv->txfid;
336 struct ethhdr *eh;
337 int tx_control;
338 unsigned long flags;
340 if (!netif_running(dev)) {
341 printk(KERN_ERR "%s: Tx on stopped device!\n",
342 dev->name);
343 return NETDEV_TX_BUSY;
346 if (netif_queue_stopped(dev)) {
347 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
348 dev->name);
349 return NETDEV_TX_BUSY;
352 if (orinoco_lock(priv, &flags) != 0) {
353 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
354 dev->name);
355 return NETDEV_TX_BUSY;
358 if (!netif_carrier_ok(dev) || (priv->iw_mode == IW_MODE_MONITOR)) {
359 /* Oops, the firmware hasn't established a connection,
360 silently drop the packet (this seems to be the
361 safest approach). */
362 goto drop;
365 /* Check packet length */
366 if (skb->len < ETH_HLEN)
367 goto drop;
369 tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
371 if (priv->encode_alg == IW_ENCODE_ALG_TKIP)
372 tx_control |= (priv->tx_key << HERMES_MIC_KEY_ID_SHIFT) |
373 HERMES_TXCTRL_MIC;
375 if (priv->has_alt_txcntl) {
376 /* WPA enabled firmwares have tx_cntl at the end of
377 * the 802.11 header. So write zeroed descriptor and
378 * 802.11 header at the same time
380 char desc[HERMES_802_3_OFFSET];
381 __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
383 memset(&desc, 0, sizeof(desc));
385 *txcntl = cpu_to_le16(tx_control);
386 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
387 txfid, 0);
388 if (err) {
389 if (net_ratelimit())
390 printk(KERN_ERR "%s: Error %d writing Tx "
391 "descriptor to BAP\n", dev->name, err);
392 goto busy;
394 } else {
395 struct hermes_tx_descriptor desc;
397 memset(&desc, 0, sizeof(desc));
399 desc.tx_control = cpu_to_le16(tx_control);
400 err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
401 txfid, 0);
402 if (err) {
403 if (net_ratelimit())
404 printk(KERN_ERR "%s: Error %d writing Tx "
405 "descriptor to BAP\n", dev->name, err);
406 goto busy;
409 /* Clear the 802.11 header and data length fields - some
410 * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
411 * if this isn't done. */
412 hermes_clear_words(hw, HERMES_DATA0,
413 HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
416 eh = (struct ethhdr *)skb->data;
418 /* Encapsulate Ethernet-II frames */
419 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
420 struct header_struct {
421 struct ethhdr eth; /* 802.3 header */
422 u8 encap[6]; /* 802.2 header */
423 } __attribute__ ((packed)) hdr;
425 /* Strip destination and source from the data */
426 skb_pull(skb, 2 * ETH_ALEN);
428 /* And move them to a separate header */
429 memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
430 hdr.eth.h_proto = htons(sizeof(encaps_hdr) + skb->len);
431 memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
433 /* Insert the SNAP header */
434 if (skb_headroom(skb) < sizeof(hdr)) {
435 printk(KERN_ERR
436 "%s: Not enough headroom for 802.2 headers %d\n",
437 dev->name, skb_headroom(skb));
438 goto drop;
440 eh = (struct ethhdr *) skb_push(skb, sizeof(hdr));
441 memcpy(eh, &hdr, sizeof(hdr));
444 err = hermes_bap_pwrite(hw, USER_BAP, skb->data, skb->len,
445 txfid, HERMES_802_3_OFFSET);
446 if (err) {
447 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
448 dev->name, err);
449 goto busy;
452 /* Calculate Michael MIC */
453 if (priv->encode_alg == IW_ENCODE_ALG_TKIP) {
454 u8 mic_buf[MICHAEL_MIC_LEN + 1];
455 u8 *mic;
456 size_t offset;
457 size_t len;
459 if (skb->len % 2) {
460 /* MIC start is on an odd boundary */
461 mic_buf[0] = skb->data[skb->len - 1];
462 mic = &mic_buf[1];
463 offset = skb->len - 1;
464 len = MICHAEL_MIC_LEN + 1;
465 } else {
466 mic = &mic_buf[0];
467 offset = skb->len;
468 len = MICHAEL_MIC_LEN;
471 orinoco_mic(priv->tx_tfm_mic,
472 priv->tkip_key[priv->tx_key].tx_mic,
473 eh->h_dest, eh->h_source, 0 /* priority */,
474 skb->data + ETH_HLEN, skb->len - ETH_HLEN, mic);
476 /* Write the MIC */
477 err = hermes_bap_pwrite(hw, USER_BAP, &mic_buf[0], len,
478 txfid, HERMES_802_3_OFFSET + offset);
479 if (err) {
480 printk(KERN_ERR "%s: Error %d writing MIC to BAP\n",
481 dev->name, err);
482 goto busy;
486 /* Finally, we actually initiate the send */
487 netif_stop_queue(dev);
489 err = hermes_docmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
490 txfid, NULL);
491 if (err) {
492 netif_start_queue(dev);
493 if (net_ratelimit())
494 printk(KERN_ERR "%s: Error %d transmitting packet\n",
495 dev->name, err);
496 goto busy;
499 dev->trans_start = jiffies;
500 stats->tx_bytes += HERMES_802_3_OFFSET + skb->len;
501 goto ok;
503 drop:
504 stats->tx_errors++;
505 stats->tx_dropped++;
508 orinoco_unlock(priv, &flags);
509 dev_kfree_skb(skb);
510 return NETDEV_TX_OK;
512 busy:
513 if (err == -EIO)
514 schedule_work(&priv->reset_work);
515 orinoco_unlock(priv, &flags);
516 return NETDEV_TX_BUSY;
519 static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
521 struct orinoco_private *priv = netdev_priv(dev);
522 u16 fid = hermes_read_regn(hw, ALLOCFID);
524 if (fid != priv->txfid) {
525 if (fid != DUMMY_FID)
526 printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
527 dev->name, fid);
528 return;
531 hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
534 static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
536 struct orinoco_private *priv = netdev_priv(dev);
537 struct net_device_stats *stats = &priv->stats;
539 stats->tx_packets++;
541 netif_wake_queue(dev);
543 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
546 static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
548 struct orinoco_private *priv = netdev_priv(dev);
549 struct net_device_stats *stats = &priv->stats;
550 u16 fid = hermes_read_regn(hw, TXCOMPLFID);
551 u16 status;
552 struct hermes_txexc_data hdr;
553 int err = 0;
555 if (fid == DUMMY_FID)
556 return; /* Nothing's really happened */
558 /* Read part of the frame header - we need status and addr1 */
559 err = hermes_bap_pread(hw, IRQ_BAP, &hdr,
560 sizeof(struct hermes_txexc_data),
561 fid, 0);
563 hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
564 stats->tx_errors++;
566 if (err) {
567 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
568 "(FID=%04X error %d)\n",
569 dev->name, fid, err);
570 return;
573 DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
574 err, fid);
576 /* We produce a TXDROP event only for retry or lifetime
577 * exceeded, because that's the only status that really mean
578 * that this particular node went away.
579 * Other errors means that *we* screwed up. - Jean II */
580 status = le16_to_cpu(hdr.desc.status);
581 if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
582 union iwreq_data wrqu;
584 /* Copy 802.11 dest address.
585 * We use the 802.11 header because the frame may
586 * not be 802.3 or may be mangled...
587 * In Ad-Hoc mode, it will be the node address.
588 * In managed mode, it will be most likely the AP addr
589 * User space will figure out how to convert it to
590 * whatever it needs (IP address or else).
591 * - Jean II */
592 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
593 wrqu.addr.sa_family = ARPHRD_ETHER;
595 /* Send event to user space */
596 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
599 netif_wake_queue(dev);
602 static void orinoco_tx_timeout(struct net_device *dev)
604 struct orinoco_private *priv = netdev_priv(dev);
605 struct net_device_stats *stats = &priv->stats;
606 struct hermes *hw = &priv->hw;
608 printk(KERN_WARNING "%s: Tx timeout! "
609 "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
610 dev->name, hermes_read_regn(hw, ALLOCFID),
611 hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
613 stats->tx_errors++;
615 schedule_work(&priv->reset_work);
618 /********************************************************************/
619 /* Rx path (data frames) */
620 /********************************************************************/
622 /* Does the frame have a SNAP header indicating it should be
623 * de-encapsulated to Ethernet-II? */
624 static inline int is_ethersnap(void *_hdr)
626 u8 *hdr = _hdr;
628 /* We de-encapsulate all packets which, a) have SNAP headers
629 * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
630 * and where b) the OUI of the SNAP header is 00:00:00 or
631 * 00:00:f8 - we need both because different APs appear to use
632 * different OUIs for some reason */
633 return (memcmp(hdr, &encaps_hdr, 5) == 0)
634 && ((hdr[5] == 0x00) || (hdr[5] == 0xf8));
637 static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
638 int level, int noise)
640 struct iw_quality wstats;
641 wstats.level = level - 0x95;
642 wstats.noise = noise - 0x95;
643 wstats.qual = (level > noise) ? (level - noise) : 0;
644 wstats.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
645 /* Update spy records */
646 wireless_spy_update(dev, mac, &wstats);
649 static void orinoco_stat_gather(struct net_device *dev,
650 struct sk_buff *skb,
651 struct hermes_rx_descriptor *desc)
653 struct orinoco_private *priv = netdev_priv(dev);
655 /* Using spy support with lots of Rx packets, like in an
656 * infrastructure (AP), will really slow down everything, because
657 * the MAC address must be compared to each entry of the spy list.
658 * If the user really asks for it (set some address in the
659 * spy list), we do it, but he will pay the price.
660 * Note that to get here, you need both WIRELESS_SPY
661 * compiled in AND some addresses in the list !!!
663 /* Note : gcc will optimise the whole section away if
664 * WIRELESS_SPY is not defined... - Jean II */
665 if (SPY_NUMBER(priv)) {
666 orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
667 desc->signal, desc->silence);
672 * orinoco_rx_monitor - handle received monitor frames.
674 * Arguments:
675 * dev network device
676 * rxfid received FID
677 * desc rx descriptor of the frame
679 * Call context: interrupt
681 static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
682 struct hermes_rx_descriptor *desc)
684 u32 hdrlen = 30; /* return full header by default */
685 u32 datalen = 0;
686 u16 fc;
687 int err;
688 int len;
689 struct sk_buff *skb;
690 struct orinoco_private *priv = netdev_priv(dev);
691 struct net_device_stats *stats = &priv->stats;
692 hermes_t *hw = &priv->hw;
694 len = le16_to_cpu(desc->data_len);
696 /* Determine the size of the header and the data */
697 fc = le16_to_cpu(desc->frame_ctl);
698 switch (fc & IEEE80211_FCTL_FTYPE) {
699 case IEEE80211_FTYPE_DATA:
700 if ((fc & IEEE80211_FCTL_TODS)
701 && (fc & IEEE80211_FCTL_FROMDS))
702 hdrlen = 30;
703 else
704 hdrlen = 24;
705 datalen = len;
706 break;
707 case IEEE80211_FTYPE_MGMT:
708 hdrlen = 24;
709 datalen = len;
710 break;
711 case IEEE80211_FTYPE_CTL:
712 switch (fc & IEEE80211_FCTL_STYPE) {
713 case IEEE80211_STYPE_PSPOLL:
714 case IEEE80211_STYPE_RTS:
715 case IEEE80211_STYPE_CFEND:
716 case IEEE80211_STYPE_CFENDACK:
717 hdrlen = 16;
718 break;
719 case IEEE80211_STYPE_CTS:
720 case IEEE80211_STYPE_ACK:
721 hdrlen = 10;
722 break;
724 break;
725 default:
726 /* Unknown frame type */
727 break;
730 /* sanity check the length */
731 if (datalen > IEEE80211_MAX_DATA_LEN + 12) {
732 printk(KERN_DEBUG "%s: oversized monitor frame, "
733 "data length = %d\n", dev->name, datalen);
734 err = -EIO;
735 stats->rx_length_errors++;
736 goto update_stats;
739 skb = dev_alloc_skb(hdrlen + datalen);
740 if (!skb) {
741 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
742 dev->name);
743 err = -ENOMEM;
744 goto drop;
747 /* Copy the 802.11 header to the skb */
748 memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
749 skb_reset_mac_header(skb);
751 /* If any, copy the data from the card to the skb */
752 if (datalen > 0) {
753 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
754 ALIGN(datalen, 2), rxfid,
755 HERMES_802_2_OFFSET);
756 if (err) {
757 printk(KERN_ERR "%s: error %d reading monitor frame\n",
758 dev->name, err);
759 goto drop;
763 skb->dev = dev;
764 skb->ip_summed = CHECKSUM_NONE;
765 skb->pkt_type = PACKET_OTHERHOST;
766 skb->protocol = cpu_to_be16(ETH_P_802_2);
768 stats->rx_packets++;
769 stats->rx_bytes += skb->len;
771 netif_rx(skb);
772 return;
774 drop:
775 dev_kfree_skb_irq(skb);
776 update_stats:
777 stats->rx_errors++;
778 stats->rx_dropped++;
781 static void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
783 struct orinoco_private *priv = netdev_priv(dev);
784 struct net_device_stats *stats = &priv->stats;
785 struct iw_statistics *wstats = &priv->wstats;
786 struct sk_buff *skb = NULL;
787 u16 rxfid, status;
788 int length;
789 struct hermes_rx_descriptor *desc;
790 struct orinoco_rx_data *rx_data;
791 int err;
793 desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
794 if (!desc) {
795 printk(KERN_WARNING
796 "%s: Can't allocate space for RX descriptor\n",
797 dev->name);
798 goto update_stats;
801 rxfid = hermes_read_regn(hw, RXFID);
803 err = hermes_bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
804 rxfid, 0);
805 if (err) {
806 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
807 "Frame dropped.\n", dev->name, err);
808 goto update_stats;
811 status = le16_to_cpu(desc->status);
813 if (status & HERMES_RXSTAT_BADCRC) {
814 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
815 dev->name);
816 stats->rx_crc_errors++;
817 goto update_stats;
820 /* Handle frames in monitor mode */
821 if (priv->iw_mode == IW_MODE_MONITOR) {
822 orinoco_rx_monitor(dev, rxfid, desc);
823 goto out;
826 if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
827 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
828 dev->name);
829 wstats->discard.code++;
830 goto update_stats;
833 length = le16_to_cpu(desc->data_len);
835 /* Sanity checks */
836 if (length < 3) { /* No for even an 802.2 LLC header */
837 /* At least on Symbol firmware with PCF we get quite a
838 lot of these legitimately - Poll frames with no
839 data. */
840 goto out;
842 if (length > IEEE80211_MAX_DATA_LEN) {
843 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
844 dev->name, length);
845 stats->rx_length_errors++;
846 goto update_stats;
849 /* Payload size does not include Michael MIC. Increase payload
850 * size to read it together with the data. */
851 if (status & HERMES_RXSTAT_MIC)
852 length += MICHAEL_MIC_LEN;
854 /* We need space for the packet data itself, plus an ethernet
855 header, plus 2 bytes so we can align the IP header on a
856 32bit boundary, plus 1 byte so we can read in odd length
857 packets from the card, which has an IO granularity of 16
858 bits */
859 skb = dev_alloc_skb(length+ETH_HLEN+2+1);
860 if (!skb) {
861 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
862 dev->name);
863 goto update_stats;
866 /* We'll prepend the header, so reserve space for it. The worst
867 case is no decapsulation, when 802.3 header is prepended and
868 nothing is removed. 2 is for aligning the IP header. */
869 skb_reserve(skb, ETH_HLEN + 2);
871 err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, length),
872 ALIGN(length, 2), rxfid,
873 HERMES_802_2_OFFSET);
874 if (err) {
875 printk(KERN_ERR "%s: error %d reading frame. "
876 "Frame dropped.\n", dev->name, err);
877 goto drop;
880 /* Add desc and skb to rx queue */
881 rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
882 if (!rx_data) {
883 printk(KERN_WARNING "%s: Can't allocate RX packet\n",
884 dev->name);
885 goto drop;
887 rx_data->desc = desc;
888 rx_data->skb = skb;
889 list_add_tail(&rx_data->list, &priv->rx_list);
890 tasklet_schedule(&priv->rx_tasklet);
892 return;
894 drop:
895 dev_kfree_skb_irq(skb);
896 update_stats:
897 stats->rx_errors++;
898 stats->rx_dropped++;
899 out:
900 kfree(desc);
903 static void orinoco_rx(struct net_device *dev,
904 struct hermes_rx_descriptor *desc,
905 struct sk_buff *skb)
907 struct orinoco_private *priv = netdev_priv(dev);
908 struct net_device_stats *stats = &priv->stats;
909 u16 status, fc;
910 int length;
911 struct ethhdr *hdr;
913 status = le16_to_cpu(desc->status);
914 length = le16_to_cpu(desc->data_len);
915 fc = le16_to_cpu(desc->frame_ctl);
917 /* Calculate and check MIC */
918 if (status & HERMES_RXSTAT_MIC) {
919 int key_id = ((status & HERMES_RXSTAT_MIC_KEY_ID) >>
920 HERMES_MIC_KEY_ID_SHIFT);
921 u8 mic[MICHAEL_MIC_LEN];
922 u8 *rxmic;
923 u8 *src = (fc & IEEE80211_FCTL_FROMDS) ?
924 desc->addr3 : desc->addr2;
926 /* Extract Michael MIC from payload */
927 rxmic = skb->data + skb->len - MICHAEL_MIC_LEN;
929 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
930 length -= MICHAEL_MIC_LEN;
932 orinoco_mic(priv->rx_tfm_mic,
933 priv->tkip_key[key_id].rx_mic,
934 desc->addr1,
935 src,
936 0, /* priority or QoS? */
937 skb->data,
938 skb->len,
939 &mic[0]);
941 if (memcmp(mic, rxmic,
942 MICHAEL_MIC_LEN)) {
943 union iwreq_data wrqu;
944 struct iw_michaelmicfailure wxmic;
946 printk(KERN_WARNING "%s: "
947 "Invalid Michael MIC in data frame from %pM, "
948 "using key %i\n",
949 dev->name, src, key_id);
951 /* TODO: update stats */
953 /* Notify userspace */
954 memset(&wxmic, 0, sizeof(wxmic));
955 wxmic.flags = key_id & IW_MICFAILURE_KEY_ID;
956 wxmic.flags |= (desc->addr1[0] & 1) ?
957 IW_MICFAILURE_GROUP : IW_MICFAILURE_PAIRWISE;
958 wxmic.src_addr.sa_family = ARPHRD_ETHER;
959 memcpy(wxmic.src_addr.sa_data, src, ETH_ALEN);
961 (void) orinoco_hw_get_tkip_iv(priv, key_id,
962 &wxmic.tsc[0]);
964 memset(&wrqu, 0, sizeof(wrqu));
965 wrqu.data.length = sizeof(wxmic);
966 wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu,
967 (char *) &wxmic);
969 goto drop;
973 /* Handle decapsulation
974 * In most cases, the firmware tell us about SNAP frames.
975 * For some reason, the SNAP frames sent by LinkSys APs
976 * are not properly recognised by most firmwares.
977 * So, check ourselves */
978 if (length >= ENCAPS_OVERHEAD &&
979 (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
980 ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
981 is_ethersnap(skb->data))) {
982 /* These indicate a SNAP within 802.2 LLC within
983 802.11 frame which we'll need to de-encapsulate to
984 the original EthernetII frame. */
985 hdr = (struct ethhdr *)skb_push(skb,
986 ETH_HLEN - ENCAPS_OVERHEAD);
987 } else {
988 /* 802.3 frame - prepend 802.3 header as is */
989 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
990 hdr->h_proto = htons(length);
992 memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
993 if (fc & IEEE80211_FCTL_FROMDS)
994 memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
995 else
996 memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
998 skb->protocol = eth_type_trans(skb, dev);
999 skb->ip_summed = CHECKSUM_NONE;
1000 if (fc & IEEE80211_FCTL_TODS)
1001 skb->pkt_type = PACKET_OTHERHOST;
1003 /* Process the wireless stats if needed */
1004 orinoco_stat_gather(dev, skb, desc);
1006 /* Pass the packet to the networking stack */
1007 netif_rx(skb);
1008 stats->rx_packets++;
1009 stats->rx_bytes += length;
1011 return;
1013 drop:
1014 dev_kfree_skb(skb);
1015 stats->rx_errors++;
1016 stats->rx_dropped++;
1019 static void orinoco_rx_isr_tasklet(unsigned long data)
1021 struct net_device *dev = (struct net_device *) data;
1022 struct orinoco_private *priv = netdev_priv(dev);
1023 struct orinoco_rx_data *rx_data, *temp;
1024 struct hermes_rx_descriptor *desc;
1025 struct sk_buff *skb;
1026 unsigned long flags;
1028 /* orinoco_rx requires the driver lock, and we also need to
1029 * protect priv->rx_list, so just hold the lock over the
1030 * lot.
1032 * If orinoco_lock fails, we've unplugged the card. In this
1033 * case just abort. */
1034 if (orinoco_lock(priv, &flags) != 0)
1035 return;
1037 /* extract desc and skb from queue */
1038 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
1039 desc = rx_data->desc;
1040 skb = rx_data->skb;
1041 list_del(&rx_data->list);
1042 kfree(rx_data);
1044 orinoco_rx(dev, desc, skb);
1046 kfree(desc);
1049 orinoco_unlock(priv, &flags);
1052 /********************************************************************/
1053 /* Rx path (info frames) */
1054 /********************************************************************/
1056 static void print_linkstatus(struct net_device *dev, u16 status)
1058 char *s;
1060 if (suppress_linkstatus)
1061 return;
1063 switch (status) {
1064 case HERMES_LINKSTATUS_NOT_CONNECTED:
1065 s = "Not Connected";
1066 break;
1067 case HERMES_LINKSTATUS_CONNECTED:
1068 s = "Connected";
1069 break;
1070 case HERMES_LINKSTATUS_DISCONNECTED:
1071 s = "Disconnected";
1072 break;
1073 case HERMES_LINKSTATUS_AP_CHANGE:
1074 s = "AP Changed";
1075 break;
1076 case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1077 s = "AP Out of Range";
1078 break;
1079 case HERMES_LINKSTATUS_AP_IN_RANGE:
1080 s = "AP In Range";
1081 break;
1082 case HERMES_LINKSTATUS_ASSOC_FAILED:
1083 s = "Association Failed";
1084 break;
1085 default:
1086 s = "UNKNOWN";
1089 printk(KERN_DEBUG "%s: New link status: %s (%04x)\n",
1090 dev->name, s, status);
1093 /* Search scan results for requested BSSID, join it if found */
1094 static void orinoco_join_ap(struct work_struct *work)
1096 struct orinoco_private *priv =
1097 container_of(work, struct orinoco_private, join_work);
1098 struct net_device *dev = priv->ndev;
1099 struct hermes *hw = &priv->hw;
1100 int err;
1101 unsigned long flags;
1102 struct join_req {
1103 u8 bssid[ETH_ALEN];
1104 __le16 channel;
1105 } __attribute__ ((packed)) req;
1106 const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
1107 struct prism2_scan_apinfo *atom = NULL;
1108 int offset = 4;
1109 int found = 0;
1110 u8 *buf;
1111 u16 len;
1113 /* Allocate buffer for scan results */
1114 buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
1115 if (!buf)
1116 return;
1118 if (orinoco_lock(priv, &flags) != 0)
1119 goto fail_lock;
1121 /* Sanity checks in case user changed something in the meantime */
1122 if (!priv->bssid_fixed)
1123 goto out;
1125 if (strlen(priv->desired_essid) == 0)
1126 goto out;
1128 /* Read scan results from the firmware */
1129 err = hermes_read_ltv(hw, USER_BAP,
1130 HERMES_RID_SCANRESULTSTABLE,
1131 MAX_SCAN_LEN, &len, buf);
1132 if (err) {
1133 printk(KERN_ERR "%s: Cannot read scan results\n",
1134 dev->name);
1135 goto out;
1138 len = HERMES_RECLEN_TO_BYTES(len);
1140 /* Go through the scan results looking for the channel of the AP
1141 * we were requested to join */
1142 for (; offset + atom_len <= len; offset += atom_len) {
1143 atom = (struct prism2_scan_apinfo *) (buf + offset);
1144 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1145 found = 1;
1146 break;
1150 if (!found) {
1151 DEBUG(1, "%s: Requested AP not found in scan results\n",
1152 dev->name);
1153 goto out;
1156 memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1157 req.channel = atom->channel; /* both are little-endian */
1158 err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1159 &req);
1160 if (err)
1161 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1163 out:
1164 orinoco_unlock(priv, &flags);
1166 fail_lock:
1167 kfree(buf);
1170 /* Send new BSSID to userspace */
1171 static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
1173 struct net_device *dev = priv->ndev;
1174 struct hermes *hw = &priv->hw;
1175 union iwreq_data wrqu;
1176 int err;
1178 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
1179 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1180 if (err != 0)
1181 return;
1183 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1185 /* Send event to user space */
1186 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
1189 static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
1191 struct net_device *dev = priv->ndev;
1192 struct hermes *hw = &priv->hw;
1193 union iwreq_data wrqu;
1194 int err;
1195 u8 buf[88];
1196 u8 *ie;
1198 if (!priv->has_wpa)
1199 return;
1201 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
1202 sizeof(buf), NULL, &buf);
1203 if (err != 0)
1204 return;
1206 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1207 if (ie) {
1208 int rem = sizeof(buf) - (ie - &buf[0]);
1209 wrqu.data.length = ie[1] + 2;
1210 if (wrqu.data.length > rem)
1211 wrqu.data.length = rem;
1213 if (wrqu.data.length)
1214 /* Send event to user space */
1215 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
1219 static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
1221 struct net_device *dev = priv->ndev;
1222 struct hermes *hw = &priv->hw;
1223 union iwreq_data wrqu;
1224 int err;
1225 u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
1226 u8 *ie;
1228 if (!priv->has_wpa)
1229 return;
1231 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_RESP_INFO,
1232 sizeof(buf), NULL, &buf);
1233 if (err != 0)
1234 return;
1236 ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1237 if (ie) {
1238 int rem = sizeof(buf) - (ie - &buf[0]);
1239 wrqu.data.length = ie[1] + 2;
1240 if (wrqu.data.length > rem)
1241 wrqu.data.length = rem;
1243 if (wrqu.data.length)
1244 /* Send event to user space */
1245 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
1249 static void orinoco_send_wevents(struct work_struct *work)
1251 struct orinoco_private *priv =
1252 container_of(work, struct orinoco_private, wevent_work);
1253 unsigned long flags;
1255 if (orinoco_lock(priv, &flags) != 0)
1256 return;
1258 orinoco_send_assocreqie_wevent(priv);
1259 orinoco_send_assocrespie_wevent(priv);
1260 orinoco_send_bssid_wevent(priv);
1262 orinoco_unlock(priv, &flags);
1265 static void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
1267 struct orinoco_private *priv = netdev_priv(dev);
1268 u16 infofid;
1269 struct {
1270 __le16 len;
1271 __le16 type;
1272 } __attribute__ ((packed)) info;
1273 int len, type;
1274 int err;
1276 /* This is an answer to an INQUIRE command that we did earlier,
1277 * or an information "event" generated by the card
1278 * The controller return to us a pseudo frame containing
1279 * the information in question - Jean II */
1280 infofid = hermes_read_regn(hw, INFOFID);
1282 /* Read the info frame header - don't try too hard */
1283 err = hermes_bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1284 infofid, 0);
1285 if (err) {
1286 printk(KERN_ERR "%s: error %d reading info frame. "
1287 "Frame dropped.\n", dev->name, err);
1288 return;
1291 len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1292 type = le16_to_cpu(info.type);
1294 switch (type) {
1295 case HERMES_INQ_TALLIES: {
1296 struct hermes_tallies_frame tallies;
1297 struct iw_statistics *wstats = &priv->wstats;
1299 if (len > sizeof(tallies)) {
1300 printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1301 dev->name, len);
1302 len = sizeof(tallies);
1305 err = hermes_bap_pread(hw, IRQ_BAP, &tallies, len,
1306 infofid, sizeof(info));
1307 if (err)
1308 break;
1310 /* Increment our various counters */
1311 /* wstats->discard.nwid - no wrong BSSID stuff */
1312 wstats->discard.code +=
1313 le16_to_cpu(tallies.RxWEPUndecryptable);
1314 if (len == sizeof(tallies))
1315 wstats->discard.code +=
1316 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1317 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1318 wstats->discard.misc +=
1319 le16_to_cpu(tallies.TxDiscardsWrongSA);
1320 wstats->discard.fragment +=
1321 le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1322 wstats->discard.retries +=
1323 le16_to_cpu(tallies.TxRetryLimitExceeded);
1324 /* wstats->miss.beacon - no match */
1326 break;
1327 case HERMES_INQ_LINKSTATUS: {
1328 struct hermes_linkstatus linkstatus;
1329 u16 newstatus;
1330 int connected;
1332 if (priv->iw_mode == IW_MODE_MONITOR)
1333 break;
1335 if (len != sizeof(linkstatus)) {
1336 printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1337 dev->name, len);
1338 break;
1341 err = hermes_bap_pread(hw, IRQ_BAP, &linkstatus, len,
1342 infofid, sizeof(info));
1343 if (err)
1344 break;
1345 newstatus = le16_to_cpu(linkstatus.linkstatus);
1347 /* Symbol firmware uses "out of range" to signal that
1348 * the hostscan frame can be requested. */
1349 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
1350 priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
1351 priv->has_hostscan && priv->scan_inprogress) {
1352 hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
1353 break;
1356 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
1357 || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1358 || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
1360 if (connected)
1361 netif_carrier_on(dev);
1362 else if (!ignore_disconnect)
1363 netif_carrier_off(dev);
1365 if (newstatus != priv->last_linkstatus) {
1366 priv->last_linkstatus = newstatus;
1367 print_linkstatus(dev, newstatus);
1368 /* The info frame contains only one word which is the
1369 * status (see hermes.h). The status is pretty boring
1370 * in itself, that's why we export the new BSSID...
1371 * Jean II */
1372 schedule_work(&priv->wevent_work);
1375 break;
1376 case HERMES_INQ_SCAN:
1377 if (!priv->scan_inprogress && priv->bssid_fixed &&
1378 priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
1379 schedule_work(&priv->join_work);
1380 break;
1382 /* fall through */
1383 case HERMES_INQ_HOSTSCAN:
1384 case HERMES_INQ_HOSTSCAN_SYMBOL: {
1385 /* Result of a scanning. Contains information about
1386 * cells in the vicinity - Jean II */
1387 union iwreq_data wrqu;
1388 unsigned char *buf;
1390 /* Scan is no longer in progress */
1391 priv->scan_inprogress = 0;
1393 /* Sanity check */
1394 if (len > 4096) {
1395 printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
1396 dev->name, len);
1397 break;
1400 /* Allocate buffer for results */
1401 buf = kmalloc(len, GFP_ATOMIC);
1402 if (buf == NULL)
1403 /* No memory, so can't printk()... */
1404 break;
1406 /* Read scan data */
1407 err = hermes_bap_pread(hw, IRQ_BAP, (void *) buf, len,
1408 infofid, sizeof(info));
1409 if (err) {
1410 kfree(buf);
1411 break;
1414 #ifdef ORINOCO_DEBUG
1416 int i;
1417 printk(KERN_DEBUG "Scan result [%02X", buf[0]);
1418 for (i = 1; i < (len * 2); i++)
1419 printk(":%02X", buf[i]);
1420 printk("]\n");
1422 #endif /* ORINOCO_DEBUG */
1424 if (orinoco_process_scan_results(priv, buf, len) == 0) {
1425 /* Send an empty event to user space.
1426 * We don't send the received data on the event because
1427 * it would require us to do complex transcoding, and
1428 * we want to minimise the work done in the irq handler
1429 * Use a request to extract the data - Jean II */
1430 wrqu.data.length = 0;
1431 wrqu.data.flags = 0;
1432 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
1434 kfree(buf);
1436 break;
1437 case HERMES_INQ_CHANNELINFO:
1439 struct agere_ext_scan_info *bss;
1441 if (!priv->scan_inprogress) {
1442 printk(KERN_DEBUG "%s: Got chaninfo without scan, "
1443 "len=%d\n", dev->name, len);
1444 break;
1447 /* An empty result indicates that the scan is complete */
1448 if (len == 0) {
1449 union iwreq_data wrqu;
1451 /* Scan is no longer in progress */
1452 priv->scan_inprogress = 0;
1454 wrqu.data.length = 0;
1455 wrqu.data.flags = 0;
1456 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
1457 break;
1460 /* Sanity check */
1461 else if (len > sizeof(*bss)) {
1462 printk(KERN_WARNING
1463 "%s: Ext scan results too large (%d bytes). "
1464 "Truncating results to %zd bytes.\n",
1465 dev->name, len, sizeof(*bss));
1466 len = sizeof(*bss);
1467 } else if (len < (offsetof(struct agere_ext_scan_info,
1468 data) + 2)) {
1469 /* Drop this result now so we don't have to
1470 * keep checking later */
1471 printk(KERN_WARNING
1472 "%s: Ext scan results too short (%d bytes)\n",
1473 dev->name, len);
1474 break;
1477 bss = kmalloc(sizeof(*bss), GFP_ATOMIC);
1478 if (bss == NULL)
1479 break;
1481 /* Read scan data */
1482 err = hermes_bap_pread(hw, IRQ_BAP, (void *) bss, len,
1483 infofid, sizeof(info));
1484 if (err) {
1485 kfree(bss);
1486 break;
1489 orinoco_add_ext_scan_result(priv, bss);
1491 kfree(bss);
1492 break;
1494 case HERMES_INQ_SEC_STAT_AGERE:
1495 /* Security status (Agere specific) */
1496 /* Ignore this frame for now */
1497 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
1498 break;
1499 /* fall through */
1500 default:
1501 printk(KERN_DEBUG "%s: Unknown information frame received: "
1502 "type 0x%04x, length %d\n", dev->name, type, len);
1503 /* We don't actually do anything about it */
1504 break;
1508 static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
1510 if (net_ratelimit())
1511 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
1514 /********************************************************************/
1515 /* Internal hardware control routines */
1516 /********************************************************************/
1518 int __orinoco_up(struct net_device *dev)
1520 struct orinoco_private *priv = netdev_priv(dev);
1521 struct hermes *hw = &priv->hw;
1522 int err;
1524 netif_carrier_off(dev); /* just to make sure */
1526 err = __orinoco_program_rids(dev);
1527 if (err) {
1528 printk(KERN_ERR "%s: Error %d configuring card\n",
1529 dev->name, err);
1530 return err;
1533 /* Fire things up again */
1534 hermes_set_irqmask(hw, ORINOCO_INTEN);
1535 err = hermes_enable_port(hw, 0);
1536 if (err) {
1537 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
1538 dev->name, err);
1539 return err;
1542 netif_start_queue(dev);
1544 return 0;
1546 EXPORT_SYMBOL(__orinoco_up);
1548 int __orinoco_down(struct net_device *dev)
1550 struct orinoco_private *priv = netdev_priv(dev);
1551 struct hermes *hw = &priv->hw;
1552 int err;
1554 netif_stop_queue(dev);
1556 if (!priv->hw_unavailable) {
1557 if (!priv->broken_disableport) {
1558 err = hermes_disable_port(hw, 0);
1559 if (err) {
1560 /* Some firmwares (e.g. Intersil 1.3.x) seem
1561 * to have problems disabling the port, oh
1562 * well, too bad. */
1563 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
1564 dev->name, err);
1565 priv->broken_disableport = 1;
1568 hermes_set_irqmask(hw, 0);
1569 hermes_write_regn(hw, EVACK, 0xffff);
1572 /* firmware will have to reassociate */
1573 netif_carrier_off(dev);
1574 priv->last_linkstatus = 0xffff;
1576 return 0;
1578 EXPORT_SYMBOL(__orinoco_down);
1580 static int orinoco_allocate_fid(struct net_device *dev)
1582 struct orinoco_private *priv = netdev_priv(dev);
1583 struct hermes *hw = &priv->hw;
1584 int err;
1586 err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
1587 if (err == -EIO && priv->nicbuf_size > TX_NICBUF_SIZE_BUG) {
1588 /* Try workaround for old Symbol firmware bug */
1589 priv->nicbuf_size = TX_NICBUF_SIZE_BUG;
1590 err = hermes_allocate(hw, priv->nicbuf_size, &priv->txfid);
1592 printk(KERN_WARNING "%s: firmware ALLOC bug detected "
1593 "(old Symbol firmware?). Work around %s\n",
1594 dev->name, err ? "failed!" : "ok.");
1597 return err;
1600 int orinoco_reinit_firmware(struct net_device *dev)
1602 struct orinoco_private *priv = netdev_priv(dev);
1603 struct hermes *hw = &priv->hw;
1604 int err;
1606 err = hermes_init(hw);
1607 if (priv->do_fw_download && !err) {
1608 err = orinoco_download(priv);
1609 if (err)
1610 priv->do_fw_download = 0;
1612 if (!err)
1613 err = orinoco_allocate_fid(dev);
1615 return err;
1617 EXPORT_SYMBOL(orinoco_reinit_firmware);
1619 int __orinoco_program_rids(struct net_device *dev)
1621 struct orinoco_private *priv = netdev_priv(dev);
1622 hermes_t *hw = &priv->hw;
1623 int err;
1624 struct hermes_idstring idbuf;
1626 /* Set the MAC address */
1627 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
1628 HERMES_BYTES_TO_RECLEN(ETH_ALEN), dev->dev_addr);
1629 if (err) {
1630 printk(KERN_ERR "%s: Error %d setting MAC address\n",
1631 dev->name, err);
1632 return err;
1635 /* Set up the link mode */
1636 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFPORTTYPE,
1637 priv->port_type);
1638 if (err) {
1639 printk(KERN_ERR "%s: Error %d setting port type\n",
1640 dev->name, err);
1641 return err;
1643 /* Set the channel/frequency */
1644 if (priv->channel != 0 && priv->iw_mode != IW_MODE_INFRA) {
1645 err = hermes_write_wordrec(hw, USER_BAP,
1646 HERMES_RID_CNFOWNCHANNEL,
1647 priv->channel);
1648 if (err) {
1649 printk(KERN_ERR "%s: Error %d setting channel %d\n",
1650 dev->name, err, priv->channel);
1651 return err;
1655 if (priv->has_ibss) {
1656 u16 createibss;
1658 if ((strlen(priv->desired_essid) == 0) && (priv->createibss)) {
1659 printk(KERN_WARNING "%s: This firmware requires an "
1660 "ESSID in IBSS-Ad-Hoc mode.\n", dev->name);
1661 /* With wvlan_cs, in this case, we would crash.
1662 * hopefully, this driver will behave better...
1663 * Jean II */
1664 createibss = 0;
1665 } else {
1666 createibss = priv->createibss;
1669 err = hermes_write_wordrec(hw, USER_BAP,
1670 HERMES_RID_CNFCREATEIBSS,
1671 createibss);
1672 if (err) {
1673 printk(KERN_ERR "%s: Error %d setting CREATEIBSS\n",
1674 dev->name, err);
1675 return err;
1679 /* Set the desired BSSID */
1680 err = __orinoco_hw_set_wap(priv);
1681 if (err) {
1682 printk(KERN_ERR "%s: Error %d setting AP address\n",
1683 dev->name, err);
1684 return err;
1686 /* Set the desired ESSID */
1687 idbuf.len = cpu_to_le16(strlen(priv->desired_essid));
1688 memcpy(&idbuf.val, priv->desired_essid, sizeof(idbuf.val));
1689 /* WinXP wants partner to configure OWNSSID even in IBSS mode. (jimc) */
1690 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNSSID,
1691 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
1692 &idbuf);
1693 if (err) {
1694 printk(KERN_ERR "%s: Error %d setting OWNSSID\n",
1695 dev->name, err);
1696 return err;
1698 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFDESIREDSSID,
1699 HERMES_BYTES_TO_RECLEN(strlen(priv->desired_essid)+2),
1700 &idbuf);
1701 if (err) {
1702 printk(KERN_ERR "%s: Error %d setting DESIREDSSID\n",
1703 dev->name, err);
1704 return err;
1707 /* Set the station name */
1708 idbuf.len = cpu_to_le16(strlen(priv->nick));
1709 memcpy(&idbuf.val, priv->nick, sizeof(idbuf.val));
1710 err = hermes_write_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
1711 HERMES_BYTES_TO_RECLEN(strlen(priv->nick)+2),
1712 &idbuf);
1713 if (err) {
1714 printk(KERN_ERR "%s: Error %d setting nickname\n",
1715 dev->name, err);
1716 return err;
1719 /* Set AP density */
1720 if (priv->has_sensitivity) {
1721 err = hermes_write_wordrec(hw, USER_BAP,
1722 HERMES_RID_CNFSYSTEMSCALE,
1723 priv->ap_density);
1724 if (err) {
1725 printk(KERN_WARNING "%s: Error %d setting SYSTEMSCALE. "
1726 "Disabling sensitivity control\n",
1727 dev->name, err);
1729 priv->has_sensitivity = 0;
1733 /* Set RTS threshold */
1734 err = hermes_write_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
1735 priv->rts_thresh);
1736 if (err) {
1737 printk(KERN_ERR "%s: Error %d setting RTS threshold\n",
1738 dev->name, err);
1739 return err;
1742 /* Set fragmentation threshold or MWO robustness */
1743 if (priv->has_mwo)
1744 err = hermes_write_wordrec(hw, USER_BAP,
1745 HERMES_RID_CNFMWOROBUST_AGERE,
1746 priv->mwo_robust);
1747 else
1748 err = hermes_write_wordrec(hw, USER_BAP,
1749 HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
1750 priv->frag_thresh);
1751 if (err) {
1752 printk(KERN_ERR "%s: Error %d setting fragmentation\n",
1753 dev->name, err);
1754 return err;
1757 /* Set bitrate */
1758 err = __orinoco_hw_set_bitrate(priv);
1759 if (err) {
1760 printk(KERN_ERR "%s: Error %d setting bitrate\n",
1761 dev->name, err);
1762 return err;
1765 /* Set power management */
1766 if (priv->has_pm) {
1767 err = hermes_write_wordrec(hw, USER_BAP,
1768 HERMES_RID_CNFPMENABLED,
1769 priv->pm_on);
1770 if (err) {
1771 printk(KERN_ERR "%s: Error %d setting up PM\n",
1772 dev->name, err);
1773 return err;
1776 err = hermes_write_wordrec(hw, USER_BAP,
1777 HERMES_RID_CNFMULTICASTRECEIVE,
1778 priv->pm_mcast);
1779 if (err) {
1780 printk(KERN_ERR "%s: Error %d setting up PM\n",
1781 dev->name, err);
1782 return err;
1784 err = hermes_write_wordrec(hw, USER_BAP,
1785 HERMES_RID_CNFMAXSLEEPDURATION,
1786 priv->pm_period);
1787 if (err) {
1788 printk(KERN_ERR "%s: Error %d setting up PM\n",
1789 dev->name, err);
1790 return err;
1792 err = hermes_write_wordrec(hw, USER_BAP,
1793 HERMES_RID_CNFPMHOLDOVERDURATION,
1794 priv->pm_timeout);
1795 if (err) {
1796 printk(KERN_ERR "%s: Error %d setting up PM\n",
1797 dev->name, err);
1798 return err;
1802 /* Set preamble - only for Symbol so far... */
1803 if (priv->has_preamble) {
1804 err = hermes_write_wordrec(hw, USER_BAP,
1805 HERMES_RID_CNFPREAMBLE_SYMBOL,
1806 priv->preamble);
1807 if (err) {
1808 printk(KERN_ERR "%s: Error %d setting preamble\n",
1809 dev->name, err);
1810 return err;
1814 /* Set up encryption */
1815 if (priv->has_wep || priv->has_wpa) {
1816 err = __orinoco_hw_setup_enc(priv);
1817 if (err) {
1818 printk(KERN_ERR "%s: Error %d activating encryption\n",
1819 dev->name, err);
1820 return err;
1824 if (priv->iw_mode == IW_MODE_MONITOR) {
1825 /* Enable monitor mode */
1826 dev->type = ARPHRD_IEEE80211;
1827 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
1828 HERMES_TEST_MONITOR, 0, NULL);
1829 } else {
1830 /* Disable monitor mode */
1831 dev->type = ARPHRD_ETHER;
1832 err = hermes_docmd_wait(hw, HERMES_CMD_TEST |
1833 HERMES_TEST_STOP, 0, NULL);
1835 if (err)
1836 return err;
1838 /* Set promiscuity / multicast*/
1839 priv->promiscuous = 0;
1840 priv->mc_count = 0;
1842 /* FIXME: what about netif_tx_lock */
1843 __orinoco_set_multicast_list(dev);
1845 return 0;
1848 /* FIXME: return int? */
1849 static void
1850 __orinoco_set_multicast_list(struct net_device *dev)
1852 struct orinoco_private *priv = netdev_priv(dev);
1853 int err = 0;
1854 int promisc, mc_count;
1856 /* The Hermes doesn't seem to have an allmulti mode, so we go
1857 * into promiscuous mode and let the upper levels deal. */
1858 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
1859 (dev->mc_count > MAX_MULTICAST(priv))) {
1860 promisc = 1;
1861 mc_count = 0;
1862 } else {
1863 promisc = 0;
1864 mc_count = dev->mc_count;
1867 err = __orinoco_hw_set_multicast_list(priv, dev->mc_list, mc_count,
1868 promisc);
1871 /* This must be called from user context, without locks held - use
1872 * schedule_work() */
1873 void orinoco_reset(struct work_struct *work)
1875 struct orinoco_private *priv =
1876 container_of(work, struct orinoco_private, reset_work);
1877 struct net_device *dev = priv->ndev;
1878 struct hermes *hw = &priv->hw;
1879 int err;
1880 unsigned long flags;
1882 if (orinoco_lock(priv, &flags) != 0)
1883 /* When the hardware becomes available again, whatever
1884 * detects that is responsible for re-initializing
1885 * it. So no need for anything further */
1886 return;
1888 netif_stop_queue(dev);
1890 /* Shut off interrupts. Depending on what state the hardware
1891 * is in, this might not work, but we'll try anyway */
1892 hermes_set_irqmask(hw, 0);
1893 hermes_write_regn(hw, EVACK, 0xffff);
1895 priv->hw_unavailable++;
1896 priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
1897 netif_carrier_off(dev);
1899 orinoco_unlock(priv, &flags);
1901 /* Scanning support: Cleanup of driver struct */
1902 orinoco_clear_scan_results(priv, 0);
1903 priv->scan_inprogress = 0;
1905 if (priv->hard_reset) {
1906 err = (*priv->hard_reset)(priv);
1907 if (err) {
1908 printk(KERN_ERR "%s: orinoco_reset: Error %d "
1909 "performing hard reset\n", dev->name, err);
1910 goto disable;
1914 err = orinoco_reinit_firmware(dev);
1915 if (err) {
1916 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
1917 dev->name, err);
1918 goto disable;
1921 /* This has to be called from user context */
1922 spin_lock_irq(&priv->lock);
1924 priv->hw_unavailable--;
1926 /* priv->open or priv->hw_unavailable might have changed while
1927 * we dropped the lock */
1928 if (priv->open && (!priv->hw_unavailable)) {
1929 err = __orinoco_up(dev);
1930 if (err) {
1931 printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
1932 dev->name, err);
1933 } else
1934 dev->trans_start = jiffies;
1937 spin_unlock_irq(&priv->lock);
1939 return;
1940 disable:
1941 hermes_set_irqmask(hw, 0);
1942 netif_device_detach(dev);
1943 printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
1946 /********************************************************************/
1947 /* Interrupt handler */
1948 /********************************************************************/
1950 static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
1952 printk(KERN_DEBUG "%s: TICK\n", dev->name);
1955 static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
1957 /* This seems to happen a fair bit under load, but ignoring it
1958 seems to work fine...*/
1959 printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
1960 dev->name);
1963 irqreturn_t orinoco_interrupt(int irq, void *dev_id)
1965 struct net_device *dev = dev_id;
1966 struct orinoco_private *priv = netdev_priv(dev);
1967 hermes_t *hw = &priv->hw;
1968 int count = MAX_IRQLOOPS_PER_IRQ;
1969 u16 evstat, events;
1970 /* These are used to detect a runaway interrupt situation.
1972 * If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
1973 * we panic and shut down the hardware
1975 /* jiffies value the last time we were called */
1976 static int last_irq_jiffy; /* = 0 */
1977 static int loops_this_jiffy; /* = 0 */
1978 unsigned long flags;
1980 if (orinoco_lock(priv, &flags) != 0) {
1981 /* If hw is unavailable - we don't know if the irq was
1982 * for us or not */
1983 return IRQ_HANDLED;
1986 evstat = hermes_read_regn(hw, EVSTAT);
1987 events = evstat & hw->inten;
1988 if (!events) {
1989 orinoco_unlock(priv, &flags);
1990 return IRQ_NONE;
1993 if (jiffies != last_irq_jiffy)
1994 loops_this_jiffy = 0;
1995 last_irq_jiffy = jiffies;
1997 while (events && count--) {
1998 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
1999 printk(KERN_WARNING "%s: IRQ handler is looping too "
2000 "much! Resetting.\n", dev->name);
2001 /* Disable interrupts for now */
2002 hermes_set_irqmask(hw, 0);
2003 schedule_work(&priv->reset_work);
2004 break;
2007 /* Check the card hasn't been removed */
2008 if (!hermes_present(hw)) {
2009 DEBUG(0, "orinoco_interrupt(): card removed\n");
2010 break;
2013 if (events & HERMES_EV_TICK)
2014 __orinoco_ev_tick(dev, hw);
2015 if (events & HERMES_EV_WTERR)
2016 __orinoco_ev_wterr(dev, hw);
2017 if (events & HERMES_EV_INFDROP)
2018 __orinoco_ev_infdrop(dev, hw);
2019 if (events & HERMES_EV_INFO)
2020 __orinoco_ev_info(dev, hw);
2021 if (events & HERMES_EV_RX)
2022 __orinoco_ev_rx(dev, hw);
2023 if (events & HERMES_EV_TXEXC)
2024 __orinoco_ev_txexc(dev, hw);
2025 if (events & HERMES_EV_TX)
2026 __orinoco_ev_tx(dev, hw);
2027 if (events & HERMES_EV_ALLOC)
2028 __orinoco_ev_alloc(dev, hw);
2030 hermes_write_regn(hw, EVACK, evstat);
2032 evstat = hermes_read_regn(hw, EVSTAT);
2033 events = evstat & hw->inten;
2036 orinoco_unlock(priv, &flags);
2037 return IRQ_HANDLED;
2039 EXPORT_SYMBOL(orinoco_interrupt);
2041 /********************************************************************/
2042 /* Power management */
2043 /********************************************************************/
2044 #if defined(CONFIG_PM_SLEEP) && !defined(CONFIG_HERMES_CACHE_FW_ON_INIT)
2045 static int orinoco_pm_notifier(struct notifier_block *notifier,
2046 unsigned long pm_event,
2047 void *unused)
2049 struct orinoco_private *priv = container_of(notifier,
2050 struct orinoco_private,
2051 pm_notifier);
2053 /* All we need to do is cache the firmware before suspend, and
2054 * release it when we come out.
2056 * Only need to do this if we're downloading firmware. */
2057 if (!priv->do_fw_download)
2058 return NOTIFY_DONE;
2060 switch (pm_event) {
2061 case PM_HIBERNATION_PREPARE:
2062 case PM_SUSPEND_PREPARE:
2063 orinoco_cache_fw(priv, 0);
2064 break;
2066 case PM_POST_RESTORE:
2067 /* Restore from hibernation failed. We need to clean
2068 * up in exactly the same way, so fall through. */
2069 case PM_POST_HIBERNATION:
2070 case PM_POST_SUSPEND:
2071 orinoco_uncache_fw(priv);
2072 break;
2074 case PM_RESTORE_PREPARE:
2075 default:
2076 break;
2079 return NOTIFY_DONE;
2082 static void orinoco_register_pm_notifier(struct orinoco_private *priv)
2084 priv->pm_notifier.notifier_call = orinoco_pm_notifier;
2085 register_pm_notifier(&priv->pm_notifier);
2088 static void orinoco_unregister_pm_notifier(struct orinoco_private *priv)
2090 unregister_pm_notifier(&priv->pm_notifier);
2092 #else /* !PM_SLEEP || HERMES_CACHE_FW_ON_INIT */
2093 #define orinoco_register_pm_notifier(priv) do { } while(0)
2094 #define orinoco_unregister_pm_notifier(priv) do { } while(0)
2095 #endif
2097 /********************************************************************/
2098 /* Initialization */
2099 /********************************************************************/
2101 struct comp_id {
2102 u16 id, variant, major, minor;
2103 } __attribute__ ((packed));
2105 static inline fwtype_t determine_firmware_type(struct comp_id *nic_id)
2107 if (nic_id->id < 0x8000)
2108 return FIRMWARE_TYPE_AGERE;
2109 else if (nic_id->id == 0x8000 && nic_id->major == 0)
2110 return FIRMWARE_TYPE_SYMBOL;
2111 else
2112 return FIRMWARE_TYPE_INTERSIL;
2115 /* Set priv->firmware type, determine firmware properties */
2116 static int determine_firmware(struct net_device *dev)
2118 struct orinoco_private *priv = netdev_priv(dev);
2119 hermes_t *hw = &priv->hw;
2120 int err;
2121 struct comp_id nic_id, sta_id;
2122 unsigned int firmver;
2123 char tmp[SYMBOL_MAX_VER_LEN+1] __attribute__((aligned(2)));
2125 /* Get the hardware version */
2126 err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_NICID, &nic_id);
2127 if (err) {
2128 printk(KERN_ERR "%s: Cannot read hardware identity: error %d\n",
2129 dev->name, err);
2130 return err;
2133 le16_to_cpus(&nic_id.id);
2134 le16_to_cpus(&nic_id.variant);
2135 le16_to_cpus(&nic_id.major);
2136 le16_to_cpus(&nic_id.minor);
2137 printk(KERN_DEBUG "%s: Hardware identity %04x:%04x:%04x:%04x\n",
2138 dev->name, nic_id.id, nic_id.variant,
2139 nic_id.major, nic_id.minor);
2141 priv->firmware_type = determine_firmware_type(&nic_id);
2143 /* Get the firmware version */
2144 err = HERMES_READ_RECORD(hw, USER_BAP, HERMES_RID_STAID, &sta_id);
2145 if (err) {
2146 printk(KERN_ERR "%s: Cannot read station identity: error %d\n",
2147 dev->name, err);
2148 return err;
2151 le16_to_cpus(&sta_id.id);
2152 le16_to_cpus(&sta_id.variant);
2153 le16_to_cpus(&sta_id.major);
2154 le16_to_cpus(&sta_id.minor);
2155 printk(KERN_DEBUG "%s: Station identity %04x:%04x:%04x:%04x\n",
2156 dev->name, sta_id.id, sta_id.variant,
2157 sta_id.major, sta_id.minor);
2159 switch (sta_id.id) {
2160 case 0x15:
2161 printk(KERN_ERR "%s: Primary firmware is active\n",
2162 dev->name);
2163 return -ENODEV;
2164 case 0x14b:
2165 printk(KERN_ERR "%s: Tertiary firmware is active\n",
2166 dev->name);
2167 return -ENODEV;
2168 case 0x1f: /* Intersil, Agere, Symbol Spectrum24 */
2169 case 0x21: /* Symbol Spectrum24 Trilogy */
2170 break;
2171 default:
2172 printk(KERN_NOTICE "%s: Unknown station ID, please report\n",
2173 dev->name);
2174 break;
2177 /* Default capabilities */
2178 priv->has_sensitivity = 1;
2179 priv->has_mwo = 0;
2180 priv->has_preamble = 0;
2181 priv->has_port3 = 1;
2182 priv->has_ibss = 1;
2183 priv->has_wep = 0;
2184 priv->has_big_wep = 0;
2185 priv->has_alt_txcntl = 0;
2186 priv->has_ext_scan = 0;
2187 priv->has_wpa = 0;
2188 priv->do_fw_download = 0;
2190 /* Determine capabilities from the firmware version */
2191 switch (priv->firmware_type) {
2192 case FIRMWARE_TYPE_AGERE:
2193 /* Lucent Wavelan IEEE, Lucent Orinoco, Cabletron RoamAbout,
2194 ELSA, Melco, HP, IBM, Dell 1150, Compaq 110/210 */
2195 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2196 "Lucent/Agere %d.%02d", sta_id.major, sta_id.minor);
2198 firmver = ((unsigned long)sta_id.major << 16) | sta_id.minor;
2200 priv->has_ibss = (firmver >= 0x60006);
2201 priv->has_wep = (firmver >= 0x40020);
2202 priv->has_big_wep = 1; /* FIXME: this is wrong - how do we tell
2203 Gold cards from the others? */
2204 priv->has_mwo = (firmver >= 0x60000);
2205 priv->has_pm = (firmver >= 0x40020); /* Don't work in 7.52 ? */
2206 priv->ibss_port = 1;
2207 priv->has_hostscan = (firmver >= 0x8000a);
2208 priv->do_fw_download = 1;
2209 priv->broken_monitor = (firmver >= 0x80000);
2210 priv->has_alt_txcntl = (firmver >= 0x90000); /* All 9.x ? */
2211 priv->has_ext_scan = (firmver >= 0x90000); /* All 9.x ? */
2212 priv->has_wpa = (firmver >= 0x9002a);
2213 /* Tested with Agere firmware :
2214 * 1.16 ; 4.08 ; 4.52 ; 6.04 ; 6.16 ; 7.28 => Jean II
2215 * Tested CableTron firmware : 4.32 => Anton */
2216 break;
2217 case FIRMWARE_TYPE_SYMBOL:
2218 /* Symbol , 3Com AirConnect, Intel, Ericsson WLAN */
2219 /* Intel MAC : 00:02:B3:* */
2220 /* 3Com MAC : 00:50:DA:* */
2221 memset(tmp, 0, sizeof(tmp));
2222 /* Get the Symbol firmware version */
2223 err = hermes_read_ltv(hw, USER_BAP,
2224 HERMES_RID_SECONDARYVERSION_SYMBOL,
2225 SYMBOL_MAX_VER_LEN, NULL, &tmp);
2226 if (err) {
2227 printk(KERN_WARNING
2228 "%s: Error %d reading Symbol firmware info. "
2229 "Wildly guessing capabilities...\n",
2230 dev->name, err);
2231 firmver = 0;
2232 tmp[0] = '\0';
2233 } else {
2234 /* The firmware revision is a string, the format is
2235 * something like : "V2.20-01".
2236 * Quick and dirty parsing... - Jean II
2238 firmver = ((tmp[1] - '0') << 16)
2239 | ((tmp[3] - '0') << 12)
2240 | ((tmp[4] - '0') << 8)
2241 | ((tmp[6] - '0') << 4)
2242 | (tmp[7] - '0');
2244 tmp[SYMBOL_MAX_VER_LEN] = '\0';
2247 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2248 "Symbol %s", tmp);
2250 priv->has_ibss = (firmver >= 0x20000);
2251 priv->has_wep = (firmver >= 0x15012);
2252 priv->has_big_wep = (firmver >= 0x20000);
2253 priv->has_pm = (firmver >= 0x20000 && firmver < 0x22000) ||
2254 (firmver >= 0x29000 && firmver < 0x30000) ||
2255 firmver >= 0x31000;
2256 priv->has_preamble = (firmver >= 0x20000);
2257 priv->ibss_port = 4;
2259 /* Symbol firmware is found on various cards, but
2260 * there has been no attempt to check firmware
2261 * download on non-spectrum_cs based cards.
2263 * Given that the Agere firmware download works
2264 * differently, we should avoid doing a firmware
2265 * download with the Symbol algorithm on non-spectrum
2266 * cards.
2268 * For now we can identify a spectrum_cs based card
2269 * because it has a firmware reset function.
2271 priv->do_fw_download = (priv->stop_fw != NULL);
2273 priv->broken_disableport = (firmver == 0x25013) ||
2274 (firmver >= 0x30000 && firmver <= 0x31000);
2275 priv->has_hostscan = (firmver >= 0x31001) ||
2276 (firmver >= 0x29057 && firmver < 0x30000);
2277 /* Tested with Intel firmware : 0x20015 => Jean II */
2278 /* Tested with 3Com firmware : 0x15012 & 0x22001 => Jean II */
2279 break;
2280 case FIRMWARE_TYPE_INTERSIL:
2281 /* D-Link, Linksys, Adtron, ZoomAir, and many others...
2282 * Samsung, Compaq 100/200 and Proxim are slightly
2283 * different and less well tested */
2284 /* D-Link MAC : 00:40:05:* */
2285 /* Addtron MAC : 00:90:D1:* */
2286 snprintf(priv->fw_name, sizeof(priv->fw_name) - 1,
2287 "Intersil %d.%d.%d", sta_id.major, sta_id.minor,
2288 sta_id.variant);
2290 firmver = ((unsigned long)sta_id.major << 16) |
2291 ((unsigned long)sta_id.minor << 8) | sta_id.variant;
2293 priv->has_ibss = (firmver >= 0x000700); /* FIXME */
2294 priv->has_big_wep = priv->has_wep = (firmver >= 0x000800);
2295 priv->has_pm = (firmver >= 0x000700);
2296 priv->has_hostscan = (firmver >= 0x010301);
2298 if (firmver >= 0x000800)
2299 priv->ibss_port = 0;
2300 else {
2301 printk(KERN_NOTICE "%s: Intersil firmware earlier "
2302 "than v0.8.x - several features not supported\n",
2303 dev->name);
2304 priv->ibss_port = 1;
2306 break;
2308 printk(KERN_DEBUG "%s: Firmware determined as %s\n", dev->name,
2309 priv->fw_name);
2311 return 0;
2314 static int orinoco_init(struct net_device *dev)
2316 struct orinoco_private *priv = netdev_priv(dev);
2317 hermes_t *hw = &priv->hw;
2318 int err = 0;
2319 struct hermes_idstring nickbuf;
2320 u16 reclen;
2321 int len;
2323 /* No need to lock, the hw_unavailable flag is already set in
2324 * alloc_orinocodev() */
2325 priv->nicbuf_size = IEEE80211_MAX_FRAME_LEN + ETH_HLEN;
2327 /* Initialize the firmware */
2328 err = hermes_init(hw);
2329 if (err != 0) {
2330 printk(KERN_ERR "%s: failed to initialize firmware (err = %d)\n",
2331 dev->name, err);
2332 goto out;
2335 err = determine_firmware(dev);
2336 if (err != 0) {
2337 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
2338 dev->name);
2339 goto out;
2342 if (priv->do_fw_download) {
2343 #ifdef CONFIG_HERMES_CACHE_FW_ON_INIT
2344 orinoco_cache_fw(priv, 0);
2345 #endif
2347 err = orinoco_download(priv);
2348 if (err)
2349 priv->do_fw_download = 0;
2351 /* Check firmware version again */
2352 err = determine_firmware(dev);
2353 if (err != 0) {
2354 printk(KERN_ERR "%s: Incompatible firmware, aborting\n",
2355 dev->name);
2356 goto out;
2360 if (priv->has_port3)
2361 printk(KERN_DEBUG "%s: Ad-hoc demo mode supported\n",
2362 dev->name);
2363 if (priv->has_ibss)
2364 printk(KERN_DEBUG "%s: IEEE standard IBSS ad-hoc mode supported\n",
2365 dev->name);
2366 if (priv->has_wep) {
2367 printk(KERN_DEBUG "%s: WEP supported, %s-bit key\n", dev->name,
2368 priv->has_big_wep ? "104" : "40");
2370 if (priv->has_wpa) {
2371 printk(KERN_DEBUG "%s: WPA-PSK supported\n", dev->name);
2372 if (orinoco_mic_init(priv)) {
2373 printk(KERN_ERR "%s: Failed to setup MIC crypto "
2374 "algorithm. Disabling WPA support\n", dev->name);
2375 priv->has_wpa = 0;
2379 /* Now we have the firmware capabilities, allocate appropiate
2380 * sized scan buffers */
2381 if (orinoco_bss_data_allocate(priv))
2382 goto out;
2383 orinoco_bss_data_init(priv);
2385 /* Get the MAC address */
2386 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNMACADDR,
2387 ETH_ALEN, NULL, dev->dev_addr);
2388 if (err) {
2389 printk(KERN_WARNING "%s: failed to read MAC address!\n",
2390 dev->name);
2391 goto out;
2394 printk(KERN_DEBUG "%s: MAC address %pM\n",
2395 dev->name, dev->dev_addr);
2397 /* Get the station name */
2398 err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CNFOWNNAME,
2399 sizeof(nickbuf), &reclen, &nickbuf);
2400 if (err) {
2401 printk(KERN_ERR "%s: failed to read station name\n",
2402 dev->name);
2403 goto out;
2405 if (nickbuf.len)
2406 len = min(IW_ESSID_MAX_SIZE, (int)le16_to_cpu(nickbuf.len));
2407 else
2408 len = min(IW_ESSID_MAX_SIZE, 2 * reclen);
2409 memcpy(priv->nick, &nickbuf.val, len);
2410 priv->nick[len] = '\0';
2412 printk(KERN_DEBUG "%s: Station name \"%s\"\n", dev->name, priv->nick);
2414 err = orinoco_allocate_fid(dev);
2415 if (err) {
2416 printk(KERN_ERR "%s: failed to allocate NIC buffer!\n",
2417 dev->name);
2418 goto out;
2421 /* Get allowed channels */
2422 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CHANNELLIST,
2423 &priv->channel_mask);
2424 if (err) {
2425 printk(KERN_ERR "%s: failed to read channel list!\n",
2426 dev->name);
2427 goto out;
2430 /* Get initial AP density */
2431 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFSYSTEMSCALE,
2432 &priv->ap_density);
2433 if (err || priv->ap_density < 1 || priv->ap_density > 3)
2434 priv->has_sensitivity = 0;
2436 /* Get initial RTS threshold */
2437 err = hermes_read_wordrec(hw, USER_BAP, HERMES_RID_CNFRTSTHRESHOLD,
2438 &priv->rts_thresh);
2439 if (err) {
2440 printk(KERN_ERR "%s: failed to read RTS threshold!\n",
2441 dev->name);
2442 goto out;
2445 /* Get initial fragmentation settings */
2446 if (priv->has_mwo)
2447 err = hermes_read_wordrec(hw, USER_BAP,
2448 HERMES_RID_CNFMWOROBUST_AGERE,
2449 &priv->mwo_robust);
2450 else
2451 err = hermes_read_wordrec(hw, USER_BAP,
2452 HERMES_RID_CNFFRAGMENTATIONTHRESHOLD,
2453 &priv->frag_thresh);
2454 if (err) {
2455 printk(KERN_ERR "%s: failed to read fragmentation settings!\n",
2456 dev->name);
2457 goto out;
2460 /* Power management setup */
2461 if (priv->has_pm) {
2462 priv->pm_on = 0;
2463 priv->pm_mcast = 1;
2464 err = hermes_read_wordrec(hw, USER_BAP,
2465 HERMES_RID_CNFMAXSLEEPDURATION,
2466 &priv->pm_period);
2467 if (err) {
2468 printk(KERN_ERR "%s: failed to read power management period!\n",
2469 dev->name);
2470 goto out;
2472 err = hermes_read_wordrec(hw, USER_BAP,
2473 HERMES_RID_CNFPMHOLDOVERDURATION,
2474 &priv->pm_timeout);
2475 if (err) {
2476 printk(KERN_ERR "%s: failed to read power management timeout!\n",
2477 dev->name);
2478 goto out;
2482 /* Preamble setup */
2483 if (priv->has_preamble) {
2484 err = hermes_read_wordrec(hw, USER_BAP,
2485 HERMES_RID_CNFPREAMBLE_SYMBOL,
2486 &priv->preamble);
2487 if (err)
2488 goto out;
2491 /* Set up the default configuration */
2492 priv->iw_mode = IW_MODE_INFRA;
2493 /* By default use IEEE/IBSS ad-hoc mode if we have it */
2494 priv->prefer_port3 = priv->has_port3 && (!priv->has_ibss);
2495 set_port_type(priv);
2496 priv->channel = 0; /* use firmware default */
2498 priv->promiscuous = 0;
2499 priv->encode_alg = IW_ENCODE_ALG_NONE;
2500 priv->tx_key = 0;
2501 priv->wpa_enabled = 0;
2502 priv->tkip_cm_active = 0;
2503 priv->key_mgmt = 0;
2504 priv->wpa_ie_len = 0;
2505 priv->wpa_ie = NULL;
2507 /* Make the hardware available, as long as it hasn't been
2508 * removed elsewhere (e.g. by PCMCIA hot unplug) */
2509 spin_lock_irq(&priv->lock);
2510 priv->hw_unavailable--;
2511 spin_unlock_irq(&priv->lock);
2513 printk(KERN_DEBUG "%s: ready\n", dev->name);
2515 out:
2516 return err;
2519 static const struct net_device_ops orinoco_netdev_ops = {
2520 .ndo_init = orinoco_init,
2521 .ndo_open = orinoco_open,
2522 .ndo_stop = orinoco_stop,
2523 .ndo_start_xmit = orinoco_xmit,
2524 .ndo_set_multicast_list = orinoco_set_multicast_list,
2525 .ndo_change_mtu = orinoco_change_mtu,
2526 .ndo_set_mac_address = eth_mac_addr,
2527 .ndo_validate_addr = eth_validate_addr,
2528 .ndo_tx_timeout = orinoco_tx_timeout,
2529 .ndo_get_stats = orinoco_get_stats,
2532 struct net_device
2533 *alloc_orinocodev(int sizeof_card,
2534 struct device *device,
2535 int (*hard_reset)(struct orinoco_private *),
2536 int (*stop_fw)(struct orinoco_private *, int))
2538 struct net_device *dev;
2539 struct orinoco_private *priv;
2541 dev = alloc_etherdev(sizeof(struct orinoco_private) + sizeof_card);
2542 if (!dev)
2543 return NULL;
2544 priv = netdev_priv(dev);
2545 priv->ndev = dev;
2546 if (sizeof_card)
2547 priv->card = (void *)((unsigned long)priv
2548 + sizeof(struct orinoco_private));
2549 else
2550 priv->card = NULL;
2551 priv->dev = device;
2553 /* Setup / override net_device fields */
2554 dev->netdev_ops = &orinoco_netdev_ops;
2555 dev->watchdog_timeo = HZ; /* 1 second timeout */
2556 dev->ethtool_ops = &orinoco_ethtool_ops;
2557 dev->wireless_handlers = &orinoco_handler_def;
2558 #ifdef WIRELESS_SPY
2559 priv->wireless_data.spy_data = &priv->spy_data;
2560 dev->wireless_data = &priv->wireless_data;
2561 #endif
2563 /* Reserve space in skb for the SNAP header */
2564 dev->hard_header_len += ENCAPS_OVERHEAD;
2566 /* Set up default callbacks */
2567 priv->hard_reset = hard_reset;
2568 priv->stop_fw = stop_fw;
2570 spin_lock_init(&priv->lock);
2571 priv->open = 0;
2572 priv->hw_unavailable = 1; /* orinoco_init() must clear this
2573 * before anything else touches the
2574 * hardware */
2575 INIT_WORK(&priv->reset_work, orinoco_reset);
2576 INIT_WORK(&priv->join_work, orinoco_join_ap);
2577 INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
2579 INIT_LIST_HEAD(&priv->rx_list);
2580 tasklet_init(&priv->rx_tasklet, orinoco_rx_isr_tasklet,
2581 (unsigned long) dev);
2583 netif_carrier_off(dev);
2584 priv->last_linkstatus = 0xffff;
2586 #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
2587 priv->cached_pri_fw = NULL;
2588 priv->cached_fw = NULL;
2589 #endif
2591 /* Register PM notifiers */
2592 orinoco_register_pm_notifier(priv);
2594 return dev;
2596 EXPORT_SYMBOL(alloc_orinocodev);
2598 void free_orinocodev(struct net_device *dev)
2600 struct orinoco_private *priv = netdev_priv(dev);
2601 struct orinoco_rx_data *rx_data, *temp;
2603 /* If the tasklet is scheduled when we call tasklet_kill it
2604 * will run one final time. However the tasklet will only
2605 * drain priv->rx_list if the hw is still available. */
2606 tasklet_kill(&priv->rx_tasklet);
2608 /* Explicitly drain priv->rx_list */
2609 list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
2610 list_del(&rx_data->list);
2612 dev_kfree_skb(rx_data->skb);
2613 kfree(rx_data->desc);
2614 kfree(rx_data);
2617 orinoco_unregister_pm_notifier(priv);
2618 orinoco_uncache_fw(priv);
2620 priv->wpa_ie_len = 0;
2621 kfree(priv->wpa_ie);
2622 orinoco_mic_free(priv);
2623 orinoco_bss_data_free(priv);
2624 free_netdev(dev);
2626 EXPORT_SYMBOL(free_orinocodev);
2628 static void orinoco_get_drvinfo(struct net_device *dev,
2629 struct ethtool_drvinfo *info)
2631 struct orinoco_private *priv = netdev_priv(dev);
2633 strncpy(info->driver, DRIVER_NAME, sizeof(info->driver) - 1);
2634 strncpy(info->version, DRIVER_VERSION, sizeof(info->version) - 1);
2635 strncpy(info->fw_version, priv->fw_name, sizeof(info->fw_version) - 1);
2636 if (dev->dev.parent)
2637 strncpy(info->bus_info, dev_name(dev->dev.parent),
2638 sizeof(info->bus_info) - 1);
2639 else
2640 snprintf(info->bus_info, sizeof(info->bus_info) - 1,
2641 "PCMCIA %p", priv->hw.iobase);
2644 static const struct ethtool_ops orinoco_ethtool_ops = {
2645 .get_drvinfo = orinoco_get_drvinfo,
2646 .get_link = ethtool_op_get_link,
2649 /********************************************************************/
2650 /* Module initialization */
2651 /********************************************************************/
2653 /* Can't be declared "const" or the whole __initdata section will
2654 * become const */
2655 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
2656 " (David Gibson <hermes@gibson.dropbear.id.au>, "
2657 "Pavel Roskin <proski@gnu.org>, et al)";
2659 static int __init init_orinoco(void)
2661 printk(KERN_DEBUG "%s\n", version);
2662 return 0;
2665 static void __exit exit_orinoco(void)
2669 module_init(init_orinoco);
2670 module_exit(exit_orinoco);