Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[sfrench/cifs-2.6.git] / drivers / net / wireless / orinoco / main.c
1 /* main.c - (formerly known as dldwd_cs.c, orinoco_cs.c and orinoco.c)
2  *
3  * A driver for Hermes or Prism 2 chipset based PCMCIA wireless
4  * adaptors, with Lucent/Agere, Intersil or Symbol firmware.
5  *
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>
9  *
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
15  *
16  * Based on dummy_cs.c 1.27 2000/06/12 21:27:25
17  *
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/
21  *
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/
26  *
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.
31  *
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.
36  *
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.  */
47
48 /*
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.
57  */
58
59 /* Locking and synchronization:
60  *
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.
66  *
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).
75  */
76
77 #define DRIVER_NAME "orinoco"
78
79 #include <linux/module.h>
80 #include <linux/kernel.h>
81 #include <linux/slab.h>
82 #include <linux/init.h>
83 #include <linux/delay.h>
84 #include <linux/device.h>
85 #include <linux/netdevice.h>
86 #include <linux/etherdevice.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>
93
94 #include "hermes_rid.h"
95 #include "hermes_dld.h"
96 #include "hw.h"
97 #include "scan.h"
98 #include "mic.h"
99 #include "fw.h"
100 #include "wext.h"
101 #include "cfg.h"
102 #include "main.h"
103
104 #include "orinoco.h"
105
106 /********************************************************************/
107 /* Module information                                               */
108 /********************************************************************/
109
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");
115
116 /* Level of debugging. Used in the macros in orinoco.h */
117 #ifdef ORINOCO_DEBUG
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");
122 #endif
123
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");
127
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");
132
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");
136
137 /********************************************************************/
138 /* Internal constants                                               */
139 /********************************************************************/
140
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)
144
145 #define ORINOCO_MIN_MTU         256
146 #define ORINOCO_MAX_MTU         (IEEE80211_MAX_DATA_LEN - ENCAPS_OVERHEAD)
147
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
151                                             * device could
152                                             * legitimately generate */
153
154 #define DUMMY_FID               0xFFFF
155
156 /*#define MAX_MULTICAST(priv)   (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
157   HERMES_MAX_MULTICAST : 0)*/
158 #define MAX_MULTICAST(priv)     (HERMES_MAX_MULTICAST)
159
160 #define ORINOCO_INTEN           (HERMES_EV_RX | HERMES_EV_ALLOC \
161                                  | HERMES_EV_TX | HERMES_EV_TXEXC \
162                                  | HERMES_EV_WTERR | HERMES_EV_INFO \
163                                  | HERMES_EV_INFDROP)
164
165 /********************************************************************/
166 /* Data types                                                       */
167 /********************************************************************/
168
169 /* Beginning of the Tx descriptor, used in TxExc handling */
170 struct hermes_txexc_data {
171         struct hermes_tx_descriptor desc;
172         __le16 frame_ctl;
173         __le16 duration_id;
174         u8 addr1[ETH_ALEN];
175 } __attribute__ ((packed));
176
177 /* Rx frame header except compatibility 802.3 header */
178 struct hermes_rx_descriptor {
179         /* Control */
180         __le16 status;
181         __le32 time;
182         u8 silence;
183         u8 signal;
184         u8 rate;
185         u8 rxflow;
186         __le32 reserved;
187
188         /* 802.11 header */
189         __le16 frame_ctl;
190         __le16 duration_id;
191         u8 addr1[ETH_ALEN];
192         u8 addr2[ETH_ALEN];
193         u8 addr3[ETH_ALEN];
194         __le16 seq_ctl;
195         u8 addr4[ETH_ALEN];
196
197         /* Data length */
198         __le16 data_len;
199 } __attribute__ ((packed));
200
201 struct orinoco_rx_data {
202         struct hermes_rx_descriptor *desc;
203         struct sk_buff *skb;
204         struct list_head list;
205 };
206
207 struct orinoco_scan_data {
208         void *buf;
209         size_t len;
210         int type;
211         struct list_head list;
212 };
213
214 /********************************************************************/
215 /* Function prototypes                                              */
216 /********************************************************************/
217
218 static int __orinoco_set_multicast_list(struct net_device *dev);
219 static int __orinoco_up(struct orinoco_private *priv);
220 static int __orinoco_down(struct orinoco_private *priv);
221 static int __orinoco_commit(struct orinoco_private *priv);
222
223 /********************************************************************/
224 /* Internal helper functions                                        */
225 /********************************************************************/
226
227 void set_port_type(struct orinoco_private *priv)
228 {
229         switch (priv->iw_mode) {
230         case NL80211_IFTYPE_STATION:
231                 priv->port_type = 1;
232                 priv->createibss = 0;
233                 break;
234         case NL80211_IFTYPE_ADHOC:
235                 if (priv->prefer_port3) {
236                         priv->port_type = 3;
237                         priv->createibss = 0;
238                 } else {
239                         priv->port_type = priv->ibss_port;
240                         priv->createibss = 1;
241                 }
242                 break;
243         case NL80211_IFTYPE_MONITOR:
244                 priv->port_type = 3;
245                 priv->createibss = 0;
246                 break;
247         default:
248                 printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
249                        priv->ndev->name);
250         }
251 }
252
253 /********************************************************************/
254 /* Device methods                                                   */
255 /********************************************************************/
256
257 int orinoco_open(struct net_device *dev)
258 {
259         struct orinoco_private *priv = ndev_priv(dev);
260         unsigned long flags;
261         int err;
262
263         if (orinoco_lock(priv, &flags) != 0)
264                 return -EBUSY;
265
266         err = __orinoco_up(priv);
267
268         if (!err)
269                 priv->open = 1;
270
271         orinoco_unlock(priv, &flags);
272
273         return err;
274 }
275 EXPORT_SYMBOL(orinoco_open);
276
277 int orinoco_stop(struct net_device *dev)
278 {
279         struct orinoco_private *priv = ndev_priv(dev);
280         int err = 0;
281
282         /* We mustn't use orinoco_lock() here, because we need to be
283            able to close the interface even if hw_unavailable is set
284            (e.g. as we're released after a PC Card removal) */
285         orinoco_lock_irq(priv);
286
287         priv->open = 0;
288
289         err = __orinoco_down(priv);
290
291         orinoco_unlock_irq(priv);
292
293         return err;
294 }
295 EXPORT_SYMBOL(orinoco_stop);
296
297 struct net_device_stats *orinoco_get_stats(struct net_device *dev)
298 {
299         struct orinoco_private *priv = ndev_priv(dev);
300
301         return &priv->stats;
302 }
303 EXPORT_SYMBOL(orinoco_get_stats);
304
305 void orinoco_set_multicast_list(struct net_device *dev)
306 {
307         struct orinoco_private *priv = ndev_priv(dev);
308         unsigned long flags;
309
310         if (orinoco_lock(priv, &flags) != 0) {
311                 printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
312                        "called when hw_unavailable\n", dev->name);
313                 return;
314         }
315
316         __orinoco_set_multicast_list(dev);
317         orinoco_unlock(priv, &flags);
318 }
319 EXPORT_SYMBOL(orinoco_set_multicast_list);
320
321 int orinoco_change_mtu(struct net_device *dev, int new_mtu)
322 {
323         struct orinoco_private *priv = ndev_priv(dev);
324
325         if ((new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU))
326                 return -EINVAL;
327
328         /* MTU + encapsulation + header length */
329         if ((new_mtu + ENCAPS_OVERHEAD + sizeof(struct ieee80211_hdr)) >
330              (priv->nicbuf_size - ETH_HLEN))
331                 return -EINVAL;
332
333         dev->mtu = new_mtu;
334
335         return 0;
336 }
337 EXPORT_SYMBOL(orinoco_change_mtu);
338
339 /********************************************************************/
340 /* Tx path                                                          */
341 /********************************************************************/
342
343 static netdev_tx_t orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
344 {
345         struct orinoco_private *priv = ndev_priv(dev);
346         struct net_device_stats *stats = &priv->stats;
347         struct orinoco_tkip_key *key;
348         hermes_t *hw = &priv->hw;
349         int err = 0;
350         u16 txfid = priv->txfid;
351         struct ethhdr *eh;
352         int tx_control;
353         unsigned long flags;
354         int do_mic;
355
356         if (!netif_running(dev)) {
357                 printk(KERN_ERR "%s: Tx on stopped device!\n",
358                        dev->name);
359                 return NETDEV_TX_BUSY;
360         }
361
362         if (netif_queue_stopped(dev)) {
363                 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
364                        dev->name);
365                 return NETDEV_TX_BUSY;
366         }
367
368         if (orinoco_lock(priv, &flags) != 0) {
369                 printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
370                        dev->name);
371                 return NETDEV_TX_BUSY;
372         }
373
374         if (!netif_carrier_ok(dev) ||
375             (priv->iw_mode == NL80211_IFTYPE_MONITOR)) {
376                 /* Oops, the firmware hasn't established a connection,
377                    silently drop the packet (this seems to be the
378                    safest approach). */
379                 goto drop;
380         }
381
382         /* Check packet length */
383         if (skb->len < ETH_HLEN)
384                 goto drop;
385
386         key = (struct orinoco_tkip_key *) priv->keys[priv->tx_key].key;
387
388         do_mic = ((priv->encode_alg == ORINOCO_ALG_TKIP) &&
389                   (key != NULL));
390
391         tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
392
393         if (do_mic)
394                 tx_control |= (priv->tx_key << HERMES_MIC_KEY_ID_SHIFT) |
395                         HERMES_TXCTRL_MIC;
396
397         if (priv->has_alt_txcntl) {
398                 /* WPA enabled firmwares have tx_cntl at the end of
399                  * the 802.11 header.  So write zeroed descriptor and
400                  * 802.11 header at the same time
401                  */
402                 char desc[HERMES_802_3_OFFSET];
403                 __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
404
405                 memset(&desc, 0, sizeof(desc));
406
407                 *txcntl = cpu_to_le16(tx_control);
408                 err = hw->ops->bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
409                                           txfid, 0);
410                 if (err) {
411                         if (net_ratelimit())
412                                 printk(KERN_ERR "%s: Error %d writing Tx "
413                                        "descriptor to BAP\n", dev->name, err);
414                         goto busy;
415                 }
416         } else {
417                 struct hermes_tx_descriptor desc;
418
419                 memset(&desc, 0, sizeof(desc));
420
421                 desc.tx_control = cpu_to_le16(tx_control);
422                 err = hw->ops->bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
423                                           txfid, 0);
424                 if (err) {
425                         if (net_ratelimit())
426                                 printk(KERN_ERR "%s: Error %d writing Tx "
427                                        "descriptor to BAP\n", dev->name, err);
428                         goto busy;
429                 }
430
431                 /* Clear the 802.11 header and data length fields - some
432                  * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
433                  * if this isn't done. */
434                 hermes_clear_words(hw, HERMES_DATA0,
435                                    HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
436         }
437
438         eh = (struct ethhdr *)skb->data;
439
440         /* Encapsulate Ethernet-II frames */
441         if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
442                 struct header_struct {
443                         struct ethhdr eth;      /* 802.3 header */
444                         u8 encap[6];            /* 802.2 header */
445                 } __attribute__ ((packed)) hdr;
446
447                 /* Strip destination and source from the data */
448                 skb_pull(skb, 2 * ETH_ALEN);
449
450                 /* And move them to a separate header */
451                 memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
452                 hdr.eth.h_proto = htons(sizeof(encaps_hdr) + skb->len);
453                 memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
454
455                 /* Insert the SNAP header */
456                 if (skb_headroom(skb) < sizeof(hdr)) {
457                         printk(KERN_ERR
458                                "%s: Not enough headroom for 802.2 headers %d\n",
459                                dev->name, skb_headroom(skb));
460                         goto drop;
461                 }
462                 eh = (struct ethhdr *) skb_push(skb, sizeof(hdr));
463                 memcpy(eh, &hdr, sizeof(hdr));
464         }
465
466         err = hw->ops->bap_pwrite(hw, USER_BAP, skb->data, skb->len,
467                                   txfid, HERMES_802_3_OFFSET);
468         if (err) {
469                 printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
470                        dev->name, err);
471                 goto busy;
472         }
473
474         /* Calculate Michael MIC */
475         if (do_mic) {
476                 u8 mic_buf[MICHAEL_MIC_LEN + 1];
477                 u8 *mic;
478                 size_t offset;
479                 size_t len;
480
481                 if (skb->len % 2) {
482                         /* MIC start is on an odd boundary */
483                         mic_buf[0] = skb->data[skb->len - 1];
484                         mic = &mic_buf[1];
485                         offset = skb->len - 1;
486                         len = MICHAEL_MIC_LEN + 1;
487                 } else {
488                         mic = &mic_buf[0];
489                         offset = skb->len;
490                         len = MICHAEL_MIC_LEN;
491                 }
492
493                 orinoco_mic(priv->tx_tfm_mic, key->tx_mic,
494                             eh->h_dest, eh->h_source, 0 /* priority */,
495                             skb->data + ETH_HLEN, skb->len - ETH_HLEN, mic);
496
497                 /* Write the MIC */
498                 err = hw->ops->bap_pwrite(hw, USER_BAP, &mic_buf[0], len,
499                                           txfid, HERMES_802_3_OFFSET + offset);
500                 if (err) {
501                         printk(KERN_ERR "%s: Error %d writing MIC to BAP\n",
502                                dev->name, err);
503                         goto busy;
504                 }
505         }
506
507         /* Finally, we actually initiate the send */
508         netif_stop_queue(dev);
509
510         err = hw->ops->cmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
511                                 txfid, NULL);
512         if (err) {
513                 netif_start_queue(dev);
514                 if (net_ratelimit())
515                         printk(KERN_ERR "%s: Error %d transmitting packet\n",
516                                 dev->name, err);
517                 goto busy;
518         }
519
520         dev->trans_start = jiffies;
521         stats->tx_bytes += HERMES_802_3_OFFSET + skb->len;
522         goto ok;
523
524  drop:
525         stats->tx_errors++;
526         stats->tx_dropped++;
527
528  ok:
529         orinoco_unlock(priv, &flags);
530         dev_kfree_skb(skb);
531         return NETDEV_TX_OK;
532
533  busy:
534         if (err == -EIO)
535                 schedule_work(&priv->reset_work);
536         orinoco_unlock(priv, &flags);
537         return NETDEV_TX_BUSY;
538 }
539
540 static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
541 {
542         struct orinoco_private *priv = ndev_priv(dev);
543         u16 fid = hermes_read_regn(hw, ALLOCFID);
544
545         if (fid != priv->txfid) {
546                 if (fid != DUMMY_FID)
547                         printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
548                                dev->name, fid);
549                 return;
550         }
551
552         hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
553 }
554
555 static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
556 {
557         struct orinoco_private *priv = ndev_priv(dev);
558         struct net_device_stats *stats = &priv->stats;
559
560         stats->tx_packets++;
561
562         netif_wake_queue(dev);
563
564         hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
565 }
566
567 static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
568 {
569         struct orinoco_private *priv = ndev_priv(dev);
570         struct net_device_stats *stats = &priv->stats;
571         u16 fid = hermes_read_regn(hw, TXCOMPLFID);
572         u16 status;
573         struct hermes_txexc_data hdr;
574         int err = 0;
575
576         if (fid == DUMMY_FID)
577                 return; /* Nothing's really happened */
578
579         /* Read part of the frame header - we need status and addr1 */
580         err = hw->ops->bap_pread(hw, IRQ_BAP, &hdr,
581                                  sizeof(struct hermes_txexc_data),
582                                  fid, 0);
583
584         hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
585         stats->tx_errors++;
586
587         if (err) {
588                 printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
589                        "(FID=%04X error %d)\n",
590                        dev->name, fid, err);
591                 return;
592         }
593
594         DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
595               err, fid);
596
597         /* We produce a TXDROP event only for retry or lifetime
598          * exceeded, because that's the only status that really mean
599          * that this particular node went away.
600          * Other errors means that *we* screwed up. - Jean II */
601         status = le16_to_cpu(hdr.desc.status);
602         if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
603                 union iwreq_data        wrqu;
604
605                 /* Copy 802.11 dest address.
606                  * We use the 802.11 header because the frame may
607                  * not be 802.3 or may be mangled...
608                  * In Ad-Hoc mode, it will be the node address.
609                  * In managed mode, it will be most likely the AP addr
610                  * User space will figure out how to convert it to
611                  * whatever it needs (IP address or else).
612                  * - Jean II */
613                 memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
614                 wrqu.addr.sa_family = ARPHRD_ETHER;
615
616                 /* Send event to user space */
617                 wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
618         }
619
620         netif_wake_queue(dev);
621 }
622
623 void orinoco_tx_timeout(struct net_device *dev)
624 {
625         struct orinoco_private *priv = ndev_priv(dev);
626         struct net_device_stats *stats = &priv->stats;
627         struct hermes *hw = &priv->hw;
628
629         printk(KERN_WARNING "%s: Tx timeout! "
630                "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
631                dev->name, hermes_read_regn(hw, ALLOCFID),
632                hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
633
634         stats->tx_errors++;
635
636         schedule_work(&priv->reset_work);
637 }
638 EXPORT_SYMBOL(orinoco_tx_timeout);
639
640 /********************************************************************/
641 /* Rx path (data frames)                                            */
642 /********************************************************************/
643
644 /* Does the frame have a SNAP header indicating it should be
645  * de-encapsulated to Ethernet-II? */
646 static inline int is_ethersnap(void *_hdr)
647 {
648         u8 *hdr = _hdr;
649
650         /* We de-encapsulate all packets which, a) have SNAP headers
651          * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
652          * and where b) the OUI of the SNAP header is 00:00:00 or
653          * 00:00:f8 - we need both because different APs appear to use
654          * different OUIs for some reason */
655         return (memcmp(hdr, &encaps_hdr, 5) == 0)
656                 && ((hdr[5] == 0x00) || (hdr[5] == 0xf8));
657 }
658
659 static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
660                                       int level, int noise)
661 {
662         struct iw_quality wstats;
663         wstats.level = level - 0x95;
664         wstats.noise = noise - 0x95;
665         wstats.qual = (level > noise) ? (level - noise) : 0;
666         wstats.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
667         /* Update spy records */
668         wireless_spy_update(dev, mac, &wstats);
669 }
670
671 static void orinoco_stat_gather(struct net_device *dev,
672                                 struct sk_buff *skb,
673                                 struct hermes_rx_descriptor *desc)
674 {
675         struct orinoco_private *priv = ndev_priv(dev);
676
677         /* Using spy support with lots of Rx packets, like in an
678          * infrastructure (AP), will really slow down everything, because
679          * the MAC address must be compared to each entry of the spy list.
680          * If the user really asks for it (set some address in the
681          * spy list), we do it, but he will pay the price.
682          * Note that to get here, you need both WIRELESS_SPY
683          * compiled in AND some addresses in the list !!!
684          */
685         /* Note : gcc will optimise the whole section away if
686          * WIRELESS_SPY is not defined... - Jean II */
687         if (SPY_NUMBER(priv)) {
688                 orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
689                                    desc->signal, desc->silence);
690         }
691 }
692
693 /*
694  * orinoco_rx_monitor - handle received monitor frames.
695  *
696  * Arguments:
697  *      dev             network device
698  *      rxfid           received FID
699  *      desc            rx descriptor of the frame
700  *
701  * Call context: interrupt
702  */
703 static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
704                                struct hermes_rx_descriptor *desc)
705 {
706         u32 hdrlen = 30;        /* return full header by default */
707         u32 datalen = 0;
708         u16 fc;
709         int err;
710         int len;
711         struct sk_buff *skb;
712         struct orinoco_private *priv = ndev_priv(dev);
713         struct net_device_stats *stats = &priv->stats;
714         hermes_t *hw = &priv->hw;
715
716         len = le16_to_cpu(desc->data_len);
717
718         /* Determine the size of the header and the data */
719         fc = le16_to_cpu(desc->frame_ctl);
720         switch (fc & IEEE80211_FCTL_FTYPE) {
721         case IEEE80211_FTYPE_DATA:
722                 if ((fc & IEEE80211_FCTL_TODS)
723                     && (fc & IEEE80211_FCTL_FROMDS))
724                         hdrlen = 30;
725                 else
726                         hdrlen = 24;
727                 datalen = len;
728                 break;
729         case IEEE80211_FTYPE_MGMT:
730                 hdrlen = 24;
731                 datalen = len;
732                 break;
733         case IEEE80211_FTYPE_CTL:
734                 switch (fc & IEEE80211_FCTL_STYPE) {
735                 case IEEE80211_STYPE_PSPOLL:
736                 case IEEE80211_STYPE_RTS:
737                 case IEEE80211_STYPE_CFEND:
738                 case IEEE80211_STYPE_CFENDACK:
739                         hdrlen = 16;
740                         break;
741                 case IEEE80211_STYPE_CTS:
742                 case IEEE80211_STYPE_ACK:
743                         hdrlen = 10;
744                         break;
745                 }
746                 break;
747         default:
748                 /* Unknown frame type */
749                 break;
750         }
751
752         /* sanity check the length */
753         if (datalen > IEEE80211_MAX_DATA_LEN + 12) {
754                 printk(KERN_DEBUG "%s: oversized monitor frame, "
755                        "data length = %d\n", dev->name, datalen);
756                 stats->rx_length_errors++;
757                 goto update_stats;
758         }
759
760         skb = dev_alloc_skb(hdrlen + datalen);
761         if (!skb) {
762                 printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
763                        dev->name);
764                 goto update_stats;
765         }
766
767         /* Copy the 802.11 header to the skb */
768         memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
769         skb_reset_mac_header(skb);
770
771         /* If any, copy the data from the card to the skb */
772         if (datalen > 0) {
773                 err = hw->ops->bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
774                                          ALIGN(datalen, 2), rxfid,
775                                          HERMES_802_2_OFFSET);
776                 if (err) {
777                         printk(KERN_ERR "%s: error %d reading monitor frame\n",
778                                dev->name, err);
779                         goto drop;
780                 }
781         }
782
783         skb->dev = dev;
784         skb->ip_summed = CHECKSUM_NONE;
785         skb->pkt_type = PACKET_OTHERHOST;
786         skb->protocol = cpu_to_be16(ETH_P_802_2);
787
788         stats->rx_packets++;
789         stats->rx_bytes += skb->len;
790
791         netif_rx(skb);
792         return;
793
794  drop:
795         dev_kfree_skb_irq(skb);
796  update_stats:
797         stats->rx_errors++;
798         stats->rx_dropped++;
799 }
800
801 void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
802 {
803         struct orinoco_private *priv = ndev_priv(dev);
804         struct net_device_stats *stats = &priv->stats;
805         struct iw_statistics *wstats = &priv->wstats;
806         struct sk_buff *skb = NULL;
807         u16 rxfid, status;
808         int length;
809         struct hermes_rx_descriptor *desc;
810         struct orinoco_rx_data *rx_data;
811         int err;
812
813         desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
814         if (!desc) {
815                 printk(KERN_WARNING
816                        "%s: Can't allocate space for RX descriptor\n",
817                        dev->name);
818                 goto update_stats;
819         }
820
821         rxfid = hermes_read_regn(hw, RXFID);
822
823         err = hw->ops->bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
824                                  rxfid, 0);
825         if (err) {
826                 printk(KERN_ERR "%s: error %d reading Rx descriptor. "
827                        "Frame dropped.\n", dev->name, err);
828                 goto update_stats;
829         }
830
831         status = le16_to_cpu(desc->status);
832
833         if (status & HERMES_RXSTAT_BADCRC) {
834                 DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
835                       dev->name);
836                 stats->rx_crc_errors++;
837                 goto update_stats;
838         }
839
840         /* Handle frames in monitor mode */
841         if (priv->iw_mode == NL80211_IFTYPE_MONITOR) {
842                 orinoco_rx_monitor(dev, rxfid, desc);
843                 goto out;
844         }
845
846         if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
847                 DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
848                       dev->name);
849                 wstats->discard.code++;
850                 goto update_stats;
851         }
852
853         length = le16_to_cpu(desc->data_len);
854
855         /* Sanity checks */
856         if (length < 3) { /* No for even an 802.2 LLC header */
857                 /* At least on Symbol firmware with PCF we get quite a
858                    lot of these legitimately - Poll frames with no
859                    data. */
860                 goto out;
861         }
862         if (length > IEEE80211_MAX_DATA_LEN) {
863                 printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
864                        dev->name, length);
865                 stats->rx_length_errors++;
866                 goto update_stats;
867         }
868
869         /* Payload size does not include Michael MIC. Increase payload
870          * size to read it together with the data. */
871         if (status & HERMES_RXSTAT_MIC)
872                 length += MICHAEL_MIC_LEN;
873
874         /* We need space for the packet data itself, plus an ethernet
875            header, plus 2 bytes so we can align the IP header on a
876            32bit boundary, plus 1 byte so we can read in odd length
877            packets from the card, which has an IO granularity of 16
878            bits */
879         skb = dev_alloc_skb(length+ETH_HLEN+2+1);
880         if (!skb) {
881                 printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
882                        dev->name);
883                 goto update_stats;
884         }
885
886         /* We'll prepend the header, so reserve space for it.  The worst
887            case is no decapsulation, when 802.3 header is prepended and
888            nothing is removed.  2 is for aligning the IP header.  */
889         skb_reserve(skb, ETH_HLEN + 2);
890
891         err = hw->ops->bap_pread(hw, IRQ_BAP, skb_put(skb, length),
892                                  ALIGN(length, 2), rxfid,
893                                  HERMES_802_2_OFFSET);
894         if (err) {
895                 printk(KERN_ERR "%s: error %d reading frame. "
896                        "Frame dropped.\n", dev->name, err);
897                 goto drop;
898         }
899
900         /* Add desc and skb to rx queue */
901         rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
902         if (!rx_data) {
903                 printk(KERN_WARNING "%s: Can't allocate RX packet\n",
904                         dev->name);
905                 goto drop;
906         }
907         rx_data->desc = desc;
908         rx_data->skb = skb;
909         list_add_tail(&rx_data->list, &priv->rx_list);
910         tasklet_schedule(&priv->rx_tasklet);
911
912         return;
913
914 drop:
915         dev_kfree_skb_irq(skb);
916 update_stats:
917         stats->rx_errors++;
918         stats->rx_dropped++;
919 out:
920         kfree(desc);
921 }
922 EXPORT_SYMBOL(__orinoco_ev_rx);
923
924 static void orinoco_rx(struct net_device *dev,
925                        struct hermes_rx_descriptor *desc,
926                        struct sk_buff *skb)
927 {
928         struct orinoco_private *priv = ndev_priv(dev);
929         struct net_device_stats *stats = &priv->stats;
930         u16 status, fc;
931         int length;
932         struct ethhdr *hdr;
933
934         status = le16_to_cpu(desc->status);
935         length = le16_to_cpu(desc->data_len);
936         fc = le16_to_cpu(desc->frame_ctl);
937
938         /* Calculate and check MIC */
939         if (status & HERMES_RXSTAT_MIC) {
940                 struct orinoco_tkip_key *key;
941                 int key_id = ((status & HERMES_RXSTAT_MIC_KEY_ID) >>
942                               HERMES_MIC_KEY_ID_SHIFT);
943                 u8 mic[MICHAEL_MIC_LEN];
944                 u8 *rxmic;
945                 u8 *src = (fc & IEEE80211_FCTL_FROMDS) ?
946                         desc->addr3 : desc->addr2;
947
948                 /* Extract Michael MIC from payload */
949                 rxmic = skb->data + skb->len - MICHAEL_MIC_LEN;
950
951                 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
952                 length -= MICHAEL_MIC_LEN;
953
954                 key = (struct orinoco_tkip_key *) priv->keys[key_id].key;
955
956                 if (!key) {
957                         printk(KERN_WARNING "%s: Received encrypted frame from "
958                                "%pM using key %i, but key is not installed\n",
959                                dev->name, src, key_id);
960                         goto drop;
961                 }
962
963                 orinoco_mic(priv->rx_tfm_mic, key->rx_mic, desc->addr1, src,
964                             0, /* priority or QoS? */
965                             skb->data, skb->len, &mic[0]);
966
967                 if (memcmp(mic, rxmic,
968                            MICHAEL_MIC_LEN)) {
969                         union iwreq_data wrqu;
970                         struct iw_michaelmicfailure wxmic;
971
972                         printk(KERN_WARNING "%s: "
973                                "Invalid Michael MIC in data frame from %pM, "
974                                "using key %i\n",
975                                dev->name, src, key_id);
976
977                         /* TODO: update stats */
978
979                         /* Notify userspace */
980                         memset(&wxmic, 0, sizeof(wxmic));
981                         wxmic.flags = key_id & IW_MICFAILURE_KEY_ID;
982                         wxmic.flags |= (desc->addr1[0] & 1) ?
983                                 IW_MICFAILURE_GROUP : IW_MICFAILURE_PAIRWISE;
984                         wxmic.src_addr.sa_family = ARPHRD_ETHER;
985                         memcpy(wxmic.src_addr.sa_data, src, ETH_ALEN);
986
987                         (void) orinoco_hw_get_tkip_iv(priv, key_id,
988                                                       &wxmic.tsc[0]);
989
990                         memset(&wrqu, 0, sizeof(wrqu));
991                         wrqu.data.length = sizeof(wxmic);
992                         wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu,
993                                             (char *) &wxmic);
994
995                         goto drop;
996                 }
997         }
998
999         /* Handle decapsulation
1000          * In most cases, the firmware tell us about SNAP frames.
1001          * For some reason, the SNAP frames sent by LinkSys APs
1002          * are not properly recognised by most firmwares.
1003          * So, check ourselves */
1004         if (length >= ENCAPS_OVERHEAD &&
1005             (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
1006              ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
1007              is_ethersnap(skb->data))) {
1008                 /* These indicate a SNAP within 802.2 LLC within
1009                    802.11 frame which we'll need to de-encapsulate to
1010                    the original EthernetII frame. */
1011                 hdr = (struct ethhdr *)skb_push(skb,
1012                                                 ETH_HLEN - ENCAPS_OVERHEAD);
1013         } else {
1014                 /* 802.3 frame - prepend 802.3 header as is */
1015                 hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
1016                 hdr->h_proto = htons(length);
1017         }
1018         memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
1019         if (fc & IEEE80211_FCTL_FROMDS)
1020                 memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
1021         else
1022                 memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
1023
1024         skb->protocol = eth_type_trans(skb, dev);
1025         skb->ip_summed = CHECKSUM_NONE;
1026         if (fc & IEEE80211_FCTL_TODS)
1027                 skb->pkt_type = PACKET_OTHERHOST;
1028
1029         /* Process the wireless stats if needed */
1030         orinoco_stat_gather(dev, skb, desc);
1031
1032         /* Pass the packet to the networking stack */
1033         netif_rx(skb);
1034         stats->rx_packets++;
1035         stats->rx_bytes += length;
1036
1037         return;
1038
1039  drop:
1040         dev_kfree_skb(skb);
1041         stats->rx_errors++;
1042         stats->rx_dropped++;
1043 }
1044
1045 static void orinoco_rx_isr_tasklet(unsigned long data)
1046 {
1047         struct orinoco_private *priv = (struct orinoco_private *) data;
1048         struct net_device *dev = priv->ndev;
1049         struct orinoco_rx_data *rx_data, *temp;
1050         struct hermes_rx_descriptor *desc;
1051         struct sk_buff *skb;
1052         unsigned long flags;
1053
1054         /* orinoco_rx requires the driver lock, and we also need to
1055          * protect priv->rx_list, so just hold the lock over the
1056          * lot.
1057          *
1058          * If orinoco_lock fails, we've unplugged the card. In this
1059          * case just abort. */
1060         if (orinoco_lock(priv, &flags) != 0)
1061                 return;
1062
1063         /* extract desc and skb from queue */
1064         list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
1065                 desc = rx_data->desc;
1066                 skb = rx_data->skb;
1067                 list_del(&rx_data->list);
1068                 kfree(rx_data);
1069
1070                 orinoco_rx(dev, desc, skb);
1071
1072                 kfree(desc);
1073         }
1074
1075         orinoco_unlock(priv, &flags);
1076 }
1077
1078 /********************************************************************/
1079 /* Rx path (info frames)                                            */
1080 /********************************************************************/
1081
1082 static void print_linkstatus(struct net_device *dev, u16 status)
1083 {
1084         char *s;
1085
1086         if (suppress_linkstatus)
1087                 return;
1088
1089         switch (status) {
1090         case HERMES_LINKSTATUS_NOT_CONNECTED:
1091                 s = "Not Connected";
1092                 break;
1093         case HERMES_LINKSTATUS_CONNECTED:
1094                 s = "Connected";
1095                 break;
1096         case HERMES_LINKSTATUS_DISCONNECTED:
1097                 s = "Disconnected";
1098                 break;
1099         case HERMES_LINKSTATUS_AP_CHANGE:
1100                 s = "AP Changed";
1101                 break;
1102         case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
1103                 s = "AP Out of Range";
1104                 break;
1105         case HERMES_LINKSTATUS_AP_IN_RANGE:
1106                 s = "AP In Range";
1107                 break;
1108         case HERMES_LINKSTATUS_ASSOC_FAILED:
1109                 s = "Association Failed";
1110                 break;
1111         default:
1112                 s = "UNKNOWN";
1113         }
1114
1115         printk(KERN_DEBUG "%s: New link status: %s (%04x)\n",
1116                dev->name, s, status);
1117 }
1118
1119 /* Search scan results for requested BSSID, join it if found */
1120 static void orinoco_join_ap(struct work_struct *work)
1121 {
1122         struct orinoco_private *priv =
1123                 container_of(work, struct orinoco_private, join_work);
1124         struct net_device *dev = priv->ndev;
1125         struct hermes *hw = &priv->hw;
1126         int err;
1127         unsigned long flags;
1128         struct join_req {
1129                 u8 bssid[ETH_ALEN];
1130                 __le16 channel;
1131         } __attribute__ ((packed)) req;
1132         const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
1133         struct prism2_scan_apinfo *atom = NULL;
1134         int offset = 4;
1135         int found = 0;
1136         u8 *buf;
1137         u16 len;
1138
1139         /* Allocate buffer for scan results */
1140         buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
1141         if (!buf)
1142                 return;
1143
1144         if (orinoco_lock(priv, &flags) != 0)
1145                 goto fail_lock;
1146
1147         /* Sanity checks in case user changed something in the meantime */
1148         if (!priv->bssid_fixed)
1149                 goto out;
1150
1151         if (strlen(priv->desired_essid) == 0)
1152                 goto out;
1153
1154         /* Read scan results from the firmware */
1155         err = hw->ops->read_ltv(hw, USER_BAP,
1156                                 HERMES_RID_SCANRESULTSTABLE,
1157                                 MAX_SCAN_LEN, &len, buf);
1158         if (err) {
1159                 printk(KERN_ERR "%s: Cannot read scan results\n",
1160                        dev->name);
1161                 goto out;
1162         }
1163
1164         len = HERMES_RECLEN_TO_BYTES(len);
1165
1166         /* Go through the scan results looking for the channel of the AP
1167          * we were requested to join */
1168         for (; offset + atom_len <= len; offset += atom_len) {
1169                 atom = (struct prism2_scan_apinfo *) (buf + offset);
1170                 if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
1171                         found = 1;
1172                         break;
1173                 }
1174         }
1175
1176         if (!found) {
1177                 DEBUG(1, "%s: Requested AP not found in scan results\n",
1178                       dev->name);
1179                 goto out;
1180         }
1181
1182         memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
1183         req.channel = atom->channel;    /* both are little-endian */
1184         err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
1185                                   &req);
1186         if (err)
1187                 printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
1188
1189  out:
1190         orinoco_unlock(priv, &flags);
1191
1192  fail_lock:
1193         kfree(buf);
1194 }
1195
1196 /* Send new BSSID to userspace */
1197 static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
1198 {
1199         struct net_device *dev = priv->ndev;
1200         struct hermes *hw = &priv->hw;
1201         union iwreq_data wrqu;
1202         int err;
1203
1204         err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
1205                                 ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
1206         if (err != 0)
1207                 return;
1208
1209         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1210
1211         /* Send event to user space */
1212         wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
1213 }
1214
1215 static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
1216 {
1217         struct net_device *dev = priv->ndev;
1218         struct hermes *hw = &priv->hw;
1219         union iwreq_data wrqu;
1220         int err;
1221         u8 buf[88];
1222         u8 *ie;
1223
1224         if (!priv->has_wpa)
1225                 return;
1226
1227         err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
1228                                 sizeof(buf), NULL, &buf);
1229         if (err != 0)
1230                 return;
1231
1232         ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1233         if (ie) {
1234                 int rem = sizeof(buf) - (ie - &buf[0]);
1235                 wrqu.data.length = ie[1] + 2;
1236                 if (wrqu.data.length > rem)
1237                         wrqu.data.length = rem;
1238
1239                 if (wrqu.data.length)
1240                         /* Send event to user space */
1241                         wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
1242         }
1243 }
1244
1245 static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
1246 {
1247         struct net_device *dev = priv->ndev;
1248         struct hermes *hw = &priv->hw;
1249         union iwreq_data wrqu;
1250         int err;
1251         u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
1252         u8 *ie;
1253
1254         if (!priv->has_wpa)
1255                 return;
1256
1257         err = hw->ops->read_ltv(hw, USER_BAP,
1258                                 HERMES_RID_CURRENT_ASSOC_RESP_INFO,
1259                                 sizeof(buf), NULL, &buf);
1260         if (err != 0)
1261                 return;
1262
1263         ie = orinoco_get_wpa_ie(buf, sizeof(buf));
1264         if (ie) {
1265                 int rem = sizeof(buf) - (ie - &buf[0]);
1266                 wrqu.data.length = ie[1] + 2;
1267                 if (wrqu.data.length > rem)
1268                         wrqu.data.length = rem;
1269
1270                 if (wrqu.data.length)
1271                         /* Send event to user space */
1272                         wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
1273         }
1274 }
1275
1276 static void orinoco_send_wevents(struct work_struct *work)
1277 {
1278         struct orinoco_private *priv =
1279                 container_of(work, struct orinoco_private, wevent_work);
1280         unsigned long flags;
1281
1282         if (orinoco_lock(priv, &flags) != 0)
1283                 return;
1284
1285         orinoco_send_assocreqie_wevent(priv);
1286         orinoco_send_assocrespie_wevent(priv);
1287         orinoco_send_bssid_wevent(priv);
1288
1289         orinoco_unlock(priv, &flags);
1290 }
1291
1292 static void qbuf_scan(struct orinoco_private *priv, void *buf,
1293                       int len, int type)
1294 {
1295         struct orinoco_scan_data *sd;
1296         unsigned long flags;
1297
1298         sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
1299         sd->buf = buf;
1300         sd->len = len;
1301         sd->type = type;
1302
1303         spin_lock_irqsave(&priv->scan_lock, flags);
1304         list_add_tail(&sd->list, &priv->scan_list);
1305         spin_unlock_irqrestore(&priv->scan_lock, flags);
1306
1307         schedule_work(&priv->process_scan);
1308 }
1309
1310 static void qabort_scan(struct orinoco_private *priv)
1311 {
1312         struct orinoco_scan_data *sd;
1313         unsigned long flags;
1314
1315         sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
1316         sd->len = -1; /* Abort */
1317
1318         spin_lock_irqsave(&priv->scan_lock, flags);
1319         list_add_tail(&sd->list, &priv->scan_list);
1320         spin_unlock_irqrestore(&priv->scan_lock, flags);
1321
1322         schedule_work(&priv->process_scan);
1323 }
1324
1325 static void orinoco_process_scan_results(struct work_struct *work)
1326 {
1327         struct orinoco_private *priv =
1328                 container_of(work, struct orinoco_private, process_scan);
1329         struct orinoco_scan_data *sd, *temp;
1330         unsigned long flags;
1331         void *buf;
1332         int len;
1333         int type;
1334
1335         spin_lock_irqsave(&priv->scan_lock, flags);
1336         list_for_each_entry_safe(sd, temp, &priv->scan_list, list) {
1337                 spin_unlock_irqrestore(&priv->scan_lock, flags);
1338
1339                 buf = sd->buf;
1340                 len = sd->len;
1341                 type = sd->type;
1342
1343                 list_del(&sd->list);
1344                 kfree(sd);
1345
1346                 if (len > 0) {
1347                         if (type == HERMES_INQ_CHANNELINFO)
1348                                 orinoco_add_extscan_result(priv, buf, len);
1349                         else
1350                                 orinoco_add_hostscan_results(priv, buf, len);
1351
1352                         kfree(buf);
1353                 } else if (priv->scan_request) {
1354                         /* Either abort or complete the scan */
1355                         cfg80211_scan_done(priv->scan_request, (len < 0));
1356                         priv->scan_request = NULL;
1357                 }
1358
1359                 spin_lock_irqsave(&priv->scan_lock, flags);
1360         }
1361         spin_unlock_irqrestore(&priv->scan_lock, flags);
1362 }
1363
1364 void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
1365 {
1366         struct orinoco_private *priv = ndev_priv(dev);
1367         u16 infofid;
1368         struct {
1369                 __le16 len;
1370                 __le16 type;
1371         } __attribute__ ((packed)) info;
1372         int len, type;
1373         int err;
1374
1375         /* This is an answer to an INQUIRE command that we did earlier,
1376          * or an information "event" generated by the card
1377          * The controller return to us a pseudo frame containing
1378          * the information in question - Jean II */
1379         infofid = hermes_read_regn(hw, INFOFID);
1380
1381         /* Read the info frame header - don't try too hard */
1382         err = hw->ops->bap_pread(hw, IRQ_BAP, &info, sizeof(info),
1383                                  infofid, 0);
1384         if (err) {
1385                 printk(KERN_ERR "%s: error %d reading info frame. "
1386                        "Frame dropped.\n", dev->name, err);
1387                 return;
1388         }
1389
1390         len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
1391         type = le16_to_cpu(info.type);
1392
1393         switch (type) {
1394         case HERMES_INQ_TALLIES: {
1395                 struct hermes_tallies_frame tallies;
1396                 struct iw_statistics *wstats = &priv->wstats;
1397
1398                 if (len > sizeof(tallies)) {
1399                         printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
1400                                dev->name, len);
1401                         len = sizeof(tallies);
1402                 }
1403
1404                 err = hw->ops->bap_pread(hw, IRQ_BAP, &tallies, len,
1405                                          infofid, sizeof(info));
1406                 if (err)
1407                         break;
1408
1409                 /* Increment our various counters */
1410                 /* wstats->discard.nwid - no wrong BSSID stuff */
1411                 wstats->discard.code +=
1412                         le16_to_cpu(tallies.RxWEPUndecryptable);
1413                 if (len == sizeof(tallies))
1414                         wstats->discard.code +=
1415                                 le16_to_cpu(tallies.RxDiscards_WEPICVError) +
1416                                 le16_to_cpu(tallies.RxDiscards_WEPExcluded);
1417                 wstats->discard.misc +=
1418                         le16_to_cpu(tallies.TxDiscardsWrongSA);
1419                 wstats->discard.fragment +=
1420                         le16_to_cpu(tallies.RxMsgInBadMsgFragments);
1421                 wstats->discard.retries +=
1422                         le16_to_cpu(tallies.TxRetryLimitExceeded);
1423                 /* wstats->miss.beacon - no match */
1424         }
1425         break;
1426         case HERMES_INQ_LINKSTATUS: {
1427                 struct hermes_linkstatus linkstatus;
1428                 u16 newstatus;
1429                 int connected;
1430
1431                 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
1432                         break;
1433
1434                 if (len != sizeof(linkstatus)) {
1435                         printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
1436                                dev->name, len);
1437                         break;
1438                 }
1439
1440                 err = hw->ops->bap_pread(hw, IRQ_BAP, &linkstatus, len,
1441                                          infofid, sizeof(info));
1442                 if (err)
1443                         break;
1444                 newstatus = le16_to_cpu(linkstatus.linkstatus);
1445
1446                 /* Symbol firmware uses "out of range" to signal that
1447                  * the hostscan frame can be requested.  */
1448                 if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
1449                     priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
1450                     priv->has_hostscan && priv->scan_request) {
1451                         hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
1452                         break;
1453                 }
1454
1455                 connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
1456                         || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
1457                         || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
1458
1459                 if (connected)
1460                         netif_carrier_on(dev);
1461                 else if (!ignore_disconnect)
1462                         netif_carrier_off(dev);
1463
1464                 if (newstatus != priv->last_linkstatus) {
1465                         priv->last_linkstatus = newstatus;
1466                         print_linkstatus(dev, newstatus);
1467                         /* The info frame contains only one word which is the
1468                          * status (see hermes.h). The status is pretty boring
1469                          * in itself, that's why we export the new BSSID...
1470                          * Jean II */
1471                         schedule_work(&priv->wevent_work);
1472                 }
1473         }
1474         break;
1475         case HERMES_INQ_SCAN:
1476                 if (!priv->scan_request && priv->bssid_fixed &&
1477                     priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
1478                         schedule_work(&priv->join_work);
1479                         break;
1480                 }
1481                 /* fall through */
1482         case HERMES_INQ_HOSTSCAN:
1483         case HERMES_INQ_HOSTSCAN_SYMBOL: {
1484                 /* Result of a scanning. Contains information about
1485                  * cells in the vicinity - Jean II */
1486                 unsigned char *buf;
1487
1488                 /* Sanity check */
1489                 if (len > 4096) {
1490                         printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
1491                                dev->name, len);
1492                         qabort_scan(priv);
1493                         break;
1494                 }
1495
1496                 /* Allocate buffer for results */
1497                 buf = kmalloc(len, GFP_ATOMIC);
1498                 if (buf == NULL) {
1499                         /* No memory, so can't printk()... */
1500                         qabort_scan(priv);
1501                         break;
1502                 }
1503
1504                 /* Read scan data */
1505                 err = hw->ops->bap_pread(hw, IRQ_BAP, (void *) buf, len,
1506                                          infofid, sizeof(info));
1507                 if (err) {
1508                         kfree(buf);
1509                         qabort_scan(priv);
1510                         break;
1511                 }
1512
1513 #ifdef ORINOCO_DEBUG
1514                 {
1515                         int     i;
1516                         printk(KERN_DEBUG "Scan result [%02X", buf[0]);
1517                         for (i = 1; i < (len * 2); i++)
1518                                 printk(":%02X", buf[i]);
1519                         printk("]\n");
1520                 }
1521 #endif  /* ORINOCO_DEBUG */
1522
1523                 qbuf_scan(priv, buf, len, type);
1524         }
1525         break;
1526         case HERMES_INQ_CHANNELINFO:
1527         {
1528                 struct agere_ext_scan_info *bss;
1529
1530                 if (!priv->scan_request) {
1531                         printk(KERN_DEBUG "%s: Got chaninfo without scan, "
1532                                "len=%d\n", dev->name, len);
1533                         break;
1534                 }
1535
1536                 /* An empty result indicates that the scan is complete */
1537                 if (len == 0) {
1538                         qbuf_scan(priv, NULL, len, type);
1539                         break;
1540                 }
1541
1542                 /* Sanity check */
1543                 else if (len < (offsetof(struct agere_ext_scan_info,
1544                                            data) + 2)) {
1545                         /* Drop this result now so we don't have to
1546                          * keep checking later */
1547                         printk(KERN_WARNING
1548                                "%s: Ext scan results too short (%d bytes)\n",
1549                                dev->name, len);
1550                         break;
1551                 }
1552
1553                 bss = kmalloc(len, GFP_ATOMIC);
1554                 if (bss == NULL)
1555                         break;
1556
1557                 /* Read scan data */
1558                 err = hw->ops->bap_pread(hw, IRQ_BAP, (void *) bss, len,
1559                                          infofid, sizeof(info));
1560                 if (err)
1561                         kfree(bss);
1562                 else
1563                         qbuf_scan(priv, bss, len, type);
1564
1565                 break;
1566         }
1567         case HERMES_INQ_SEC_STAT_AGERE:
1568                 /* Security status (Agere specific) */
1569                 /* Ignore this frame for now */
1570                 if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
1571                         break;
1572                 /* fall through */
1573         default:
1574                 printk(KERN_DEBUG "%s: Unknown information frame received: "
1575                        "type 0x%04x, length %d\n", dev->name, type, len);
1576                 /* We don't actually do anything about it */
1577                 break;
1578         }
1579
1580         return;
1581 }
1582 EXPORT_SYMBOL(__orinoco_ev_info);
1583
1584 static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
1585 {
1586         if (net_ratelimit())
1587                 printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
1588 }
1589
1590 /********************************************************************/
1591 /* Internal hardware control routines                               */
1592 /********************************************************************/
1593
1594 static int __orinoco_up(struct orinoco_private *priv)
1595 {
1596         struct net_device *dev = priv->ndev;
1597         struct hermes *hw = &priv->hw;
1598         int err;
1599
1600         netif_carrier_off(dev); /* just to make sure */
1601
1602         err = __orinoco_commit(priv);
1603         if (err) {
1604                 printk(KERN_ERR "%s: Error %d configuring card\n",
1605                        dev->name, err);
1606                 return err;
1607         }
1608
1609         /* Fire things up again */
1610         hermes_set_irqmask(hw, ORINOCO_INTEN);
1611         err = hermes_enable_port(hw, 0);
1612         if (err) {
1613                 printk(KERN_ERR "%s: Error %d enabling MAC port\n",
1614                        dev->name, err);
1615                 return err;
1616         }
1617
1618         netif_start_queue(dev);
1619
1620         return 0;
1621 }
1622
1623 static int __orinoco_down(struct orinoco_private *priv)
1624 {
1625         struct net_device *dev = priv->ndev;
1626         struct hermes *hw = &priv->hw;
1627         int err;
1628
1629         netif_stop_queue(dev);
1630
1631         if (!priv->hw_unavailable) {
1632                 if (!priv->broken_disableport) {
1633                         err = hermes_disable_port(hw, 0);
1634                         if (err) {
1635                                 /* Some firmwares (e.g. Intersil 1.3.x) seem
1636                                  * to have problems disabling the port, oh
1637                                  * well, too bad. */
1638                                 printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
1639                                        dev->name, err);
1640                                 priv->broken_disableport = 1;
1641                         }
1642                 }
1643                 hermes_set_irqmask(hw, 0);
1644                 hermes_write_regn(hw, EVACK, 0xffff);
1645         }
1646
1647         /* firmware will have to reassociate */
1648         netif_carrier_off(dev);
1649         priv->last_linkstatus = 0xffff;
1650
1651         return 0;
1652 }
1653
1654 static int orinoco_reinit_firmware(struct orinoco_private *priv)
1655 {
1656         struct hermes *hw = &priv->hw;
1657         int err;
1658
1659         err = hw->ops->init(hw);
1660         if (priv->do_fw_download && !err) {
1661                 err = orinoco_download(priv);
1662                 if (err)
1663                         priv->do_fw_download = 0;
1664         }
1665         if (!err)
1666                 err = orinoco_hw_allocate_fid(priv);
1667
1668         return err;
1669 }
1670
1671 static int
1672 __orinoco_set_multicast_list(struct net_device *dev)
1673 {
1674         struct orinoco_private *priv = ndev_priv(dev);
1675         int err = 0;
1676         int promisc, mc_count;
1677
1678         /* The Hermes doesn't seem to have an allmulti mode, so we go
1679          * into promiscuous mode and let the upper levels deal. */
1680         if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
1681             (netdev_mc_count(dev) > MAX_MULTICAST(priv))) {
1682                 promisc = 1;
1683                 mc_count = 0;
1684         } else {
1685                 promisc = 0;
1686                 mc_count = netdev_mc_count(dev);
1687         }
1688
1689         err = __orinoco_hw_set_multicast_list(priv, dev, mc_count, promisc);
1690
1691         return err;
1692 }
1693
1694 /* This must be called from user context, without locks held - use
1695  * schedule_work() */
1696 void orinoco_reset(struct work_struct *work)
1697 {
1698         struct orinoco_private *priv =
1699                 container_of(work, struct orinoco_private, reset_work);
1700         struct net_device *dev = priv->ndev;
1701         struct hermes *hw = &priv->hw;
1702         int err;
1703         unsigned long flags;
1704
1705         if (orinoco_lock(priv, &flags) != 0)
1706                 /* When the hardware becomes available again, whatever
1707                  * detects that is responsible for re-initializing
1708                  * it. So no need for anything further */
1709                 return;
1710
1711         netif_stop_queue(dev);
1712
1713         /* Shut off interrupts.  Depending on what state the hardware
1714          * is in, this might not work, but we'll try anyway */
1715         hermes_set_irqmask(hw, 0);
1716         hermes_write_regn(hw, EVACK, 0xffff);
1717
1718         priv->hw_unavailable++;
1719         priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
1720         netif_carrier_off(dev);
1721
1722         orinoco_unlock(priv, &flags);
1723
1724         /* Scanning support: Notify scan cancellation */
1725         if (priv->scan_request) {
1726                 cfg80211_scan_done(priv->scan_request, 1);
1727                 priv->scan_request = NULL;
1728         }
1729
1730         if (priv->hard_reset) {
1731                 err = (*priv->hard_reset)(priv);
1732                 if (err) {
1733                         printk(KERN_ERR "%s: orinoco_reset: Error %d "
1734                                "performing hard reset\n", dev->name, err);
1735                         goto disable;
1736                 }
1737         }
1738
1739         err = orinoco_reinit_firmware(priv);
1740         if (err) {
1741                 printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
1742                        dev->name, err);
1743                 goto disable;
1744         }
1745
1746         /* This has to be called from user context */
1747         orinoco_lock_irq(priv);
1748
1749         priv->hw_unavailable--;
1750
1751         /* priv->open or priv->hw_unavailable might have changed while
1752          * we dropped the lock */
1753         if (priv->open && (!priv->hw_unavailable)) {
1754                 err = __orinoco_up(priv);
1755                 if (err) {
1756                         printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
1757                                dev->name, err);
1758                 } else
1759                         dev->trans_start = jiffies;
1760         }
1761
1762         orinoco_unlock_irq(priv);
1763
1764         return;
1765  disable:
1766         hermes_set_irqmask(hw, 0);
1767         netif_device_detach(dev);
1768         printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
1769 }
1770
1771 static int __orinoco_commit(struct orinoco_private *priv)
1772 {
1773         struct net_device *dev = priv->ndev;
1774         int err = 0;
1775
1776         err = orinoco_hw_program_rids(priv);
1777
1778         /* FIXME: what about netif_tx_lock */
1779         (void) __orinoco_set_multicast_list(dev);
1780
1781         return err;
1782 }
1783
1784 /* Ensures configuration changes are applied. May result in a reset.
1785  * The caller should hold priv->lock
1786  */
1787 int orinoco_commit(struct orinoco_private *priv)
1788 {
1789         struct net_device *dev = priv->ndev;
1790         hermes_t *hw = &priv->hw;
1791         int err;
1792
1793         if (priv->broken_disableport) {
1794                 schedule_work(&priv->reset_work);
1795                 return 0;
1796         }
1797
1798         err = hermes_disable_port(hw, 0);
1799         if (err) {
1800                 printk(KERN_WARNING "%s: Unable to disable port "
1801                        "while reconfiguring card\n", dev->name);
1802                 priv->broken_disableport = 1;
1803                 goto out;
1804         }
1805
1806         err = __orinoco_commit(priv);
1807         if (err) {
1808                 printk(KERN_WARNING "%s: Unable to reconfigure card\n",
1809                        dev->name);
1810                 goto out;
1811         }
1812
1813         err = hermes_enable_port(hw, 0);
1814         if (err) {
1815                 printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
1816                        dev->name);
1817                 goto out;
1818         }
1819
1820  out:
1821         if (err) {
1822                 printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
1823                 schedule_work(&priv->reset_work);
1824                 err = 0;
1825         }
1826         return err;
1827 }
1828
1829 /********************************************************************/
1830 /* Interrupt handler                                                */
1831 /********************************************************************/
1832
1833 static void __orinoco_ev_tick(struct net_device *dev, hermes_t *hw)
1834 {
1835         printk(KERN_DEBUG "%s: TICK\n", dev->name);
1836 }
1837
1838 static void __orinoco_ev_wterr(struct net_device *dev, hermes_t *hw)
1839 {
1840         /* This seems to happen a fair bit under load, but ignoring it
1841            seems to work fine...*/
1842         printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
1843                dev->name);
1844 }
1845
1846 irqreturn_t orinoco_interrupt(int irq, void *dev_id)
1847 {
1848         struct orinoco_private *priv = dev_id;
1849         struct net_device *dev = priv->ndev;
1850         hermes_t *hw = &priv->hw;
1851         int count = MAX_IRQLOOPS_PER_IRQ;
1852         u16 evstat, events;
1853         /* These are used to detect a runaway interrupt situation.
1854          *
1855          * If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
1856          * we panic and shut down the hardware
1857          */
1858         /* jiffies value the last time we were called */
1859         static int last_irq_jiffy; /* = 0 */
1860         static int loops_this_jiffy; /* = 0 */
1861         unsigned long flags;
1862
1863         if (orinoco_lock(priv, &flags) != 0) {
1864                 /* If hw is unavailable - we don't know if the irq was
1865                  * for us or not */
1866                 return IRQ_HANDLED;
1867         }
1868
1869         evstat = hermes_read_regn(hw, EVSTAT);
1870         events = evstat & hw->inten;
1871         if (!events) {
1872                 orinoco_unlock(priv, &flags);
1873                 return IRQ_NONE;
1874         }
1875
1876         if (jiffies != last_irq_jiffy)
1877                 loops_this_jiffy = 0;
1878         last_irq_jiffy = jiffies;
1879
1880         while (events && count--) {
1881                 if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
1882                         printk(KERN_WARNING "%s: IRQ handler is looping too "
1883                                "much! Resetting.\n", dev->name);
1884                         /* Disable interrupts for now */
1885                         hermes_set_irqmask(hw, 0);
1886                         schedule_work(&priv->reset_work);
1887                         break;
1888                 }
1889
1890                 /* Check the card hasn't been removed */
1891                 if (!hermes_present(hw)) {
1892                         DEBUG(0, "orinoco_interrupt(): card removed\n");
1893                         break;
1894                 }
1895
1896                 if (events & HERMES_EV_TICK)
1897                         __orinoco_ev_tick(dev, hw);
1898                 if (events & HERMES_EV_WTERR)
1899                         __orinoco_ev_wterr(dev, hw);
1900                 if (events & HERMES_EV_INFDROP)
1901                         __orinoco_ev_infdrop(dev, hw);
1902                 if (events & HERMES_EV_INFO)
1903                         __orinoco_ev_info(dev, hw);
1904                 if (events & HERMES_EV_RX)
1905                         __orinoco_ev_rx(dev, hw);
1906                 if (events & HERMES_EV_TXEXC)
1907                         __orinoco_ev_txexc(dev, hw);
1908                 if (events & HERMES_EV_TX)
1909                         __orinoco_ev_tx(dev, hw);
1910                 if (events & HERMES_EV_ALLOC)
1911                         __orinoco_ev_alloc(dev, hw);
1912
1913                 hermes_write_regn(hw, EVACK, evstat);
1914
1915                 evstat = hermes_read_regn(hw, EVSTAT);
1916                 events = evstat & hw->inten;
1917         };
1918
1919         orinoco_unlock(priv, &flags);
1920         return IRQ_HANDLED;
1921 }
1922 EXPORT_SYMBOL(orinoco_interrupt);
1923
1924 /********************************************************************/
1925 /* Power management                                                 */
1926 /********************************************************************/
1927 #if defined(CONFIG_PM_SLEEP) && !defined(CONFIG_HERMES_CACHE_FW_ON_INIT)
1928 static int orinoco_pm_notifier(struct notifier_block *notifier,
1929                                unsigned long pm_event,
1930                                void *unused)
1931 {
1932         struct orinoco_private *priv = container_of(notifier,
1933                                                     struct orinoco_private,
1934                                                     pm_notifier);
1935
1936         /* All we need to do is cache the firmware before suspend, and
1937          * release it when we come out.
1938          *
1939          * Only need to do this if we're downloading firmware. */
1940         if (!priv->do_fw_download)
1941                 return NOTIFY_DONE;
1942
1943         switch (pm_event) {
1944         case PM_HIBERNATION_PREPARE:
1945         case PM_SUSPEND_PREPARE:
1946                 orinoco_cache_fw(priv, 0);
1947                 break;
1948
1949         case PM_POST_RESTORE:
1950                 /* Restore from hibernation failed. We need to clean
1951                  * up in exactly the same way, so fall through. */
1952         case PM_POST_HIBERNATION:
1953         case PM_POST_SUSPEND:
1954                 orinoco_uncache_fw(priv);
1955                 break;
1956
1957         case PM_RESTORE_PREPARE:
1958         default:
1959                 break;
1960         }
1961
1962         return NOTIFY_DONE;
1963 }
1964
1965 static void orinoco_register_pm_notifier(struct orinoco_private *priv)
1966 {
1967         priv->pm_notifier.notifier_call = orinoco_pm_notifier;
1968         register_pm_notifier(&priv->pm_notifier);
1969 }
1970
1971 static void orinoco_unregister_pm_notifier(struct orinoco_private *priv)
1972 {
1973         unregister_pm_notifier(&priv->pm_notifier);
1974 }
1975 #else /* !PM_SLEEP || HERMES_CACHE_FW_ON_INIT */
1976 #define orinoco_register_pm_notifier(priv) do { } while(0)
1977 #define orinoco_unregister_pm_notifier(priv) do { } while(0)
1978 #endif
1979
1980 /********************************************************************/
1981 /* Initialization                                                   */
1982 /********************************************************************/
1983
1984 int orinoco_init(struct orinoco_private *priv)
1985 {
1986         struct device *dev = priv->dev;
1987         struct wiphy *wiphy = priv_to_wiphy(priv);
1988         hermes_t *hw = &priv->hw;
1989         int err = 0;
1990
1991         /* No need to lock, the hw_unavailable flag is already set in
1992          * alloc_orinocodev() */
1993         priv->nicbuf_size = IEEE80211_MAX_FRAME_LEN + ETH_HLEN;
1994
1995         /* Initialize the firmware */
1996         err = hw->ops->init(hw);
1997         if (err != 0) {
1998                 dev_err(dev, "Failed to initialize firmware (err = %d)\n",
1999                         err);
2000                 goto out;
2001         }
2002
2003         err = determine_fw_capabilities(priv, wiphy->fw_version,
2004                                         sizeof(wiphy->fw_version),
2005                                         &wiphy->hw_version);
2006         if (err != 0) {
2007                 dev_err(dev, "Incompatible firmware, aborting\n");
2008                 goto out;
2009         }
2010
2011         if (priv->do_fw_download) {
2012 #ifdef CONFIG_HERMES_CACHE_FW_ON_INIT
2013                 orinoco_cache_fw(priv, 0);
2014 #endif
2015
2016                 err = orinoco_download(priv);
2017                 if (err)
2018                         priv->do_fw_download = 0;
2019
2020                 /* Check firmware version again */
2021                 err = determine_fw_capabilities(priv, wiphy->fw_version,
2022                                                 sizeof(wiphy->fw_version),
2023                                                 &wiphy->hw_version);
2024                 if (err != 0) {
2025                         dev_err(dev, "Incompatible firmware, aborting\n");
2026                         goto out;
2027                 }
2028         }
2029
2030         if (priv->has_port3)
2031                 dev_info(dev, "Ad-hoc demo mode supported\n");
2032         if (priv->has_ibss)
2033                 dev_info(dev, "IEEE standard IBSS ad-hoc mode supported\n");
2034         if (priv->has_wep)
2035                 dev_info(dev, "WEP supported, %s-bit key\n",
2036                          priv->has_big_wep ? "104" : "40");
2037         if (priv->has_wpa) {
2038                 dev_info(dev, "WPA-PSK supported\n");
2039                 if (orinoco_mic_init(priv)) {
2040                         dev_err(dev, "Failed to setup MIC crypto algorithm. "
2041                                 "Disabling WPA support\n");
2042                         priv->has_wpa = 0;
2043                 }
2044         }
2045
2046         err = orinoco_hw_read_card_settings(priv, wiphy->perm_addr);
2047         if (err)
2048                 goto out;
2049
2050         err = orinoco_hw_allocate_fid(priv);
2051         if (err) {
2052                 dev_err(dev, "Failed to allocate NIC buffer!\n");
2053                 goto out;
2054         }
2055
2056         /* Set up the default configuration */
2057         priv->iw_mode = NL80211_IFTYPE_STATION;
2058         /* By default use IEEE/IBSS ad-hoc mode if we have it */
2059         priv->prefer_port3 = priv->has_port3 && (!priv->has_ibss);
2060         set_port_type(priv);
2061         priv->channel = 0; /* use firmware default */
2062
2063         priv->promiscuous = 0;
2064         priv->encode_alg = ORINOCO_ALG_NONE;
2065         priv->tx_key = 0;
2066         priv->wpa_enabled = 0;
2067         priv->tkip_cm_active = 0;
2068         priv->key_mgmt = 0;
2069         priv->wpa_ie_len = 0;
2070         priv->wpa_ie = NULL;
2071
2072         if (orinoco_wiphy_register(wiphy)) {
2073                 err = -ENODEV;
2074                 goto out;
2075         }
2076
2077         /* Make the hardware available, as long as it hasn't been
2078          * removed elsewhere (e.g. by PCMCIA hot unplug) */
2079         orinoco_lock_irq(priv);
2080         priv->hw_unavailable--;
2081         orinoco_unlock_irq(priv);
2082
2083         dev_dbg(dev, "Ready\n");
2084
2085  out:
2086         return err;
2087 }
2088 EXPORT_SYMBOL(orinoco_init);
2089
2090 static const struct net_device_ops orinoco_netdev_ops = {
2091         .ndo_open               = orinoco_open,
2092         .ndo_stop               = orinoco_stop,
2093         .ndo_start_xmit         = orinoco_xmit,
2094         .ndo_set_multicast_list = orinoco_set_multicast_list,
2095         .ndo_change_mtu         = orinoco_change_mtu,
2096         .ndo_set_mac_address    = eth_mac_addr,
2097         .ndo_validate_addr      = eth_validate_addr,
2098         .ndo_tx_timeout         = orinoco_tx_timeout,
2099         .ndo_get_stats          = orinoco_get_stats,
2100 };
2101
2102 /* Allocate private data.
2103  *
2104  * This driver has a number of structures associated with it
2105  *  netdev - Net device structure for each network interface
2106  *  wiphy - structure associated with wireless phy
2107  *  wireless_dev (wdev) - structure for each wireless interface
2108  *  hw - structure for hermes chip info
2109  *  card - card specific structure for use by the card driver
2110  *         (airport, orinoco_cs)
2111  *  priv - orinoco private data
2112  *  device - generic linux device structure
2113  *
2114  *  +---------+    +---------+
2115  *  |  wiphy  |    | netdev  |
2116  *  | +-------+    | +-------+
2117  *  | | priv  |    | | wdev  |
2118  *  | | +-----+    +-+-------+
2119  *  | | | hw  |
2120  *  | +-+-----+
2121  *  | | card  |
2122  *  +-+-------+
2123  *
2124  * priv has a link to netdev and device
2125  * wdev has a link to wiphy
2126  */
2127 struct orinoco_private
2128 *alloc_orinocodev(int sizeof_card,
2129                   struct device *device,
2130                   int (*hard_reset)(struct orinoco_private *),
2131                   int (*stop_fw)(struct orinoco_private *, int))
2132 {
2133         struct orinoco_private *priv;
2134         struct wiphy *wiphy;
2135
2136         /* allocate wiphy
2137          * NOTE: We only support a single virtual interface
2138          *       but this may change when monitor mode is added
2139          */
2140         wiphy = wiphy_new(&orinoco_cfg_ops,
2141                           sizeof(struct orinoco_private) + sizeof_card);
2142         if (!wiphy)
2143                 return NULL;
2144
2145         priv = wiphy_priv(wiphy);
2146         priv->dev = device;
2147
2148         if (sizeof_card)
2149                 priv->card = (void *)((unsigned long)priv
2150                                       + sizeof(struct orinoco_private));
2151         else
2152                 priv->card = NULL;
2153
2154         orinoco_wiphy_init(wiphy);
2155
2156 #ifdef WIRELESS_SPY
2157         priv->wireless_data.spy_data = &priv->spy_data;
2158 #endif
2159
2160         /* Set up default callbacks */
2161         priv->hard_reset = hard_reset;
2162         priv->stop_fw = stop_fw;
2163
2164         spin_lock_init(&priv->lock);
2165         priv->open = 0;
2166         priv->hw_unavailable = 1; /* orinoco_init() must clear this
2167                                    * before anything else touches the
2168                                    * hardware */
2169         INIT_WORK(&priv->reset_work, orinoco_reset);
2170         INIT_WORK(&priv->join_work, orinoco_join_ap);
2171         INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
2172
2173         INIT_LIST_HEAD(&priv->rx_list);
2174         tasklet_init(&priv->rx_tasklet, orinoco_rx_isr_tasklet,
2175                      (unsigned long) priv);
2176
2177         spin_lock_init(&priv->scan_lock);
2178         INIT_LIST_HEAD(&priv->scan_list);
2179         INIT_WORK(&priv->process_scan, orinoco_process_scan_results);
2180
2181         priv->last_linkstatus = 0xffff;
2182
2183 #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
2184         priv->cached_pri_fw = NULL;
2185         priv->cached_fw = NULL;
2186 #endif
2187
2188         /* Register PM notifiers */
2189         orinoco_register_pm_notifier(priv);
2190
2191         return priv;
2192 }
2193 EXPORT_SYMBOL(alloc_orinocodev);
2194
2195 /* We can only support a single interface. We provide a separate
2196  * function to set it up to distinguish between hardware
2197  * initialisation and interface setup.
2198  *
2199  * The base_addr and irq parameters are passed on to netdev for use
2200  * with SIOCGIFMAP.
2201  */
2202 int orinoco_if_add(struct orinoco_private *priv,
2203                    unsigned long base_addr,
2204                    unsigned int irq,
2205                    const struct net_device_ops *ops)
2206 {
2207         struct wiphy *wiphy = priv_to_wiphy(priv);
2208         struct wireless_dev *wdev;
2209         struct net_device *dev;
2210         int ret;
2211
2212         dev = alloc_etherdev(sizeof(struct wireless_dev));
2213
2214         if (!dev)
2215                 return -ENOMEM;
2216
2217         /* Initialise wireless_dev */
2218         wdev = netdev_priv(dev);
2219         wdev->wiphy = wiphy;
2220         wdev->iftype = NL80211_IFTYPE_STATION;
2221
2222         /* Setup / override net_device fields */
2223         dev->ieee80211_ptr = wdev;
2224         dev->watchdog_timeo = HZ; /* 1 second timeout */
2225         dev->wireless_handlers = &orinoco_handler_def;
2226 #ifdef WIRELESS_SPY
2227         dev->wireless_data = &priv->wireless_data;
2228 #endif
2229         /* Default to standard ops if not set */
2230         if (ops)
2231                 dev->netdev_ops = ops;
2232         else
2233                 dev->netdev_ops = &orinoco_netdev_ops;
2234
2235         /* we use the default eth_mac_addr for setting the MAC addr */
2236
2237         /* Reserve space in skb for the SNAP header */
2238         dev->hard_header_len += ENCAPS_OVERHEAD;
2239
2240         netif_carrier_off(dev);
2241
2242         memcpy(dev->dev_addr, wiphy->perm_addr, ETH_ALEN);
2243         memcpy(dev->perm_addr, wiphy->perm_addr, ETH_ALEN);
2244
2245         dev->base_addr = base_addr;
2246         dev->irq = irq;
2247
2248         SET_NETDEV_DEV(dev, priv->dev);
2249         ret = register_netdev(dev);
2250         if (ret)
2251                 goto fail;
2252
2253         priv->ndev = dev;
2254
2255         /* Report what we've done */
2256         dev_dbg(priv->dev, "Registerred interface %s.\n", dev->name);
2257
2258         return 0;
2259
2260  fail:
2261         free_netdev(dev);
2262         return ret;
2263 }
2264 EXPORT_SYMBOL(orinoco_if_add);
2265
2266 void orinoco_if_del(struct orinoco_private *priv)
2267 {
2268         struct net_device *dev = priv->ndev;
2269
2270         unregister_netdev(dev);
2271         free_netdev(dev);
2272 }
2273 EXPORT_SYMBOL(orinoco_if_del);
2274
2275 void free_orinocodev(struct orinoco_private *priv)
2276 {
2277         struct wiphy *wiphy = priv_to_wiphy(priv);
2278         struct orinoco_rx_data *rx_data, *temp;
2279         struct orinoco_scan_data *sd, *sdtemp;
2280
2281         wiphy_unregister(wiphy);
2282
2283         /* If the tasklet is scheduled when we call tasklet_kill it
2284          * will run one final time. However the tasklet will only
2285          * drain priv->rx_list if the hw is still available. */
2286         tasklet_kill(&priv->rx_tasklet);
2287
2288         /* Explicitly drain priv->rx_list */
2289         list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
2290                 list_del(&rx_data->list);
2291
2292                 dev_kfree_skb(rx_data->skb);
2293                 kfree(rx_data->desc);
2294                 kfree(rx_data);
2295         }
2296
2297         cancel_work_sync(&priv->process_scan);
2298         /* Explicitly drain priv->scan_list */
2299         list_for_each_entry_safe(sd, sdtemp, &priv->scan_list, list) {
2300                 list_del(&sd->list);
2301
2302                 if ((sd->len > 0) && sd->buf)
2303                         kfree(sd->buf);
2304                 kfree(sd);
2305         }
2306
2307         orinoco_unregister_pm_notifier(priv);
2308         orinoco_uncache_fw(priv);
2309
2310         priv->wpa_ie_len = 0;
2311         kfree(priv->wpa_ie);
2312         orinoco_mic_free(priv);
2313         wiphy_free(wiphy);
2314 }
2315 EXPORT_SYMBOL(free_orinocodev);
2316
2317 int orinoco_up(struct orinoco_private *priv)
2318 {
2319         struct net_device *dev = priv->ndev;
2320         unsigned long flags;
2321         int err;
2322
2323         priv->hw.ops->lock_irqsave(&priv->lock, &flags);
2324
2325         err = orinoco_reinit_firmware(priv);
2326         if (err) {
2327                 printk(KERN_ERR "%s: Error %d re-initializing firmware\n",
2328                        dev->name, err);
2329                 goto exit;
2330         }
2331
2332         netif_device_attach(dev);
2333         priv->hw_unavailable--;
2334
2335         if (priv->open && !priv->hw_unavailable) {
2336                 err = __orinoco_up(priv);
2337                 if (err)
2338                         printk(KERN_ERR "%s: Error %d restarting card\n",
2339                                dev->name, err);
2340         }
2341
2342 exit:
2343         priv->hw.ops->unlock_irqrestore(&priv->lock, &flags);
2344
2345         return 0;
2346 }
2347 EXPORT_SYMBOL(orinoco_up);
2348
2349 void orinoco_down(struct orinoco_private *priv)
2350 {
2351         struct net_device *dev = priv->ndev;
2352         unsigned long flags;
2353         int err;
2354
2355         priv->hw.ops->lock_irqsave(&priv->lock, &flags);
2356         err = __orinoco_down(priv);
2357         if (err)
2358                 printk(KERN_WARNING "%s: Error %d downing interface\n",
2359                        dev->name, err);
2360
2361         netif_device_detach(dev);
2362         priv->hw_unavailable++;
2363         priv->hw.ops->unlock_irqrestore(&priv->lock, &flags);
2364 }
2365 EXPORT_SYMBOL(orinoco_down);
2366
2367 /********************************************************************/
2368 /* Module initialization                                            */
2369 /********************************************************************/
2370
2371 /* Can't be declared "const" or the whole __initdata section will
2372  * become const */
2373 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
2374         " (David Gibson <hermes@gibson.dropbear.id.au>, "
2375         "Pavel Roskin <proski@gnu.org>, et al)";
2376
2377 static int __init init_orinoco(void)
2378 {
2379         printk(KERN_DEBUG "%s\n", version);
2380         return 0;
2381 }
2382
2383 static void __exit exit_orinoco(void)
2384 {
2385 }
2386
2387 module_init(init_orinoco);
2388 module_exit(exit_orinoco);